|< Day Day Up >|
3.3 Using mysqlimport
The mysqlimport client program loads datafiles into tables. It provides a command-line interface to the LOAD DATA INFILE statement. That is, mysqlimport examines the options given on the command line and builds a LOAD DATA INFILE statement that corresponds to the actions specified by those options. It then connects to the server and, for each input file, issues the LOAD DATA INFILE statement that correctly loads the file into the appropriate table.
Because mysqlimport works this way, to use it most effectively, you should be familiar with the LOAD DATA INFILE statement. This section describes mysqlimport invocation syntax and how its options correspond to various clauses of the LOAD DATA INFILE statement. For a discussion specifically about LOAD DATA INFILE, you should also read Chapter 9, "Importing and Exporting Data." The details in that chapter aren't repeated here.
Invoke mysqlimport from the command line as follows:
shell> mysqlimport options db_name input_file
db_name names the database containing the table to be loaded and input_file names the file that contains the data to be loaded.
mysqlimport uses the filename to determine the name of the table into which the data should be loaded. The program does this by stripping off any filename extension (the last period and anything following it); the result is then used as the table name. For example, mysqlimport treats a file named City.txt or City.dat as input to be loaded into a table named City.
If you name multiple files on the command line after the database name, mysqlimport issues a LOAD DATA INFILE statement for each file.
Each table to be loaded by mysqlimport must already exist, and each input file should contain only data values. mysqlimport isn't intended for processing files that consist of SQL statements (such files can be created with the mysqldump program). For instructions on processing an SQL-format dump file produced by mysqldump that contains SQL statements, see section 3.4, "Using mysqldump."
The options part of the mysqlimport command may include any of the standard connection parameter options, such as --host or --user. You'll need to supply these options if the default connection parameters aren't appropriate. mysqlimport also understands options specific to its own operation. Invoke mysqlimport with the --help option to see a complete list of the options that can be used to tell mysqlimport the actions you want it to perform.
By default, input files for mysqlimport are assumed to contain lines terminated by newlines, with each line containing tab-separated data values. For an input file that's in a different format, use the following options to tell mysqlimport how to interpret the file:
The preceding options give you the flexibility to load input files containing data in a variety of formats. Some examples follow; each one loads an input file named City.txt into the City table in the world database. Commands that are shown on multiple lines should be entered on a single line.
shell> mysqlimport --lines-terminated-by="\r\n" world City.txt
Note that the --lines-terminated-by value is quoted with double quotes. Format option values often contain special characters, such as backslash, that might have special meaning to your command interpreter. It might be necessary to quote such characters to tell your command interpreter to pass them, unchanged, to mysqlimport.
The syntax for specifying a double quote is trickier and depends on which command interpreter you use. The following command loads a datafile containing values quoted by double quote characters:
shell> mysqlimport --fields-terminated-by='"' world City.txt
This command should work on most Unix shells, which allow the double quote character to be quoted within single quotes. This doesn't work on Windows, where you must specify a double quote within a double-quoted string by escaping it:
shell> mysqlimport --fields-terminated-by="\"" world City.txt
The following command loads a file that has data values separated by commas, and lines ending with carriage returns:
shell> mysqlimport --fields-terminated-by=, --lines-terminated-by="\r" world City.txt
|< Day Day Up >|