CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
Search Posts SEARCH
Who's Online WHO'S
Log in LOG

Home: Need a Custom or Prewritten Perl Program?: Throw Down The Gauntlet:
remap MySQL fields



Jan 25, 2001, 11:11 AM

Post #1 of 2 (7029 views)
remap MySQL fields Can't Post

Is there a script that can support remapping the fields of a db table(mine is from a CSV file) to an existing MySQL db table?


RFQ Project Developer

User / Moderator

Jan 27, 2001, 3:39 PM

Post #2 of 2 (7024 views)
Re: remap MySQL fields [In reply to] Can't Post

This is trivial to do using, DBD::CSV and DBD::mysql to read from one and insert into the other.

Something like ...

$read  = $csv->prepare("SELECT id,name FROM table"); 
$write = $mysql->prepare("INSERT INTO table ( no,name ) VALUES ( ?,? )");

# now loop over each ...

while ( my $row = $read->fetchrow_arrayref ) {
$write->execute( @$row );

... where $csv and $mysql are DBI handles to the respective databases.

Remapping fields is then as simple as making sure whatever you SELECT has the same order as what you INSERT (ie, the above inserts data from the 'id' column into 'no').

Alternately, if your CSV is very simple, there's an example in the DBI docs themselves of reading from CSV and inserting into a db.


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives