python - 将数据从 Excel 工作表转储到 mysql 数据库时出现错误

标签 python mysql excel

我正在从 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数据库

import xlrd
import MySQLdb
book = xlrd.open_workbook("dtz11.xls")
sheet = book.sheet_by_name("Sheet1")
database = MySQLdb.connect (host="localhost", user="root", passwd="", db="dtz_new")
cursor = database.cursor()
query1 = """INSERT INTO property_property( name,  inpection_date) VALUES(%s, %s )"""
query2 = """INSERT INTO property_propertylease( lease_start_date,  lease_end_date, lease, leased) VALUES(%s, %s, %s, %s)"""
for r in range(1, sheet.nrows):
    gaurav2         = sheet.cell(r,1).value
    gaurav3         = sheet.cell(r,2).value
    gaurav8         = sheet.cell(r,18).value
    gaurav9         = sheet.cell(r,19).value
    gaurav10        = sheet.cell(r,20).value
    gaurav11        = sheet.cell(r,21).value
    values1 = (gaurav2, gaurav3)
    values2 = (gaurav8, gaurav9, gaurav10, gaurav11)
    cursor.execute(query2, values2)
    cursor.execute(query1, values1)
cursor.close()
database.commit()
database.close()
print "dumped successfully"
columns = str(sheet.ncols)
rows = str(sheet.nrows)
print "I just imported "+ columns+ " columns and "+ rows+" rows to MySQL!"

我的数据库表架构是 enter image description here

请帮我解决这个问题,,,,非常感谢

最佳答案

您的插入语句断言将向数据库提供 8 个变量,但您只给了它 7 个。从这里开始。

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

编辑:因此,外键约束错误意味着根据您的架构,property_property的lease_id指向另一个表(property_propertylease)中的数据。由于您没有向第二个表提供任何数据,因此插入失败。

换句话说,您的插入语句填充父表。父表正在尝试引用不存在的子表中的数据。

关于python - 将数据从 Excel 工作表转储到 mysql 数据库时出现错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24059537/

相关文章:

vba - 查找使用连接的位置 Excel VBA

python - 如何追加屏蔽数组

c++ - 从现有图像中裁剪出图像

python - 如何在Python中多次使用迭代器

excel - 如何使用 Excel 查询编辑器或 Power BI 查询编辑器中单元格的特定值填充列?

excel - 在 Excel 2010 数据透视表中移动日期期间筛选器

用于单元测试的 Python 模拟过程

php - PHP中的mysql_query参数错误

mysql - GROUP_CONCAT 不适用于左连接

php - 使用内部联接选择 COUNT