Syst has asked for the wisdom of the Perl Monks concerning the following question:

I am new to perl asking for Compassion from the monks. Assume that I have an array of tables and that one db has all the data in it and another db just has structure in it. One db is ingres, the other db is mysql. The tables are the same on both. Is there a loop or other easy way using dbi to copy the tables over, or do I need to write a loop for each table to create the insert queries to write over.
ex
### Table 1 $sth = $dbh->prepare("select * from master_table_1"); while (@row=$sth->fetchrow) { $query = "insert into back_table_2 (f1,f2,f3) VALUES ('@row[0]','@row[ +1]','@row[2]')" } ### Table 2 ### Table 1 $sth = $dbh->prepare("select * from master_table_2"); while (@row=$sth->fetchrow) { $query = "insert into back_table_2 (abc1,abc2,abc3) VALUES ('@row[0]', +'@row[1]','@row[2]')" }
Does this make sense, is there any easy way to do this? Please excuse my lack of english.
I was hoping to do this with loops and not modules.

Replies are listed 'Best First'.
Re: Copy tables from one db to another
by jZed (Prior) on Jan 19, 2005 at 23:31 UTC
    You might want to investige import and export capabilities of your DBMSs for example, if Ingres can dump to CSV, MySQL can easily import it with the LOAD command.

    If you go the manual method, you can use something like this:

    my $ingres_dbh = DBI->connect( @ingres_connection_args ); my $mysql_dbh = DBI->connect( @mysql_connection_args ); my @sql = ( { select => 'SELECT f1,f2,f3 FROM master_table_1' , insert => 'INSERT INTO back_table_2(f1,f2,f3) VALUES(?,?,?)' } , { select => 'SELECT abc1,abc2,abc3 FROM master_table_2' , insert => 'INSERT INTO back_table_2(abc1,abc2,abc3) VALUES(?,?,? +)' } ); for( 0..1 ){ my $select = $ingres_dbh->prepare( $sql[$_]->{select} ); my $insert = $mysql_dbh->prepare( $sql[$_]->{insert} );, while(my $row = $select->fetch){ $insert->execute(@$row); } }
Re: Copy tables from one db to another
by Errto (Vicar) on Jan 19, 2005 at 23:39 UTC

    If you're doing a one-time transfer between two databases, it sounds to me like Perl might not be a great choice. What I would do is find out how to use the bulk loading facility in whatever your target database is. Then write a query to extract the data from the first database and save it to a file in the correct format for the second databases' bulk load program. Actually, for this purpose Perl might be a good choice. If you are writing a text file, a module like Text::Template or Text::CSV depending on how fancy it is.

    If you do decide to go the straight Perl way, you should at a minimum use pleaceholders for the insertion step. Something like

    my $sth_insert = $dbh2->prepare("insert into back_table_2 (abc1,abc2,a +bc3) VALUES (?, ?, ?) "); while ($row = $sth->fetchrow_arrayref) { $sth_insert->execute(@$row); }
    and you might want to do a periodic commit as well.

Re: Copy tables from one db to another
by borisz (Canon) on Jan 19, 2005 at 23:07 UTC
Re: Copy tables from one db to another
by jplindstrom (Monsignor) on Jan 20, 2005 at 18:04 UTC