需要帮助来解决此问题

时间:2020-07-18 12:01:38

标签: python mysql-python

我尝试使用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'列的整数值不正确:”)

1 个答案:

答案 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, ))