Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"

Creating Table and Inserting Data from CSV

by awohld (Hermit)
on Oct 04, 2005 at 18:50 UTC ( #497357=perlquestion: print w/replies, xml ) Need Help??
awohld has asked for the wisdom of the Perl Monks concerning the following question:

I have a CSV data file that I get every day where the column headings can change without notice.

I want to delete my old table and create a new table every day that has the same column names as my CSV file.

Are there any modules or code snippets that can CREATE a MySQL table from the the CSV file formant?

Right now I'm looking at using Text::CSV_XS to get the column names for the CREATE TABLE statement, but I'm having trouble building the SQL statment dynamically from the file. Any help is apprecieated.
  • Comment on Creating Table and Inserting Data from CSV

Replies are listed 'Best First'.
Re: Creating Table and Inserting Data from CSV
by xorl (Deacon) on Oct 04, 2005 at 19:11 UTC
    I'd suggest checking out Text::xSV. It is really cool. If the col names are in the file it can easily figure that out. If not, you just have to tell it the order of the col names. As for creating a table in MySQL from the CSV... there are a couple of way you can do it. What I'd do is use Text::xSV to get the column names, loop through that and write the SQL. Something like:
    use Text::xSV; my $csv = new Text::xSV; $csv->open_file("foo.csv"); $csv->read_header(); my $SQL = "CREATE TABLE $table_name("; foreach my $field ($csv->get_fields) { $SQL .= $field . " varchar(255),"; } $SQL .= "); # then connect to the database and run the query
    Also in MySQL 4, it now supports a CSV engine. If you have that enabled, you might be able to just write your file into the mysql data dir and have it work. I haven't tried it, so I won't recommend it. One more thing, I'd suggest using the SQL load data infile or the commandline mysqlimport to actually import the data.
      I know this may seem trivial for my app, but using the "get_fields" method returns the fields in random order, is there another similar module that will return them in order? I read Text::xSV on CPAN and I didn't see that it could do that, or from at least what I understood.
        will regular split not work for this file? If it will then you can try something like this -

        #!/usr/bin/perl -w use strict; use DBI; my $line = "var1, var2, var3"; my $dbh = DBI->connect('DBI:mysql:test') or die "Couldn't connect to d +atabase: " . DBI->errstr; my $create = "create table dummy3 (" . join (',', map { $_ . ' varchar +(255) ' } split /,/, $line) . ");"; my $sth = $dbh->prepare($create) or die "Couldn't prepare statement: " + . $dbh->errstr; $sth->execute or die "Couldn't execute statement: " . $sth->errstr;;
        mysql> desc dummy3 -> ; +-------+--------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+--------------+------+-----+---------+-------+ | var1 | varchar(255) | YES | | NULL | | | var2 | varchar(255) | YES | | NULL | | | var3 | varchar(255) | YES | | NULL | | +-------+--------------+------+-----+---------+-------+ 3 rows in set (0.00 sec)
Re: Creating Table and Inserting Data from CSV
by graff (Chancellor) on Oct 04, 2005 at 22:39 UTC
    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 ...
      Awesome, that's great! That's the track I was on but didn't quite get the "join" correct.
      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>
Re: Creating Table and Inserting Data from CSV
by sk (Curate) on Oct 04, 2005 at 19:14 UTC
    Thanks xorl! Sorry awolhd, I should have read it carefully.

    NOTE: non-Perl solution

    Wouldn't this do?


    you need to drop your previous table


      That would work fine if the table alreay existed. I think he wants to create a table first. In which case you somehow have to figure out the create table statement first. Once the table is created, like I said above, we both agree he should use load data infile

      I'd like to know exactly what he is doing. It seems odd that he'd have a csv file where the column order and column names changes randomly.

        It is odd, but I'm taking a dump of settings that change regularly with new software loads.
      When I try that it says the table doesn't exist, I want the program to create the table every time. As far as my understanding you need an existing table to do LOAD DATA INFILE.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://497357]
Approved by ikegami
[erix]: o/
[erix]: I'm a bit stuck with regex capturing. Anyone wants to ahve a look at my [erix://pad | scratchpad ? (it's the little test program at the top there)]
[erix]: hm [erix://pad]
[erix]: pad::/erix
[erix]: erix's scratchpad
[erix]: ah! ;)

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (6)
As of 2018-01-19 08:05 GMT
Find Nodes?
    Voting Booth?
    How did you see in the new year?

    Results (216 votes). Check out past polls.