[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: MySQL and loading tables from a file.



Greetings Mike,

If I was in your shoes I would write a quick perl script to generate
SQL statements to insert this data into the proper table. The script
might look something like this...

#/usr/bin/perl

open INPUT, "<inputfile.csv";
open OUTPUT, ">outputfile.sql";

if($ARGV[0] eq "") {
  print "usage: buildsql.pl <table to insert into>\n\n";
}

while(<INPUT>) {

  chomp $_;
  @fields = split(/,/, $_);   # I don't think I need to escape the , here.
   print OUTPUT "insert into $ARGV[0] values (";
   foreach $item (@fields) {
      print OUTPUT "'".$item."',"; # A better solution would be to use
  }                                                        # the DBI quote
function here.
  print OUTPUT "\n";
}

Just my 2 cents.

Jason Burke

Michael David wrote:

> I'm currently looking into loading a comma-delimited file (generated fron an
> Excel spreadsheet) into a MySQL database.
>
> LOAD DATA INFILE is the current item I'm looking at, and it does pretty much
> everything I want, however, it only snart enough (or so the documentation has
> me believing) to load that data into a single table.
>
> My conundrum is that the source file only has the information I would like to
> use for perhaps 5 fields of one table, and 3 fields of another.
>
> Does anyone know the best way to tell MySQL to split this up, or am I going to
> have to split this up using external tools?
>
> TIA,
>
> Mike
> -
> To unsubscribe, send email to majordomo@luci.org with
> "unsubscribe luci-discuss" in the body.

-
To unsubscribe, send email to majordomo@luci.org with
"unsubscribe luci-discuss" in the body.