Python PyMysql在插入命中块时失败
我有一个代码,它试图插入列表中的每个Python PyMysql在插入命中块时失败,python,python-3.x,pymysql,Python,Python 3.x,Pymysql,我有一个代码,它试图插入列表中的每个列表项 db = pymysql.connect(host="localhost",user="root",passwd="password",db="garbagedb" ) cursor = db.cursor() for brl in b_result_list: try: sql = """INSERT INT
列表项
db = pymysql.connect(host="localhost",user="root",passwd="password",db="garbagedb" )
cursor = db.cursor()
for brl in b_result_list:
try:
sql = """INSERT INTO paths(path) VALUES (brl)"""
cursor.execute(sql)
db.commit()
print("inserted: {}".format(brl))
except:
print("error inserting")
db.rollback()
db.close()
我似乎无法找出它为什么不起作用
我手动测试了查询
,效果很好
有人看见什么吗?谢谢我花了一些时间试图弄明白这一点,但最终还是弄明白了
下面是我的工作示例:我希望有一天它能帮助别人
db = pymysql.connect(host='localhost', user='root',passwd='password',db='yourdb')
cursor = db.cursor()
for brl in b_result_list:
try:
with db.cursor() as cursor:
sql = "INSERT INTO `paths`(`path`) VALUES (%s)"
cursor.execute(sql, (brl))
db.commit()
print("inserted: {}".format(brl))
except:
print("error inserting")
db.rollback()
db.close()
详情如下: