|
#!/usr/bin/env python
# -*- coding:utf-8 -*-
import pymysql
#打开数据库连接
conn = pymysql.connect(host='sydnagios', port=3306, user='yli', passwd='yli', db='mydb')
#创建一个游标对象
cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)
#SQL查询
cursor.execute("select * from student")
# 获取第一行数据
# row_1 = cursor.fetchone()
# print(row_1)
# 获取前n行数据
# row_2 = cursor.fetchmany(3)
# 获取所有数据
row_3 = cursor.fetchall()
print(row_3)
#scroll可以使用相对位置或者绝对位置,这里相对位置(末尾)向上移动2行
cursor.scroll(-2,mode='relative')
row_3 = cursor.fetchall()
print(row_3)
#提交,不然无法保存新的数据
conn.commit()
#关闭游标
cursor.close()
#关闭连接
conn.close()
-----------
[{'id': 1, 'name': 'Jay'}, {'id': 2, 'name': 'Bob'}, {'id': 3, 'name': 'Alex'}]
[{'id': 2, 'name': 'Bob'}, {'id': 3, 'name': 'Alex'}] |
|
|