Previous Section  < Day Day Up >  Next Section

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:

  • --lines-terminated-by=string

    string specifies the character sequence that each input line ends with. The default is \n (linefeed, also known as newline); other common line terminators are \r (carriage return) and \r\n (carriage return/linefeed pairs).

  • --fields-terminated-by=string

    string specifies the delimiter between data values within input lines. The default delimiter is \t (tab).

  • --fields-enclosed-by=char or --fields-optionally-enclosed-by=char

    char indicates a quote character that surrounds data values in the file. By default, values are assumed not to be quoted. Use this option if values are quoted. A common value for char is the double quote character ("). If quote characters enclose a data value, they're removed before the value is loaded into the table.

  • --fields-escaped-by=char

    By default, \within the input is taken as an escape character that signifies a special sequence. For example, if the \N sequence occurs alone in a field, it's interpreted as a NULL value. Use this option to specify a different escape character. To turn escaping off (no escape character), specify an empty value for char.

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.

The following command loads a file that has lines ending in carriage return/linefeed pairs:






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


Other mysqlimport options provide additional control over datafile loading. The following list discusses some of those you're likely to find useful:

  • --ignore or --replace

    These options tell mysqlimport how to handle input records that contain unique key values that are already present in the table. Such records result in duplicate-key errors and cannot be loaded by default. --ignore causes duplicates in the input file to be ignored. --replace causes existing records in the table to be replaced by duplicates in the input file.

  • --local

    By default, a datafile to be loaded into a table is assumed to reside on the server host, allowing the server to read the file directly. This is very efficient, but requires the user running mysqlimport to have the FILE privilege (a powerful privilege normally reserved for administrators). The --local option allows use of a datafile that's located locally on the client host where mysqlimport is invoked. With --local, mysqlimport reads the datafile and sends it over the network to the server. This allows mysqlimport to read any file on the client host to which the invoker has access, without requiring the invoker to have the FILE privilege. For the --local option to work, the server must be configured to allow local files to be transferred to it.

    Previous Section  < Day Day Up >  Next Section