python的mysql 3

import pymysql

1.连接数据库

conn = pymysql.connect(host='localhost',
                       user='root',
                       password='redhat',
                       db='westos',
                       charset='utf8',
                       autocommit='True'
)

2.创建游标对象

cur = conn.cursor()

3.对数据库操作

#####查询数据库
sqli = 'select * from hello'

默认不返回查询结果集 返回的是数据记录数

result = cur.execute(sqli)
print(result)
a = cur.fetchone()
print(a)

获取下一条查询结果集

print(cur.fetchone())
print(cur.fetchone())

获取指定个数查询结果集

print(cur.fetchmany(4))
info = cur.fetchall()
print(info)
可以通过cursor.scroll(position, mode="relative | absolute")方法,来设置相对位置游标和绝对位置游标

当mode='absolute'时,代表绝对移动,
   value就代表移动的绝对位置,
   value=0就代表移动到位置0处,就是结果集开头,
   value=3就是移动到位置3处,也就是第4条记录处

当mode='relative',代表相对移动
当mode='relative'时,value就是移动的长度,
	value>0向后移动(从位置0移动到位置2),
	value<0向前移动(比如从位置2移动到位置0)

“”"

print(cur.fetchmany(3))

print(‘移动到指针最开始的地方…’)

cur.scroll(0,'absolute')
print(cur.fetchmany(3))
print(cur.fetchmany(2))
print(cur.fetchall())
cur.scroll(-2,mode='relative')
print(cur.fetchmany(2))
发布了103 篇原创文章 · 获赞 1 · 访问量 954

猜你喜欢

转载自blog.csdn.net/qq_45652989/article/details/103951518