pymysql python 操作mysql

import pymysql
conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', passwd='', db='work')

cursor = conn.cursor()#创建游标
#增加单个
# r = cursor.execute("insert into class(caption)values (%s)",('BTCE1班',))
# print (r) #打印受影响的条数
# print (cursor.lastrowid ) #打印自增id

# ---批量增加
# l = [("BTEC1班"),("BTEC2班"),("BTEC3班"),("BTEC4班"),]
# r = cursor.executemany("insert into class(caption)values (%s)",l)
# print (r) #打印受影响的条数
# new_id = cursor.lastrowid
# print (new_id ) #打印自增id
# over_id = new_id +r
# k = [];
#
# #批量获取自增id的cid;
# while over_id > new_id:
#     k.append(new_id)
#     new_id += 1;
# print(k)
# conn.commit()


# # 改
# #语法 updata 上数据 表 set 列== %s where 设置id.
# r = cursor.execute("UPDATE class set caption = %s where cid = 44",('王玉',))
# conn.commit()
# print (r) #打印受影响的条数
# print (cursor.lastrowid ) #打印自增id

# ----------- 增删改,均需要 conn.commit() 指派取人

#查
# r = cursor.execute("select caption from class where cid = 44 or cid = 41 or cid = 40")
# #默认排序为sql排序,不按照sql查询语句排序
# print(cursor.fetchone())
# # cursor.scroll(-1,mode='relative')  # 相对当前位置移动
# # cursor.scroll(2,mode='absolute') # 相对绝对位置移动


#默认元组类型
cursor = conn.cursor(cursor=pymysql.cursors.DictCursor) #设置为字典
cursor.execute("select caption from class where cid = 44 or cid = 41 or cid = 40")
print(cursor.fetchall())
#一遍取完了,剩下的没了
print(cursor.fetchall())
cursor.close()
conn.close()

基础

猜你喜欢

转载自www.cnblogs.com/bdua/p/12400185.html