PyMySQL:插入数据准备语句

2024-09-30 08:18:47 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图使用一个准备好的语句在数据库中插入数据。我在数据库中的所有列都是VARCHAR,但是PyMySQL给出了以下错误:

Not all arguments converted during string formatting  

所有变量都是从XML文件解析的子节点。变量示例:

^{pr2}$

当我插入没有准备好的语句的数据时,它工作得很好。但是我想对转义符使用预先准备好的语句。在

这是我准备好的声明代码:

        sql = """INSERT INTO Studystore(daisycon_unique_id, title, author, isbn, price, link, image_location, category, product_condition, description, in_stock, in_stock_amount, price_shipping, language, book_edition, number_of_pages, status, insert_date, update_date)
          VALUES ('%s')"""

        args = (str(daisycon_unique_id), str(title), str(author), str(isbn), str(price), str(link), str(image_location), str(category), str(product_condition), str(description), str(in_stock), str(in_stock_amount), str(price_shipping), str(language), str(book_edition), str(number_of_pages), str(status), str(insert_date), str(update_date),)

        try:

            # Execute the SQL command
            cursor.execute(sql, args)
            # Commit your changes in the database
            db.commit()

        except Exception as e: 
            print(e)
)

Tags: 数据inid数据库sqldatetitlestock
1条回答
网友
1楼 · 发布于 2024-09-30 08:18:47

我看到哪里出了问题。 工作代码如下:

# Prepare SQL query to INSERT a record into the database.
    sql = """INSERT INTO Studystore(daisycon_unique_id, title, author, isbn, price, link, image_location, category, product_condition, description, in_stock, in_stock_amount, price_shipping, language, book_edition, number_of_pages, status, insert_date, update_date)
      VALUES ("%s", "%s", "%s", "%s", "%s", "%s", "%s", "%s", "%s", "%s", "%s", "%s", "%s", "%s", "%s", "%s", "%s", "%s", "%s")"""

    args = (daisycon_unique_id, title, author, isbn, price, link, image_location, category, product_condition, description, in_stock, in_stock_amount, price_shipping, language, book_edition, number_of_pages, status, insert_date, update_date,)

    try:

        # Execute the SQL command
        cursor.execute(sql, args)
        # Commit your changes in the database
        db.commit()

    except Exception as e:
        print(e)

相关问题 更多 >

    热门问题