使用 MySQL 数据库模块执行查询语句,与使用 SQLite 数据库模块执行查询语句基本相似,只需注意 SQL 语句中的占位符的差别即可。例如,如下程序示范了查询 MySQL 数据库中的数据:
import mysql.connector
conn = mysql.connector.connect(user='root', password='123456',
host='localhost', port='3306',
database='tb', use_unicode=True)
c = conn.cursor()
#调用执行select语句查询数据
c.execute('select * from user_tb where user_id > %s', (2,))
#通过游标的description属性获取列信息
for col in (c.description):
print(col[0], end='\t')
print('\n--------------------------------')
#直接使用for循环来遍历游标中的结果集
for row in c:
print(row)
print(row[1] + '-->' + row[2])
c.close() #关闭游标
conn.close() #关闭连接
上面程序中,调用 execute() 方法执行 select 语句查询数据,在该 SQL语句中同样使用了 %s 作为占位符,这就是与 SQLite 数据库模块的差别。 该程序直接使用 for 循环来遍历游标所包含的查询数据,这完全是可以的,因为游标本身就是可遍历对象。运行上面的程序,可以看到如下查询结果:
MySQL 数据库模块的游标对象同样支持 fetchone()、fetchmany()、fetchall() 方法,例如如下程序使用 fetchmany() 方法每次获取 3 条记录:
import mysql.connector
conn = mysql.connector.connect(user='root', password='123456',
host='localhost', port='3306',database='tb', use_unicode=True)
c = conn.cursor()
#调用执行select语句查询数据
c.execute('select * from user_tb where user_id > %s', (2,))
# 通过游标的description属性获取列信息
for col in (c.description):
print(col[0], end='\t')
print('\n--------------------------------')
#抓取3条记录,该方法返回一个多个元组组成的列表
rows = c.fetchmany(3)
# 再次使用循环遍历获取的列表
for r in rows:
print(r)
#c.close() 游标在抓取3条记录的时候已自动关闭
conn.close() #关闭连接