Page 1 sur 1

copy 2 table 2 db differente

Posté : 07 févr. 2013, 16:33
par mimi6060
bonjour j'aimerais faire une copie de la source odbc vers une base de donnée mysql .

j'ai 2 sources :
conn= new PDO('odbc:lotusreportsClose', '', ''); (table nomer frmHistoryLog)
$connecmysql = new PDO('mysql:host='.$host .';dbname='.$dbname, $user, $password );(table nomer history2)
les champs a copier sont les suivant :
$fields = "txtStateFrom,txtStateTo,txtDate,txtActionShort,txtParent";
j'ai essayer
$sql =" INSERT INTO". $connecmysql.".history2 ".$fields." SELECT ".$fields ." FROM ".$conn.".frmHistoryLog";
$connecmysql->query($sql) ;
mais cela ne fonctionne pas comment je pourais faire pour que ca soit rapide car j'ai plus de 1m de champs a copier et c'est repetitif tout les semaine

merci d'avance de m'aider :)

bonne journée

Re: copy 2 table 2 db differente

Posté : 07 févr. 2013, 23:21
par moogli
salut,


il faut d'abord fait le select et lors du traitement de la requete les insert.

tu as tout intéret à utiliser une requete préparée (non émulé) pour cela.

par exemple (en php)
<?php
<?php
$connOdbc= new PDO('odbc:lotusreportsClose', '', '');
//(table nomer frmHistoryLog)
$connecmysql = new PDO('mysql:host='.$host .';dbname='.$dbname, $user, $password );
// pour les 4 lignes qui suivent voir la doc ;)
$connecmysql->setAttribute ( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
$connecmysql->setAttribute ( PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ );
$connecmysql->setAttribute ( PDO::ATTR_EMULATE_PREPARES, false );
$connecmysql->setAttribute ( PDO::ATTR_CASE, PDO::CASE_LOWER );

$fields = "txtStateFrom,txtStateTo,txtDate,txtActionShort,txtParent";
$select = ' SELECT '.$fields .' FROM frmHistoryLog';

$insert = 'INSERT INTO history2 ('.$fields.') values(?,?,?,?,?) ';

$prepare = $connecmysql->prepare($insert);

$result = $connOdbc->query($select);
if($result === false){
	$err = $result->errorInfo();
	throw new Exception($err[2]);
}

while($data = $result->fetch()){
	$prepare->bindValue(1,$data->txtstatefrom);
	$prepare->bindValue(2,$data->txtstattto);
	$prepare->bindValue(3,$data->txtdate);
	$prepare->bindValue(4,$data->txtactionshort);
	$prepare->bindValue(5,$data->txtparent);
	$prepare->execute();
}
$result->closeCursor();
?>

Par contre tu ne pourras pas faire cela avec un script lancé dans un navigateur, si le 1m c'est pour un million, il y a des très fortes chance que cela prenne plus de 30s soir le time out par défaut ;).
donc en ligne de commande en spécifiant le time out, pour cela set_time_limit


@+

Re: copy 2 table 2 db differente

Posté : 08 févr. 2013, 10:11
par mimi6060
oui je parlais de 1million
j'ai les erreures suivant :
Notice: Trying to get property of non-object in C:\wamp\www\Dashboard\test.php on line 54
Call Stack
# Time Memory Function Location
1 0.0013 386024 {main}( ) ..\test.php:0

( ! ) Notice: Trying to get property of non-object in C:\wamp\www\Dashboard\test.php on line 55
Call Stack
# Time Memory Function Location
1 0.0013 386024 {main}( ) ..\test.php:0

( ! ) Notice: Trying to get property of non-object in C:\wamp\www\Dashboard\test.php on line 56
Call Stack
# Time Memory Function Location
1 0.0013 386024 {main}( ) ..\test.php:0

( ! ) Notice: Trying to get property of non-object in C:\wamp\www\Dashboard\test.php on line 57
Call Stack
# Time Memory Function Location
1 0.0013 386024 {main}( ) ..\test.php:0

( ! ) Notice: Trying to get property of non-object in C:\wamp\www\Dashboard\test.php on line 58
Call Stack
# Time Memory Function Location
1 0.0013 386024 {main}( ) ..\test.php:0

( ! ) Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'txtStateFrom' cannot be null' in C:\wamp\www\Dashboard\test.php on line 59
( ! ) PDOException: SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'txtStateFrom' cannot be null in C:\wamp\www\Dashboard\test.php on line 59

il aime pas
$prepare->bindValue(1,$data->txtstatefrom);
$prepare->bindValue(2,$data->txtstattto);
$prepare->bindValue(3,$data->txtdate);
$prepare->bindValue(4,$data->txtactionshort);
$prepare->bindValue(5,$data->txtparent);
aprament

<?php

set_time_limit(0);/*permet au script de s'éxécuter indéfiniment */
    $dbname= 'merlin';
    $user = 'root';
    $password = '';
    $host = 'localhost';


$connOdbc= new PDO('odbc:lotusreportsOpen', '', '');
//(table nomer frmHistoryLog)
$connecmysql = new PDO('mysql:host='.$host .';dbname='.$dbname, $user, $password );
// pour les 4 lignes qui suivent voir la doc <img src="http://forum.phpfrance.com/images/smilies/icon_wink.gif" alt=";)" title="Wink" />
$connecmysql->setAttribute ( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
$connecmysql->setAttribute ( PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ );
$connecmysql->setAttribute ( PDO::ATTR_EMULATE_PREPARES, false );
$connecmysql->setAttribute ( PDO::ATTR_CASE, PDO::CASE_LOWER );


$fields = "txtStateFrom,txtStateTo,txtDate,txtActionShort,txtParent";
$select = ' SELECT '.$fields .' FROM frmHistoryLog';

$insert = 'INSERT INTO historytest ('.$fields.') values(?,?,?,?,?) ';

$prepare = $connecmysql->prepare($insert);

$result = $connOdbc->query($select);
if($result === false){
        $err = $result->errorInfo();
        throw new Exception($err[2]);
}

while($data = $result->fetch()){
        $prepare->bindValue(1,$data->txtstatefrom);
        $prepare->bindValue(2,$data->txtstattto);
        $prepare->bindValue(3,$data->txtdate);
        $prepare->bindValue(4,$data->txtactionshort);
        $prepare->bindValue(5,$data->txtparent);
        $prepare->execute();
}
$result->closeCursor();





//Afficher le temps d'éxecution
            $page_load_time = number_format($time, 3);
            echo "Debut du script: ".date("H:i:s", $timestart);
            echo "<br>Fin du script: ".date("H:i:s", $timeend);
            echo "<br>Script execute en " . $page_load_time . " sec";




?>