This is more of an MySQL question than a Perl question, but here goes:

A possible error is that you are inserting into the same table you are selecting from.

> my $sth = $dbh->prepare( "SELECT * FROM board where serial='CN+/P4226666'" ) ||
> my $sth2 = $dbh->prepare("INSERT INTO board @abfrage");

As far as the easiest way to do this, you can use SQL:

INSERT INTO table2 SELECT * FROM table1 WHERE table1.col='value'

The SELECT query must return the exact number of items to be INSERT'ed, so in this 
case since you are selecting everything from table1 (board), table2 and table1 must 
have the same number of fields and in the same order. Also, you cannot SELECT FROM the 
table you are INSERT'ing INTO.

Help for this syntax is at:
http://www.mysql.com/documentation/mysql/bychapter/manual_Reference.html#INSERT_SELECT

General MySQL help is at:
http://www.mysql.com/documentation/mysql/bychapter/manual_toc.html

=-= Robert T.


On Wed, Jun 26, 2002 at 02:59:34PM +0200, Theuerkorn Johannes wrote:
> Hi there, again one (probably simple) question:
> 
> I have two databases and want to put the data from one table into the same table in 
>the other database...
> Doing it like follows up gives me the right values, (like i can see from the later 
>Output) but doesnt write into the second table... Any suggestions? Its Probably a 
>problem from the array i guess...
> 
> Greets Johannes
> 
> #/usr/lib/perl -w
> #
> #
> #Script um Teres Datenbank auszulesen un Daten in Direx zu schreiben
> #
> 
> 
> # Compilerpragma strict um keine Fehler zu machen :-)
> use strict;
> # Benutzung der Perl-Module CGI und DBI
> use CGI;
> use DBI;
> 
> my $user = "root";
> my $pwd = "";
> 
> # connect to databases
> my $dbh = DBI->connect( 'dbi:mysql:DiREx',$user,$pwd)||
>      die "Kann keine Verbindung zum MySQL-Server aufbauen: $DBI::errstr\n";
> my $dbh2 = DBI->connect ( 'dbi:mysql:dirextest',$user,$pwd)||
>       die "Kann keine Verbindung zum MySQL-Server aufbauen: $DBI::errstr\n";
> 
> # prepare sql query
> # Handle Objekt wird zurueckgeliefert
> 
>      die "Kann Statement nicht vorbereiten: $DBI::errstr\n";
> 
> # execute query
> $sth->execute ||
>      die "Kann Abfrage nicht ausfuehren: $DBI::errstr\n";
> 
> #put values into an array
> my @abfrage = $sth->fetchrow_array;
> 
> #prepare insert of values from the array in second database
> 
> 
> #execute insert
> $sth->execute||
>               die "Kann Abfrage nicht ausfuehren: $DBI::errstr\n";
>               
> while ( my @ergebnis = $sth->fetchrow_array ){
>    # Im Array @ergebnis steht nun ein Datensatz
>    #print "<LI>".$ergebnis[1]." ".$ergebnis[2]." ".$ergebnis[3]." ".$ergebnis[4]. 
>"\n";
>       print "@ergebnis\n";
> }
> 
> -- 
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to