Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re: Creating Table and Inserting Data from CSV

by graff (Chancellor)
on Oct 04, 2005 at 22:39 UTC ( #497426=note: print w/ replies, xml ) Need Help??


in reply to Creating Table and Inserting Data from CSV

Since your daily CSV file will only provide column headings (that is, field names), and not data types (integer vs. string vs. date, or maximum field width for strings), your main issue will be having to set up a "CREATE TABLE" statement that handles the longest string that might occur in the daily CSV file, and having to give up on using appropriate data types -- every field will have to be "varchar" (which means you can't make use of type-specific functions in mysql for things like dates and numbers when you query this table).

(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 ...


Comment on Re: Creating Table and Inserting Data from CSV
Download Code
Re^2: Creating Table and Inserting Data from CSV
by awohld (Hermit) on Oct 05, 2005 at 01:55 UTC
    Awesome, that's great! That's the track I was on but didn't quite get the "join" correct.
Re^2: Creating Table and Inserting Data from CSV
by a6april (Initiate) on Nov 08, 2012 at 14:17 UTC
    This is really cool, How do I add the browse button for something like this? I tried a few things but can't figure it out???
    //get the csv file $file = $_FILES[csv][tmp_name]; $handle = fopen($file,"r");
    The HTML
    <form action="" method="post" enctype="multipart/form-data" name="form +1" id="form1"> Choose your file: <br /> <input name="csv" type="file" id="csv" /> <input type="submit" name="Submit" value="Submit" /> </form>

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://497426]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (6)
As of 2014-09-02 06:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite cookbook is:










    Results (20 votes), past polls