pymysql模块


http://www.cnblogs.com/linhaifeng/articles/7525619.html
import pymysql
#连接
conn=pymysql.connect(
    host='localhost',
    port=3306,
    user='root',
    password='123',
    database='db11',
    charset='utf8'
)
#获取指针(相当于登录mysql的指针)
# cur=conn.cursor()
cur=conn.cursor(cursor=pymysql.cursors.Dictursor)   #字典的格式
#写sql语句
# sql='''
# create table useinfo(C
#     id int primary key auto_increment,
#     user char(16),
#     password CHAR (20)
# );
# '''
# sql='''
# insert into useinfo(user,password) VALUES ('lijing','456');
# '''
sql='''
SELECT * FROM useinfo;
'''
#执行sql语句,返回sql查询成功的记录数目
res=cur.execute(sql)
print(res)

#执行insert操作时,查看自增的主键值
# print(cur.lastrowid)
#查看返回值
print(cur.fetchone())
print(cur.fetchmany(2))
print(cur.fetchall())
#光标的移动
# cur.scroll(0,'absolute')
# cur.scroll(0,'relative')
#指针关闭
cur.close()
#提交
conn.commit()
conn.close()

 



























 

posted @ 2018-03-28 07:02  Hantaozi  阅读(69)  评论(0)    收藏  举报