简体   繁体   English

删除行后,commit() 未提交 mysql 表

[英]mysql table is not committed by commit() after delete row

I am trying to delete multiple rows in mysql table, through a loop in python.我正在尝试通过 python 中的循环删除 mysql 表中的多行。 There is no error message but finally the table is not being updated.没有错误消息,但最终表没有被更新。 My code is:我的代码是:

import mysql.connector

mydb = mysql.connector.connect(
    host="localhost",
    user="root",
    password="1234",
    database="Share")
mycursor = mydb.cursor()

for i in range(288, 297):
    sql = "DELETE from name_list where ID = " + str(i)
    mycursor.execute(sql)
mydb.commit()

mycursor.execute("SELECT * from Share.name_list")
for row in mycursor.fetchall() :
    print(row)

Thanks谢谢

How about this?这个怎么样?

for i in range(288, 297):
    sql = "DELETE from name_list where ID = %s"
    mycursor.execute(sql, (i, ))
mydb.commit()

source 来源

this should automatically quote the variable based on the datatype and has the added benefit of sql injection protection.这应该根据数据类型自动引用变量,并具有 sql 注入保护的额外好处。 In this case it doesn't matter, since the parameter is always generated by range() but concatenating variables into sql queries manually is generally bad habit.在这种情况下没关系,因为参数总是由range()生成,但是手动将变量连接到 sql 查询中通常是坏习惯。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM