Python 和 MySQL - 删除记录示例


Python 使用c.execute(q)函数从表中删除记录,其中 c 是游标,q 是要执行的删除查询。

句法

# execute SQL query using execute() method.
cursor.execute(sql)

cursor.commit()

# get the record count updated
print(mycursor.rowcount, "record(s) affected")

先生。 参数及说明
1

$sql

必需 - 用于删除表中记录的 SQL 查询。

例子

尝试以下示例在表中插入记录 -

将以下示例复制并粘贴为 mysql_example.ty -

#!/usr/bin/python

import MySQLdb

# Open database connection
db = MySQLdb.connect("localhost","root","root@123", "TUTORIALS")

# prepare a cursor object using cursor() method
cursor = db.cursor()

sql = "Delete from tutorials_tbl where tutorial_id = 2"

# execute SQL query using execute() method.
cursor.execute(sql)

db.commit()

# get the record count updated
print(cursor.rowcount, " record(s) affected")

# disconnect from server
db.close()

输出

使用 python 执行 mysql_example.py 脚本并验证输出。

1 record(s) affected