十进制(整数)值不正确:“ ” mySQL

Incorrect decimal (integer) value: ' ' mySQL

在mySQLworkbench数据库中,其中一个table有纬度、经度和地区属性

lat: 十进制 (10,8)

lng: 十进制 (11,8)

地区:int(4)

我正在尝试将数据从 .csv 文件导入到此 table

ERROR 1366: 1366: Incorrect decimal value: '' for column 'lat' at row 1
SQL Statement:
ERROR 1366: 1366: Incorrect integer value: '' for column 'district' at row 1
SQL Statement:
INSERT INTO `db`.`myTable` (`id`, `name_en`, `icon`, `lat`, `lng`, `district`, `city`, `postal_code`)
    VALUES ('686', 'Name',?, '', '', '','1', 'P.O. Box 1111')

您启用了严格的 sql 模式,并且您尝试传递一个空字符串 ('') 作为插入中小数字段的值。空字符串是数字字段的无效值,在 strict sql mode mysql 中,如果您尝试将无效数据插入列,而不是提供警告并使用默认值(0特定列的数据类型的数字列:

Strict mode controls how MySQL handles invalid or missing values in data-change statements such as INSERT or UPDATE. A value can be invalid for several reasons. For example, it might have the wrong data type for the column, or it might be out of range. A value is missing when a new row to be inserted does not contain a value for a non-NULL column that has no explicit DEFAULT clause in its definition. (For a NULL column, NULL is inserted if the value is missing.) Strict mode also affects DDL statements such as CREATE TABLE.

If strict mode is not in effect, MySQL inserts adjusted values for invalid or missing values and produces warnings (see Section 13.7.5.40, “SHOW WARNINGS Syntax”). In strict mode, you can produce this behavior by using INSERT IGNORE or UPDATE IGNORE.

在开始导入之前删除会话的严格 sql 模式:

SET SESSION sql_mode = ''

global sql_mode 中删除 STRICT_TRANS_TABLE。如果已经设置,您可能需要其他属性,例如全局 sql_mode 中的 NO_ZERO_DATE

根据您的服务器版本,在 my.cnf(或 /etc/mysql/mysql.conf.d/mysqld.cnf)中的 [mysqld] 下进行设置。

[mysqld]  
sql_mode = "NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION"

我所做的只是将我的查询从 INSERT INTO... 更改为 INSERT IGNORE INTO...,效果很好。