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.

In reply to Re: Creating Table and Inserting Data from CSV by xorl
in thread Creating Table and Inserting Data from CSV by awohld

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.