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

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

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

 



dalehend
stranger

Jan 25, 2001, 11:11 AM

Post #1 of 2 (3250 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?

dale
dalehend@projectperl.com

RFQ Project Developer


Kanji
User / Moderator

Jan 27, 2001, 3:39 PM

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

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

Something like ...


Code
$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