# -*- coding:utf-8 -*-
# Author:caya
import pymysql
# 建立连接
conn = pymysql.connect(
host='127.0.0.1',
port=3306,
user='caya',
password='123',
db='db1',
charset='utf8'
)
# 拿到游标
cursor = conn.cursor(pymysql.cursors.DictCursor) # 以字典形式取结果
# 增/删/改的执行方法
# 获取指令
# row = cursor.execute(sql, ('nana', '123'))
# cursor.executemany('insert into userinfo(user,psw) value(%s,%s)', [('caya1', 'abc'), ('caya2', 'abc'), ('caya3', 'abc')])
print(cursor.lastrowid) # 查询最后的自增id
# 查询的执行
rows = cursor.execute('select * from userinfo;')
# 逐个输出
print(cursor.fetchone())
print(cursor.fetchone())
# 多个输出
print(cursor.fetchmany(2))
# 全部取出3
print(cursor.fetchall())
# 移动光标
cursor.scroll(0, mode='absolute') # 绝对位置移动
cursor.scroll(1, mode='relative') # 相对当前位置移动
print(cursor.fetchall())
# 数据库执行
conn.commit()
# 关闭
cursor.close()
conn.close()