Python 将数据从excel工作表转储到mysql数据库时出错

Python 将数据从excel工作表转储到mysql数据库时出错,python,mysql,excel,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'

我正在从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`))')
我的数据库表模式是


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

您的insert语句断言将有8个变量提供给DB,但您只提供了7个。从那里开始

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

编辑:因此外键约束错误意味着根据您的模式,property\u property的lease\u id指向另一个表(property\u propertylease)中的数据。由于您没有向第二个表提供任何数据,因此插入失败


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

如何执行此操作?任何提示,,,,,,,,,,,请先将数据放入子表,或从表中删除外键约束。FK约束的存在正是出于这个原因……为了防止第二个表缺少数据时一个表指向另一个表,应用程序中的FK是关联数据所必需的,,,,,,第二个选项是最好的,但我如何将数据同时放入父表和子表中,因为两者都是相互关联的。这有点棘手。您必须执行一些操作,例如将除lease_id之外的所有内容插入表A,然后将数据添加到表B,然后使用lease_id再次更新表A。
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!"