错误代码:1366。不正确的整数值:第 2 行的列“ReportsTo”的“NULL”

Posted

技术标签:

【中文标题】错误代码:1366。不正确的整数值:第 2 行的列“ReportsTo”的“NULL”【英文标题】:Error Code: 1366. Incorrect integer value: 'NULL' for column 'ReportsTo' at row 2 【发布时间】:2021-03-08 12:16:37 【问题描述】:

在我从 .csv 文件加载数据时创建表后,它显示错误:

“错误代码:1366。不正确的整数值:列的'NULL' 'ReportsTo' 在第 2 行"。

我的 csv 文件在第 2 行有空值。我已经搜索了互联网,但我没有找到解决方案。我正在使用北风数据。

| EmployeeID | LastName  | FirstName | Title                    | TitleOfCourtesy | BirthDate | HireDate | Address                      | City     | Region | PostalCode | Country | HomePhone      | Extension | Notes                                                                                                                                                                                                                                                                  | ReportsTo | PhotoPath                              | Salary |
|------------|-----------|-----------|--------------------------|-----------------|-----------|----------|------------------------------|----------|--------|------------|---------|----------------|-----------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|-----------|----------------------------------------|--------|
| 1          | Davolio   | Nancy     | Sales Representative     | Ms.             | 00:00.0   | 00:00.0  | 507 - 20th Ave. E.Apt. 2A    | Seattle  | WA     | 98122      | USA     | (206) 555-9857 | 5467      | Education includes a BA in psychology from Colorado State University in   1970.  She also completed "The Art   of the Cold Call."  Nancy is a   member of Toastmasters International.                                                                                  | 2         | http://accweb/emmployees/davolio.bmp   | 120000 |
| 2          | Fuller    | Andrew    | Vice President Sales     | Dr.             | 00:00.0   | 00:00.0  | 908 W. Capital Way           | Tacoma   | WA     | 98401      | USA     | (206) 555-9482 | 3457      | Andrew received his BTS commercial in 1974 and a Ph.D. in international   marketing from the University of Dallas in 1981.  He is fluent in French and Italian and   reads German.  He joined the company as   a sales representative was promoted to sales manager i  | NULL      | http://accweb/emmployees/fuller.bmp    | 200000 |
| 3          | Leverling | Janet     | Sales Representative     | Ms.             | 00:00.0   | 00:00.0  | 722 Moss Bay Blvd.           | Kirkland | WA     | 98033      | USA     | (206) 555-3412 | 3355      | Janet has a BS degree in chemistry from Boston College (1984).  She has also completed a certificate   program in food retailing management.    Janet was hired as a sales associate in 1991 and promoted to sales   representative in February 1992.                  | 2         | http://accweb/emmployees/leverling.bmp | 125000 |
| 4          | Peacock   | Margaret  | Sales Representative     | Mrs.            | 00:00.0   | 00:00.0  | 4110 Old Redmond Rd.         | Redmond  | WA     | 98052      | USA     | (206) 555-8122 | 5176      | Margaret holds a BA in English literature from Concordia College (1958)   and an MA from the American Institute of Culinary Arts (1966).  She was assigned to the London office   temporarily from July through November 1992.                                         | 2         | http://accweb/emmployees/peacock.bmp   | 100000 |
| 5          | Buchanan  | Steven    | Sales Manager            | Mr.             | 00:00.0   | 00:00.0  | 14 Garrett Hill              | London   | NULL   | SW1 8JR    | UK      | (71) 555-4848  | 3453      | Steven Buchanan graduated from St. Andrews University Scotland with a BSC   degree in 1976.  Upon joining the   company as a sales representative in 1992. he spent 6 months in an   orientation program at the Seattle office and then returned to his permanent   po | 2         | http://accweb/emmployees/buchanan.bmp  | 100000 |
| 6          | Suyama    | Michael   | Sales Representative     | Mr.             | 00:00.0   | 00:00.0  | Coventry HouseMiner Rd.      | London   | NULL   | EC2 7JR    | UK      | (71) 555-7773  | 428       | Michael is a graduate of Sussex University (MA economics 1983) and the   University of California at Los Angeles (MBA marketing 1986).  He has also taken the courses   "Multi-Cultural Selling" and "Time Management for the Sales   Professional."  He is fluent     | 5         | http://accweb/emmployees/davolio.bmp   | 140000 |
| 7          | King      | Robert    | Sales Representative     | Mr.             | 00:00.0   | 00:00.0  | Edgeham HollowWinchester Way | London   | NULL   | RG1 9SP    | UK      | (71) 555-5598  | 465       | Robert King served in the Peace Corps and traveled extensively before   completing his degree in English at the University of Michigan in 1992 the   year he joined the company.  After   completing a course entitled "Selling in Europe" he was transferred   to the | 5         | http://accweb/emmployees/davolio.bmp   | 240000 |
| 8          | Callahan  | Laura     | Inside Sales Coordinator | Ms.             | 00:00.0   | 00:00.0  | 4726 - 11th Ave. N.E.        | Seattle  | WA     | 98105      | USA     | (206) 555-1189 | 2344      | Laura received a BA in psychology from the University of Washington.  She has also completed a course in business   French.  She reads and writes French.                                                                                                              | 2         | http://accweb/emmployees/davolio.bmp   | 300000 |
| 9          | Dodsworth | Anne      | Sales Representative     | Ms.             | 00:00.0   | 00:00.0  | 7 Houndstooth Rd.            | London   | NULL   | WG2 7LT    | UK      | (71) 555-4444  | 452       | Anne has a BA degree in English from St. Lawrence College.  She is fluent in French and German.                                                                                                                                                                        | 5         | http://accweb/emmployees/davolio.bmp   | 220000 |
CREATE TABLE Employees (
            EmployeeID INT(11),
            LastName VARCHAR(20),
            FirstName VARCHAR(10),
            Title VARCHAR(30),
            TitleOfCourtesy VARCHAR(25),
            BirthDate datetime,
            HireDate datetime,
            Address varchar(60),
            City varchar(15),
            Region varchar(15),
            PostalCode varchar(10),
            Country varchar(15),
            HomePhone varchar(24),
            Extension varchar(4),
            Notes MEDIUMTEXT,
            ReportsTo INT(11) NULL,
            PhotoPath VARCHAR(255),
            Salary FLOAT,
            PRIMARY KEY (EmployeeID)
);

LOAD DATA INFILE 'C:/ProgramData/mysql/MySQL Server 8.0/Uploads/employees.csv'
INTO TABLE EmployeeS
FIELDS TERMINATED BY ','
IGNORE 1 ROWS;

【问题讨论】:

你能告诉我们那一排吗? 我一直发现将 CSV 数据批量加载到所有字段都是可为空的 VARCHAR 的表中作为暂存,然后将其作为步骤 2 插入到真实表中更容易,而不是尝试在批量加载过程中与垃圾搏斗.. 您应该阅读“NULL 值的处理因所使用的 FIELDS 和 LINES 选项而异”部分:-dev.mysql.com/doc/refman/8.0/en/load-data.html 并发布您遇到问题的行示例。 在该行写入 NULL。 @jarlh 您尝试过什么调试问题?这甚至与编程有关吗? 【参考方案1】:

显然,您需要特殊处理来处理该值。您没有指定哪个值,但一种方法是使用变量。那么,如果salary哪里出了问题:

LOAD DATA INFILE 'C:/ProgramData/MySQL/MySQL Server 8.0/Uploads/employees.csv'
INTO TABLE EmployeeS (EmployeeId, LastName, . . . , @salary)
    SET salary = (CASE WHEN @salary <> 'NULL' THEN CAST(@salary AS FLOAT) END)
FIELDS TERMINATED BY ','
IGNORE 1 ROWS;

当然,我不知道哪一栏是真正的问题。这只是说明如何修复它。

也就是说,您可能还有其他问题。我希望数据与表中的列对齐,您可能想了解它们为什么不对齐。

【讨论】:

您的意思是“如果 salary 是问题所在”? 在 .csv 文件中,ReportsTo 列中有 NULL。试过这段代码不起作用。 @戈登林诺夫 我认为salary 不是问题所在。 @朱莉娅 @GHOSTRIDER,薪水总是是个问题,无论是对员工还是对公司...【参考方案2】:

此代码有效。

LOAD DATA INFILE 'C:/ProgramData/MySQL/MySQL Server 8.0/Uploads/employees.csv'
INTO TABLE Employees
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
IGNORE 1 ROWS;

【讨论】:

以上是关于错误代码:1366。不正确的整数值:第 2 行的列“ReportsTo”的“NULL”的主要内容,如果未能解决你的问题,请参考以下文章

不正确的整数值:''对于第1行的列'id'

错误 1366 (HY000):不正确的字符串值:第 1 行的列 'comment' 的 '\xF0\x9F\x98\x9C'

Azure 中的 Django 应用程序 - /edit_profile/ 处的 OperationalError(1366,“第 1 行的列 'first_name' 的字符串值不正确:'\\xC5

日期时间格式无效:1366 字符串值不正确

SQLSTATE[HY000]:一般错误:1366 整数值不正确:

TYPO3:SQL 错误:'不正确的整数值:'' 列 'sys_language_uid' 在第 1 行'