我尝试使用python将数据插入数据库,但出现MySQLdb错误
self.cur.execute('''
INSERT INTO book(book_name, book_desc, book_code, book_category, book_author, book_publisher, book_price)
VALUES(%s, %s, %s, %s, %s, %s, %s)
''', (book_title, book_desc, book_code, book_category, book_author, book_publisher, book_price, ))
self.db.commit()
我收到此错误:
MySQLdb._exceptions.OperationalError:(1366,“第1行的'book_code'列的整数值不正确:”)
答案 0 :(得分:0)
根据您在注释中共享的值,book_code
是一个字符串,与数据库中的int
值不兼容。您可以通过将其转换为int
来解决此问题:
self.cur.execute('''
INSERT INTO book(book_name, book_desc, book_code, book_category, book_author, book_publisher, book_price)
VALUES(%s, %s, %s, %s, %s, %s, %s)
''', (book_title, book_desc, int(book_code), book_category, book_author, book_publisher, book_price, ))