gpt4 book ai didi

python mysql中将订单号输入到表id中

转载 作者:行者123 更新时间:2023-11-29 21:37:11 25 4
gpt4 key购买 nike

我正在尝试使用字符串和订单号将 id 添加到我的表中。这是代码:

import pymysql.cursors
conn = pymysql.connect(host='localhost', port=3306, user='root', passwd='xxx', db='python')


with conn.cursor() as cursor:
# Create a new record
sql = "INSERT INTO `arzugulgundogdu2` (`id`) VALUES ('dgNotListesi_txtGelisimDurumu_'%s)"


y = int("0")
while y < 10:

cursor.execute(sql, (y))
y = y+1


conn.commit()

此外,当我为 while 循环选择 y 的常量值时,它看起来像 dgNotListesi_txtGelisimDurumu_'myconstanthere。为什么字符串和 y 变量之间有一个 ' 签名?谢谢。我对 python 还很陌生:)

最佳答案

why is there a ' signature between string and my y variable?

这是因为占位符之前有一个额外的单引号:

... 'dgNotListesi_txtGelisimDurumu_'%s
HERE^

而是在 Python 中生成完整值并用它参数化查询:

sql = "INSERT INTO `arzugulgundogdu2` (`id`) VALUES (%s)" 

value = "dgNotListesi_txtGelisimDurumu_%s" % y
cursor.execute(sql, (y, ))

关于python mysql中将订单号输入到表id中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34838558/

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