in reply to Creating Table and Inserting Data from CSV
(Well, maybe the field names might help for figuring what what data types to use, and/or maybe you can do some heuristics, reading the CSV file once in advance to guess at appropriate data types for some fields, but maybe this isn't an issue for you anyway.)
Since you're looking at Text::CSV_XS, maybe something like this would get you started:
use strict; use Text::CSV_XS; use DBI; my $db = DBI->connect( ... ); $db->do( "drop table if exists new_table" ); my $csv = Text::CSV_XS->new(); open( my $infile, "filename.csv" ) or die "filename.csv: $!"; # get CSV header my $hdr = $csv->getline( $infile ); my $create = "create table new_table (" . join( " varchar(255),", @$hdr ) . " varchar(255))"; $db->do( $create ); # ... at this point, you could prepare an insert statement # for mysql, loop over $csv->getline() and execute the insert # for each row. # # but if you use LOAD DATA INFILE instead, it'll be much faster ...
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Creating Table and Inserting Data from CSV
by awohld (Hermit) on Oct 05, 2005 at 01:55 UTC | |
|
Re^2: Creating Table and Inserting Data from CSV
by a6april (Initiate) on Nov 08, 2012 at 14:17 UTC |