gpt4 book ai didi

python - PyMySQL:插入数据准备语句

转载 作者:行者123 更新时间:2023-11-29 18:12:38 29 4
gpt4 key购买 nike

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

Not all arguments converted during string formatting  

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

try:
description1 = product.getElementsByTagName('description')[0]
description = description1.childNodes[0].data
except IndexError:
print('No description')
description = 'None'

当我在没有准备好的语句的情况下插入数据时,它工作正常。但我想对转义字符使用准备好的语句。

这是我准备好的语句代码:

        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)
)

最佳答案

我看到出了什么问题。这是工作代码:

# 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)

关于python - PyMySQL:插入数据准备语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47272256/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com