背景描述:
使用MySQL 8.0 Command Line Cline 命令行将CSV文件导入table中,win10系统。
方式:
1、找到mysql中欲导入数据的table
mysql> SHOW databases;
+--------------------+
| Database |
+--------------------+
| data |
| information_schema |
| mysql |
| performance_schema |
| sakila |
| sys |
| world |
+--------------------+
7 rows in set (0.00 sec)
mysql>USE data;
Database changed
mysql> SHOW tables;
+----------------+
| Tables_in_data |
+----------------+
| company |
| dataanalyst |
| orderinfo |
| userinfo |
+----------------+
4 rows in set (0.00 sec)
2、找到secure_file_priv文件夹(因为这事安全文件夹,所以只有这个文件夹中的文件才能上传)
mysql> SHOW variables like '%secure%' ;
+--------------------------+------------------------------------------------+
| Variable_name | Value |
+--------------------------+------------------------------------------------+
| require_secure_transport | OFF |
| secure_file_priv | C:ProgramDataMySQLMySQL Server 8.0Uploads |
+--------------------------+------------------------------------------------+
2 rows in set, 1 warning (0.00 sec)
3、将csv文件放入secure_file_priv 所指向的文件夹
(以我为例,需要将csv文件放入C:ProgramDataMySQLMySQL Server 8.0Uploads中)
4、执行导入命令
mysql> load data infile 'C:/ProgramData/MySQL/MySQL Server 8.0/Uploads/user_info_utf.csv'
-> into table userinfo
-> fields terminated by ',' ;
Query OK, 32079 rows affected (0.26 sec)
Records: 32079 Deleted: 0 Skipped: 0 Warnings: 0
5、成功。
补充说明:
在第4步中,对于MySQL 8.0的版本,会出现报错(报错原因比较有趣):
Incorrect integer value ” for column ‘name’ at row 1
报错原因:
In this situation, what is happening is that the script is attempting to insert or update a row with the integer entry taking on either the default value (e.g., of 0
or NULL
) or the next AUTO_INCREMENT
value.
But MySQL is not interpreting this action as valid due to its SQL_MODE being in STRICT
mode.
解决方法:
In this case, to not treat this action as an error, edit my.ini, find this line –
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
And set to to an empty string –
sql_mode=""
Save my.ini, restart MySQL.
some other points.
- Where is your my.ini?
You should find where your sql install in.
2. How to restart MySQL?
这里有一种方法
参考资料:
windows下重启mysql的方法
解决Incorrect integer value: '' for column 'id' at row 1的方法
MySQL Error “Incorrect integer value ” for column ‘name’ at row 1″
微信公众号:
简书:https://www.jianshu.com/u/505e89457641
知乎:https://www.zhihu.com/people/zhao-yue-62-24/posts