如何使用mysqlimport命令从命令行将csv文件导入mysql,这样就不用进入mysql了。?
我正在尝试使用此答案here
从命令行将csv文件导入mysql
root@678cf3cd1587:/home# mysqlimport --columns='head -n 1 discounts.csv' --ignore-lines=1 temp discounts.csv -u root -p
Enter password:
mysqlimport: Error: 1064, You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '-n 1 discounts.csv)' at line 1, when using table: discounts
root@678cf3cd1587:/home#
但我不能让它工作,任何人都可以告诉我做错了吗?
这是我的文件的样子
root@678cf3cd1587:/var/lib/mysql-files# cat discounts.csv
id,title,expired_date,amount
1,"1Spring Break ",20140401,20
2,"2Spring Break ",20140401,20
3,"3Spring Break ",20140401,20
4,"3Spring Break ",20140401,20
5,"3Spring Break ",20140401,20
6,"3Spring Break ",20140401,20
7,"3Spring Break ",20140401,20
这就是我的mysql的样子:
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
| temp |
+--------------------+
5 rows in set (0.00 sec)
mysql> use temp
Database changed
mysql> show tables;
Empty set (0.00 sec)
mysql>
注意我在docker容器中运行mysql,如果需要,我可以提供更多详细信息 这个问题有重复,但我认为我的具体是。
EDIT1
我曾尝试过不同的单引号“双引号”和反引号。虽然后面的勾号给出了不同的错误。这有帮助吗?
root@678cf3cd1587:/home# mysqlimport --columns='head -n 1 discounts.csv' --ignore-lines=1 temp discounts.csv -u root -p
Enter password:
mysqlimport: Error: 1064, You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '-n 1 discounts.csv)' at line 1, when using table: discounts
root@678cf3cd1587:/home# mysqlimport --columns="head -n 1 discounts.csv" --ignore-lines=1 temp discounts.csv -u root -p
Enter password:
mysqlimport: Error: 1064, You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '-n 1 discounts.csv)' at line 1, when using table: discounts
root@678cf3cd1587:/home# mysqlimport --columns=`head -n 1 discounts.csv` --ignore-lines=1 temp discounts.csv -u root -p
Enter password:
mysqlimport: Error: 1049 Unknown database 'date,amount'
EDIT2
如果我没有创建表格,我会Error: 1146, Table 'temp.discounts' doesn't exist, when using table: discounts
root@678cf3cd1587:/var/lib/mysql-files# mysqlimport temp /var/lib/mysql-files/discounts.csv -u root -p --columns=`head -n 1 /var/lib/mysql-files/discounts.csv` --ignore-lines=1
Enter password:
mysqlimport: Error: 1146, Table 'temp.discounts' doesn't exist, when using table: discounts
如果我然后创建表格,然后再试一次
mysql> CREATE TABLE discounts (
-> id INT NOT NULL AUTO_INCREMENT,
-> title VARCHAR(255) NOT NULL,
-> expired_date DATE NOT NULL,
-> amount DECIMAL(10,2) NULL,
-> PRIMARY KEY (id)
-> );
Query OK, 0 rows affected (0.03 sec)
mysql>
mysql>
mysql>
mysql> show tables;
+----------------+
| Tables_in_temp |
+----------------+
| discounts |
+----------------+
1 row in set (0.00 sec)
mysql> select * from discounts;
Empty set (0.00 sec)
我收到以下错误Error: 1265, Data truncated for column 'id' at row 1, when using table: discounts
root@678cf3cd1587:/var/lib/mysql-files# mysqlimport temp /var/lib/mysql-files/discounts.csv -u root -p --columns=`head -n 1 /var/lib/mysql-files/discounts.csv` --ignore-lines=1
Enter password:
mysqlimport: Error: 1265, Data truncated for column 'id' at row 1, when using table: discounts
root@678cf3cd1587:/var/lib/mysql-files#
我明确使用/var/lib/mysql-files/discounts.csv
因为,如果我理解正确,这是我可以使用secure_file_priv
mysql> SHOW VARIABLES LIKE "secure_file_priv";
+------------------+-----------------------+
| Variable_name | Value |
+------------------+-----------------------+
| secure_file_priv | /var/lib/mysql-files/ |
+------------------+-----------------------+
1 row in set (0.01 sec)
mysql>
EDIT3
我让这个工作
mysqlimport --columns="`head -n 1 /var/lib/mysql-files/discounts.csv`" --ignore-lines=1 temp /var/lib/mysql-files/discounts.csv -u root -p --fields-terminated-by=',' --fields-optionally-enclosed-by='"'
root@678cf3cd1587:/home# mysqlimport --columns="`head -n 1 /var/lib/mysql-files/discounts.csv`" --ignore-lines=1 temp /var/lib/mysql-files/discounts.csv -u root -p --fields-terminated-by=',' --fields-optionally-enclosed-by='"'
Enter password:
temp.discounts: Records: 7 Deleted: 0 Skipped: 0 Warnings: 0
root@678cf3cd1587:/home#
root@678cf3cd1587:/home# cat /var/lib/mysql-files/discounts.csv
id,title,expired_date,amount
1,"1Spring Break ",20140401,20
2,"2Spring Break ",20140401,20
3,"3Spring Break ",20140401,20
4,"3Spring Break ",20140401,20
5,"3Spring Break ",20140401,20
6,"3Spring Break ",20140401,20
7,"3Spring Break ",20140401,20
但是我必须先在我的sql中创建表,如上所述。
CREATE TABLE discounts (
id INT NOT NULL AUTO_INCREMENT,
title VARCHAR(255) NOT NULL,
expired_date DATE NOT NULL,
amount DECIMAL(10,2) NULL,
PRIMARY KEY (id)
);
我还必须使用此文件/var/lib/mysql-files/discounts.csv
,因为设置了secure_file_priv
,以便我只能从这个目录中加载文件,正如我所理解的那样。
mysql> SHOW VARIABLES LIKE "secure_file_priv";
+------------------+-----------------------+
| Variable_name | Value |
+------------------+-----------------------+
| secure_file_priv | /var/lib/mysql-files/ |
+------------------+-----------------------+
1 row in set (0.01 sec)
答案 0 :(得分:1)
看起来“过期日期”中的空间可能会使您绊倒。在命令行中,您需要引用--columns
参数,同时保留反引号。
mysqlimport --columns="`head -n 1 discounts.csv`" \
--ignore-lines=1 temp discounts.csv -u root -p
表格列名实际上是“过期日期”的空格吗?如果是这样,您需要在csv文件中使用反引号进行说明。
id,title,`expired date`,amount
另外,如果文件的格式与默认值
不同,请确保指定该文件的格式由'''''''''''''''''''''''''''''''''''''' 终止'\ n'开始''
在你的情况下,
--fields-terminated-by=',' --fields-optionally-enclosed-by='"'