MySQL Forums
Forum List  »  Other Migration

bulk import script for .csv files
Posted by: Justin Hibbard
Date: June 20, 2007 05:28PM

Anyone who can solve this problem will get a handy little script for bulk importing .csv files into MySQL. I'm almost there except for one detail.

I found this Windows batch script that, when run in the same directory as a bunch of .csv files, will generate an SQL script that lists LOAD DATA INFILE commands for all of the .csv files in the directory:

@echo off
echo TRUNCATE TABLE `CSVList`; > importcsv.sql
echo Creating list of MySQL import commands
for %%s in (*.csv) do echo LOAD DATA LOCAL INFILE '%%s' INTO TABLE `CSVList` FIELDS TERMINATED BY ',' ENCLOSED BY ' ' ESCAPED BY '/'LINES TERMINATED BY '\r\n' SET `CSVFilename`='%%s'; >> importcsv.sql;
echo Now run "C:\Program Files\MySQL\MySQL Server 5.0\bin\mysql" -u root -p [password] [dbname] < importcsv.sql

It works great as far as generating the list of LOAD DATA commands, which look like this:

LOAD DATA LOCAL INFILE 'A.csv' INTO TABLE `CSVList` FIELDS TERMINATED BY ',' ENCLOSED BY ' ' ESCAPED BY '/'LINES TERMINATED BY '\r\n' SET `CSVFilename`='A.csv'; ;

But when MySQL tries to execute the commands, it returns error 1054 "Unknown column 'CSVFilename' in 'field list'." It doesn't like something about the SET `CSVFilename`='%%s' command. Any ideas?

Justin

Options: ReplyQuote


Subject
Views
Written By
Posted
bulk import script for .csv files
21785
June 20, 2007 05:28PM
9517
August 13, 2007 09:16AM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.