1

I am required to write a batch program to load csv files in MySQL. I am aware of the LOAD DATA INFILE syntax that makes the bulk insert possibe by making use of CSV files.

In the batch I would also have to run the file browser in the sequential order perhaps from the command line; I would also ask for the table name for placeholding the table name. So in other word, I would want to write a generic script which takes user input for table name and asks for the file from command line.

Hence the question, is this automating of load program even possible? Sorry could not find much on this top while searching around.

I hope my question was clear and coherent.

yoda
  • 539
  • 1
  • 12
  • 32

2 Answers2

1

Check this out:

Pass parameter to MySQL script command line

Replacing table name and file name with shell script parameter, remember to check their safety before doing string replacement.

Community
  • 1
  • 1
1

Check out mysqlimport. It's the command-line interface to the LOAD DATA INFILE statement.

The restriction is that the name of the input csv file must match the name of the table to which you want to import the data. The manual states:

For each text file named on the command line, mysqlimport strips any extension from the file name and uses the result to determine the name of the table into which to import the file's contents. For example, files named patient.txt, patient.text, and patient all would be imported into a table named patient.

But you can work around that restriction in one of two ways:

  • Rename the input file in your script, to match the destination table name.
  • Import to a new table matching the filename, then RENAME TABLE.
Bill Karwin
  • 538,548
  • 86
  • 673
  • 828