Skip to main content
Version: V1.4.0

Import data

seekdb V1.4.0 supports LOAD DATA INFILE for importing server-side files and LOAD DATA LOCAL INFILE for importing files from a client. This topic demonstrates both methods with a CSV file.

Limitations​

  • The target table cannot have an INSERT or UPDATE trigger.
  • Server-side import requires the FILE privilege and the INSERT privilege on the target table.
  • Client-side import requires the INSERT privilege on the target table, and the client must support and enable local file loading.
  • Server-side file paths are restricted by the secure_file_priv system variable.

Prepare data and the target table​

Prepare a student.csv file:

1,lin,98
2,hei,90
3,ali,95

Create the target table:

CREATE DATABASE IF NOT EXISTS test;
USE test;
CREATE TABLE student (
id INT PRIMARY KEY,
name VARCHAR(50),
score INT
);

Import a server-side file​

For a server-side import, the seekdb process reads the file directly.

  1. Place student.csv in a secure directory on the seekdb server, such as /data/import/, and make sure that the seekdb process can read it.

  2. Connect through the local socket and set the secure directory. Reconnect after changing the global variable:

    SET GLOBAL secure_file_priv = '/data/import';
  3. After reconnecting, import the file:

    LOAD DATA INFILE '/data/import/student.csv'
    INTO TABLE student
    CHARACTER SET utf8mb4
    FIELDS TERMINATED BY ','
    LINES TERMINATED BY '\n';
  4. Verify the result:

    SELECT * FROM student ORDER BY id;

Import a client-side file​

For a client-side import, the client reads the file and sends it to seekdb. Use this method when the database server cannot access the file directly.

  1. Start a client with local file loading enabled. For example, with the MySQL client:

    mysql --local-infile=1 -h127.0.0.1 -P2881 -uroot -p -Dtest
  2. Import the file:

    LOAD DATA LOCAL INFILE '/home/user/student.csv'
    INTO TABLE student
    CHARACTER SET utf8mb4
    FIELDS TERMINATED BY ','
    LINES TERMINATED BY '\n';
  3. Verify the result:

    SELECT * FROM student ORDER BY id;
info

LOAD DATA LOCAL INFILE handles duplicate keys with IGNORE semantics. To replace existing rows with rows from the file, use server-side import and explicitly specify REPLACE.

Import a file with a header row​

If the first row of a CSV file contains column names, skip it by specifying IGNORE 1 LINES:

LOAD DATA LOCAL INFILE '/home/user/student-with-header.csv'
INTO TABLE student
FIELDS TERMINATED BY ','
OPTIONALLY ENCLOSED BY '"'
LINES TERMINATED BY '\n'
IGNORE 1 LINES;

For the complete syntax and error-file options, see LOAD DATA.

tip

LOAD DATA imports a text file into a table; it does not migrate an entire embedded instance. To perform a logical upgrade of embedded seekdb, see Upgrade embedded seekdb.