转储 Excel 表格数据到 MySQL 数据库时出错

2024-06-12 22:46:33 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在从excel表导入特定的数据,并将这些数据转储到mysql数据库中。 但在这样做的时候,我得到了错误

       $ python dtz_db.py
dtz_db.py:43: Warning: Field 'datetime' doesn't have a default value
  cursor.execute(query2, values2)
dtz_db.py:43: Warning: Data truncated for column 'lease_start_date' at row 1
  cursor.execute(query2, values2)
dtz_db.py:43: Warning: Data truncated for column 'lease_end_date' at row 1
  cursor.execute(query2, values2)
dtz_db.py:43: Warning: Incorrect integer value: '' for column 'lease' at row 1
  cursor.execute(query2, values2)
dtz_db.py:43: Warning: Incorrect integer value: '' for column 'leased' at row 1
  cursor.execute(query2, values2)
Traceback (most recent call last):
  File "dtz_db.py", line 44, in <module>
    cursor.execute(query1, values1)
  File "c:\Python27\lib\site-packages\MySQLdb\cursors.py", line 205, in execute
    self.errorhandler(self, exc, value)
  File "c:\Python27\lib\site-packages\MySQLdb\connections.py", line 36, in defau
lterrorhandler
    raise errorclass, errorvalue
_mysql_exceptions.IntegrityError: (1452, 'Cannot add or update a child row: a fo
reign key constraint fails (`dtz_new`.`property_property`, CONSTRAINT `lease_id_
refs_id_816819bc` FOREIGN KEY (`lease_id`) REFERENCES `property_propertylease` (
`id`))')

我的python文件是这个cod,用于从excel文件中选择特定的数据并将这些数据转储到mysql数据库

^{pr2}$

我的数据库表模式是enter image description here

请帮我解决这个问题,,,,谢谢


Tags: 数据pyforexecutedbvaluecolumncursor
1条回答
网友
1楼 · 发布于 2024-06-12 22:46:33

insert语句声明将有8个变量提供给DB,但只提供了7个变量。从那里开始。在

(我既不懂python也不懂excel交互,所以我没有发布任何代码。不过,我怀疑问题完全出在INSERT语句中。)

编辑:因此外键约束错误意味着根据您的架构,property\u property的lease_id指向另一个表(property_propertylease)中的数据。因为没有给第二个表任何数据,所以插入失败。在

换句话说,insert语句填充父表。父表试图引用不存在的子表中的数据。在

相关问题 更多 >