pymysql增删改查
#!/usr/bin/env python
# encoding: utf-8
# Date: 2018/6/24
# 1、增删改
import pymysql
conn = pymysql.connect(
host='127.0.0.1',
port=3306,
user='root',
password='123',
db='stu_system',
charset='utf8'
)
# 拿到游标
cursor = conn.cursor()
sql = 'insert into stu_teacher(tname) values (%s)'
# 插入一条
# rows = cursor.execute(sql, ('wxx'))
# print(rows)
# 插入多条
rows = cursor.executemany(sql, [('wxx',), ('egonxx',)])
print(rows)
# 查看当前自增id数值
print(cursor.lastrowid)
conn.commit()
cursor.close()
conn.close()
# # 2、查询
# import pymysql
#
# conn = pymysql.connect(
# host='127.0.0.1',
# port=3306,
# user='root',
# password='123',
# db='stu_system',
# charset='utf8'
# )
# # 拿到游标
# # cursor = conn.cursor() # 元组形式的游标
# cursor = conn.cursor(pymysql.cursors.DictCursor) # 字典形式的游标
# # 查询
# rows = cursor.execute('select * from stu_teacher;')
#
# # 取一行
# r1 = cursor.fetchone() # 元组(1, '李必胜')
# print(r1)
#
# # 取多行
# r2 = cursor.fetchmany(2) # 2行
# print(r2)
#
# # 取所有
# rall = cursor.fetchall()
# print(rall)
#
# # cursor 设置光标位置
# # cursor.scroll(3, mode='relative') # 相对位置移动
# # cursor.scroll(3, mode='absolute') # 绝对位置移动
#
# cursor.close()
# conn.close()