简体   繁体   English

需要帮助来解决这个问题

[英]need help to fix this issue

I tried to insert data to the database using python but I got a MySQLdb error我尝试使用 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()

I got this error:我收到了这个错误:

MySQLdb._exceptions.OperationalError: (1366, "Incorrect integer value: '' for column 'book_code' at row 1") MySQLdb._exceptions.OperationalError:(1366,“不正确的 integer 值:'' 列 'book_code' 在第 1 行”)

According to the values you shared in the comments, book_code is a string, which is incompatible with the int value in the database.根据您在评论中分享的值, book_code是一个字符串,与数据库中的int值不兼容。 You could overcome this issue by converting it to an 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, ))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM