- 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 - 使用連線
- Python 和 MySQL - 執行事務
- Python 和 MySQL - 處理錯誤
- Python 和 MySQL 有用資源
- Python 和 MySQL - 快速指南
- Python 和 MySQL - 有用資源
- Python 和 MySQL - 討論
Python 和 MySQL - Where 子句示例
Python 使用 c.execute(q) 函式從表格中根據 Where 子句有條件地選擇一條或多條記錄,其中 c 是遊標且 q 是要執行的選擇查詢。
語法
# execute SQL query using execute() method. cursor.execute(sql) result = cursor.fetchall() for record in result: print(record)
| 序號 | 引數和說明 |
|---|---|
| 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 = "Select * from tutorials_tbl Where tutorial_id = 3"
# execute SQL query using execute() method.
cursor.execute(sql)
# fetch all records from cursor
result = cursor.fetchall()
# iterate result and print records
for record in result:
print(record)
# disconnect from server
db.close()
輸出
使用 Python 執行 mysql_example.py 指令碼並驗證輸出。
(3, 'JQuery', 'Julie', datetime.date(2020, 5, 10))
廣告