Python Mysql 简明教程
Python & MySQL - Like Clause Example
Python 使用 c.execute(q) 函数有条件地从表中使用 Like 语句选择记录,其中 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 必需 - 使用 Like 语句从表中选择记录的 SQL 查询。 |
Example
尝试以下示例来使用 Like 语句从表中选择记录 -
复制并粘贴以下示例作为 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_title like 'J%'"
# 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()