Python和MySQL - 刪除記錄示例



Python使用c.execute(q)函式從表格中刪除記錄,其中c是游標,而q是要執行的delete查詢。

語法

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

cursor.commit()

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

Sr.編號。 引數和說明
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
廣告