- Python 及 MySQL 教程
- Python 及 MySQL - 首頁
- Python 及 MySQL - 概覽
- Python 及 MySQL - 環境設定
- Python 及 MySQL 示例
- Python 及 MySQL - 連線資料庫
- Python 及 MySQL - 建立資料庫
- Python 及 MySQL - 刪除資料庫
- Python 及 MySQL - 選擇資料庫
- Python 及 MySQL - 建立資料表
- Python 及 MySQL - 刪除資料表
- Python 及 MySQL - 插入記錄
- Python 及 MySQL - 選擇記錄
- Python 及 MySQL - 更新記錄
- Python 及 MySQL - 刪除記錄
- Python 及 MySQL - Where 從句
- Python 及 MySQL - Like 從句
- Python 及 MySQL - 排序資料
- Python 及 MySQL - 使用 JOIN
- Python 及 MySQL - 執行事務
- Python 及 MySQL - 處理錯誤
- Python 及 MySQL 實用資源
- Python 及 MySQL - 快速指南
- Python 及 MySQL - 實用資源
- Python 及 MySQL - 討論
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 = "UPDATE tutorials_tbl set tutorial_title = "Learning Java" 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
廣告