import pymysql
# 获取连接
conn = pymysql.connect(
host='10.105.212.1',
port=3306,
user='root',
password='DemoDemo',
database='db',
charset='utf8'
)
# 获取游标
# 字典方式显示 pymysql.cursors.DictCursor {'id': 1, 'name': 'liuxu', 'password': '12345'}
cursor = conn.cursor(pymysql.cursors.DictCursor)
# cursor = conn.cursor()
# 准备SQL语句
sql = 'select * from users;'
print(cursor.execute(sql)) # 返回受影响的函数
print(cursor.fetchone()) # 取出一行数据, (1, 'liuxu', '12345')
print(cursor.fetchmany(2)) # 取出N行数据
print(cursor.fetchall()) # 取出剩下所有数据
"""
4
{'id': 1, 'name': 'liuxu', 'password': '12345'}
[{'id': 2, 'name': 'liuxu2', 'password': '12345'}, {'id': 3, 'name': 'liuxu3', 'password': '12345'}]
[{'id': 4, 'name': 'liuxu4', 'password': '12345'}]
"""
标签:name,基础,pymysql,查询,cursor,password,12345,id
From: https://www.cnblogs.com/leoshi/p/17259917.html