Tag Archives: sql

SQL custom backup

A very useful script to generate a backup file in SQL server by day of week and not have to create a maintenance plan.

declare @a as nvarchar(200)
set @a=N'\\backupserver\backups\ddbb' + DATENAME(WEEKDAY, GETDATE()) + '.tbk'
BACKUP DATABASE [bbdd] TO  DISK =@a WITH NOFORMAT, INIT,  NAME ='copia', SKIP, NOREWIND, NOUNLOAD, COMPRESSION,  STATS = 10
GO
Facebooktwitterredditpinterestlinkedinmail

Read More ...

PHP flat file reading and recording in sql

This example illustrates how to read a file. Txt, and insert a record into a database MSSQLSERVER.
mysql would be the same, by simply varying the user to access the database:
 

    <?php    
    //--------------------------- Read data from file  ----------------------------    
    //read from file    
    $myFile = "meteoclimatic.htm";    
    $fh = fopen($myFile, 'r');    
    $tamany=filesize($myFile);    
    $theData = fread($fh,$tamany);    
    fclose($fh);    
    //llenO las variables    
    $hora=substr( $theData ,strrpos($theData, "*UPD=") + 16, 5);    
    $any=substr( $theData ,strrpos($theData, "*UPD=") + 11, 4);    
    $mes=substr( $theData ,strrpos($theData, "*UPD=") + 8, 2);    
    $dia=substr( $theData ,strrpos($theData,  "*UPD=") + 5 ,2 );    
    $temperatura=substr( $theData ,strrpos($theData, "*TMP=") + 5, strrpos($theData, "*WND=")-(strrpos($theData, "*TMP=") + 7));    
    $pluja=substr( $theData ,strrpos($theData, "*DPCP=") + 6, strrpos($theData, "*WRUN=")-(strrpos($theData, "*DPCP=") + 8));    
    if (trim($pluja)=="") $pluja="0";    
    $humitat=substr( $theData ,strrpos($theData, "*HUM=") + 5, strrpos($theData, "*SUN=")-(strrpos($theData, "*HUM=") + 7));    
    $viento=substr( $theData ,strrpos($theData, "*WND=") + 5, strrpos($theData, "*AZI=")-(strrpos($theData, "*WND=") + 7));    
    $racha=substr( $theData ,strrpos($theData, "*WRUN=") + 6, strrpos($theData, "*MHTM=")-(strrpos($theData, "*WRUN=") + 8));    
    $maxracha=substr( $theData ,strrpos($theData, "*DGST=") + 6, strrpos($theData, "*DSUN=")-(strrpos($theData, "*DGST=") + 8));    
    $direc=substr( $theData ,strrpos($theData, "*AZI=") + 5, strrpos($theData, "*BAR=")-(strrpos($theData, "*AZI=") + 7));    
    $pressio=substr( $theData ,strrpos($theData, "*BAR=") + 5, strrpos($theData, "*HUM=")-(strrpos($theData, "*BAR=") + 7));    
    $AMDHM=$any . $mes . $dia . $hora ;    
    //--------------------- save to  slq server  ----------------------------    
    $servidor="VISTA32";    
    $usuari="sa";    
    $password="";    
    $BBDD="RegistrosEstacion";    
    $link= mssql_connect($servidor,$usuari,$password);    
    if(!$link || !mssql_select_db($BBDD, $link))    
    {    
		mssql_close($link);    
		die('Impossible abrir Base de dades!');    
    }    
    $counter=0;    
    $result=mssql_query("select * from Registros where AMDHM='" . $AMDHM . "'",$link);    
    if ($row=mssql_fetch_array($result)) {    
		mssql_close($link);    
		die('Registro ya existente !!!!');    
    }    
    $result=mssql_query("select TOP 1 PCP from Registros  where DIA='" . $dia . "' AND MES='" . $mes . "' AND ANO='" . $any ."' ORDER BY AMDHM DESC",$link);    
    if ($row=mssql_fetch_array($result)) {    
		mssql_close($link);    
		die('Registro ja existente !!!!');    
	}    
    // FI PENDENT DE FER    
    // no existe por lo tanto lo inserto !!    
    $result=mssql_query("insert into Registros (AMDHM,DIA,MES,ANO,HORA,TMP,HUM,WND,GUST,AZI,BAR,PCP) values ('" . $AMDHM . "','" . $dia . "','" . $mes . "','" . $any .  "','" . $hora . "'," . $temperatura . "," . $humitat . "," . $viento . "," . $racha . ",'" . $direc . "'," . $pressio . "," . $pluja . ")");    
    echo("insert into Registros (AMDHM,DIA,MES,ANO,HORA,TMP,HUM,WND,GUST,AZI,BAR,PCP) values ('" . $AMDHM . "','" . $dia . "','" . $mes . "','" . $any .  "','" . $hora . "'," . $temperatura . "," . $humitat . "," . $viento . "," . $racha . ",'" . $direc . "'," . $pressio . "," . $pluja . ")");    
    mssql_close($link);    
	?>

 

Facebooktwitterredditpinterestlinkedinmail

Read More ...

Categories

Subscribe to my Newsletter




By continuing to use the site, you agree to the use of cookies. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.

Close