Import data
seekdb 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
INSERTorUPDATEtrigger. - Server-side import requires the
FILEprivilege and theINSERTprivilege on the target table. - Client-side import requires the
INSERTprivilege on the target table, and the client must support and enable local file loading. - Server-side file paths are restricted by the
secure_file_privsystem 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.
-
Place
student.csvin a secure directory on the seekdb server, such as/data/import/, and make sure that the seekdb process can read it. -
Connect through the local socket and set the secure directory. Reconnect after changing the global variable:
SET GLOBAL secure_file_priv = '/data/import'; -
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'; -
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.
-
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 -
Import the file:
LOAD DATA LOCAL INFILE '/home/user/student.csv'
INTO TABLE student
CHARACTER SET utf8mb4
FIELDS TERMINATED BY ','
LINES TERMINATED BY '\n'; -
Verify the result:
SELECT * FROM student ORDER BY id;
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.
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.