Python Mysql 简明教程
Python & MySQL - Where Clause Example
Python 使用 c.execute(q) 函数使用 Where 子句有条件选择表中记录,其中 c 是游标,q 是需要执行的选择查询。
Syntax
# execute SQL query using execute() method.
cursor.execute(sql)
result = cursor.fetchall()
for record in result:
print(record)
Sr.No. |
Parameter & Description |
1 |
$sql 必需 - 从表中选择记录的 SQL 查询。 |
Example
尝试以下示例从表中选择记录 −
复制并粘贴以下示例作为 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()