python 操作数据库 pymysql
import pymysql #创建连接 conn = pymysql.connect(host='localhost',port=3306,user='root',passwd='123',db ='mydb') #创建游标 cur = conn.cursor() #执行sql并返影响的行数 # res = cur.execute('insert into userInfo(name,sex) values("wangwang","M")') res2 = cur.execute('select * from userInfo ') #获取第一条结果 , 每个结果只能取一次 result = cur.fetchone() print(result) #获取前三条 reNumber= cur.fetchmany(3) print(reNumber) #获取所有查询结果 re_list = cur.fetchall() print(re_list) # print(res) conn.commit() conn.close()
posted on 2018-01-29 18:31 gaizhongfeng 阅读(138) 评论(0) 编辑 收藏 举报