python+mysql连接,使用pymysql库删除数据表信息记录操作(使用delete语句):
引入pymysql库:
import pymysql
连接数据库:
db = pymysql.connect(
host='127.0.0.1',
port=3306,
user='root',
passwd='root',
db='xiuyixia',
charset='utf8'
)
cursor = db.cursor()
删除语句(1):
sql = "delete from user where id =1;"
cursor.execute(sql)
删除语句(2),带变量的话(传入参数 setname ):
sql = "delete from user where name= %s;"
name = setname
cursor.execute(sql, [name])
运行结果:表中对应的记录被删条了
完整代码:
import pymysql
# 连接数据库
db = pymysql.connect(
host='127.0.0.1',
port=3306,
user='root',
passwd='root',
db='xiuyixia',
charset='utf8'
)
cursor = db.cursor()
sql = "delete from user where id = 1;"
cursor.execute(sql)
db.commit()
# 关闭光标对象
cursor.close()
# 关闭数据库连接
db.close()
带变量:
import pymysql
# 连接数据库
db = pymysql.connect(
host='127.0.0.1',
port=3306,
user='root',
passwd='root',
db='xiuyixia',
charset='utf8'
)
cursor = db.cursor()
sql = "delete from user where name = %s;"
name ='张总'
cursor.execute(sql,[name])
db.commit()
# 关闭光标对象
cursor.close()
# 关闭数据库连接
db.close()