首先需要引包:import pymysql
连接进行数据库操作需要3步
连接数据库:
# 连接MySQL需要6个参数:主机名,端口号,
#用户名,密码,
#数据库名称,字符集
# 方式一
con = pymysql.connect(
host='localhost', port=3306,
user='root', password='root',
database='mydb2', charset='utf8')
# 方式二
con = pymysql.connect('127.0.0.1', 'root', 'root', 'mydb2')
创建游标对象: 可以执行sql语句
cursor = con.cursor()
执行SQL
sql = 'select * from star'
cursor.execute(sql) # 执行SQL
获取查询结果
res = cursor.fetchall()
print(res)
关闭连接
# 关闭游标对象
cursor.close()
# 关闭mysql的连接
con.close()
import pymysql
conn = pymysql.connect('localhost', 'root', 'root', 'mydb2')
cursor = conn.cursor()
# 插入数据
sql = 'insert into person(name, age) values("aa", 20)'
try:
cursor.execute(sql)
# 提交事务
conn.commit()
except:
# 回滚
conn.rollback()
cursor.close()
conn.close()
import pymysql
conn = pymysql.connect('localhost', 'root', 'root', 'mydb2')
cursor = conn.cursor()
# 删除数据
sql = 'delete from person where id=18'
try:
cursor.execute(sql)
# 提交事务
conn.commit()
except:
# 回滚
conn.rollback()
cursor.close()
conn.close()
import pymysql
conn = pymysql.connect('localhost', 'root', 'root', 'mydb2')
cursor = conn.cursor()
# 更新数据
sql = 'update person set age=30 where id=20'
try:
cursor.execute(sql)
# 提交事务
conn.commit()
except:
# 回滚
conn.rollback()
cursor.close()
conn.close()
# 创建数据库
cursor.execute('create database mydb4 charset=utf8')
# 创建表
sql = '''
create table user (
id int primary key auto_increment,
name varchar(30)
)
'''
cursor.execute(sql)
import pymysql
from pymysql.cursors import DictCursor
db = pymysql.connect('localhost', 'root', 'root', 'mydb2')
with db.cursor(cursor=DictCursor) as cursor:
# 执行查询的sql语句
cursor.execute('select * from student')
# 获取查询结果
# res = cursor.fetchall() # 获取所有数据
# res = cursor.fetchone() # 一次获取下一条数据
# res = cursor.fetchone() # 一次获取下一条数据
res = cursor.fetchmany(3) # 一次获取3条数据
print(res)
# (('1001', 'zhangsan'), ('1002', 'xiaoming'), ('1003', 'jack'), ('1004', 'tom'), ('1005', 'jerry'), ('1006', 'lucy'))
# 使用DictCursor得到字典.
# [{'stuid': '1004', 'stuname': 'tom'}, {'stuid': '1005', 'stuname': 'jerry'}, {'stuid': '1006', 'stuname': 'lucy'}]
db.close()
create table tb_dept
(
dno int not null comment '编号',
dname varchar(10) not null comment '名称',
dloc varchar(20) not null comment '所在地',
primary key (dno)
);
insert into tb_dept values
(10, '会计部', '北京'),
(20, '研发部', '成都'),
(30, '销售部', '重庆'),
(40, '运维部', '深圳');
create table tb_emp
(
eno int not null comment '员工编号',
ename varchar(20) not null comment '员工姓名',
job varchar(20) not null comment '员工职位',
mgr int comment '主管编号',
sal int not null comment '员工月薪',
comm int comment '每月补贴',
dno int comment '所在部门编号',
primary key (eno)
);
alter table tb_emp add constraint fk_emp_dno foreign key (dno) references tb_dept (dno);
insert into tb_emp values
(7800, '张三丰', '总裁', null, 9000, 1200, 20),
(2056, '乔峰', '分析师', 7800, 5000, 1500, 20),
(3088, '李莫愁', '设计师', 2056, 3500, 800, 20),
(3211, '张无忌', '程序员', 2056, 3200, null, 20),
(3233, '丘处机', '程序员', 2056, 3400, null, 20),
(3251, '张翠山', '程序员', 2056, 4000, null, 20),
(5566, '宋远桥', '会计师', 7800, 4000, 1000, 10),
(5234, '郭靖', '出纳', 5566, 2000, null, 10),
(3344, '黄蓉', '销售主管', 7800, 3000, 800, 30),
(1359, '胡一刀', '销售员', 3344, 1800, 200, 30),
(4466, '苗人凤', '销售员', 3344, 2500, null, 30),
(3244, '欧阳锋', '程序员', 3088, 3200, null, 20),
(3577, '杨过', '会计', 5566, 2200, null, 10),
(3588, '朱九真', '会计', 5566, 2500, null, 10);
import pymysql
def main():
no = int(input('编号: '))
name = input('名字: ')
loc = input('所在地: ')
# 1. 创建数据库连接对象
con = pymysql.connect(host='localhost', port=3306,
database='hrs', charset='utf8',
user='yourname', password='yourpass')
try:
# 2. 通过连接对象获取游标
with con.cursor() as cursor:
# 3. 通过游标执行SQL并获得执行结果
result = cursor.execute(
'insert into tb_dept values (%s, %s, %s)',
(no, name, loc)
)
if result == 1:
print('添加成功!')
# 4. 操作成功提交事务
con.commit()
except:
# 回滚
conn.rollback()
finally:
# 5. 关闭连接释放资源
con.close()
if __name__ == '__main__':
main()
import pymysql
def main():
no = int(input('编号: '))
con = pymysql.connect(host='localhost', port=3306,
database='hrs', charset='utf8',
user='yourname', password='yourpass',
autocommit=True)
try:
with con.cursor() as cursor:
result = cursor.execute(
'delete from tb_dept where dno=%s',
(no, )
)
if result == 1:
print('删除成功!')
finally:
con.close()
if __name__ == '__main__':
main()
import pymysql
def main():
no = int(input('编号: '))
name = input('名字: ')
loc = input('所在地: ')
con = pymysql.connect(host='localhost', port=3306,
database='hrs', charset='utf8',
user='yourname', password='yourpass',
autocommit=True)
try:
with con.cursor() as cursor:
result = cursor.execute(
'update tb_dept set dname=%s, dloc=%s where dno=%s',
(name, loc, no)
)
if result == 1:
print('更新成功!')
finally:
con.close()
if __name__ == '__main__':
main()
import pymysql
from pymysql.cursors import DictCursor
def main():
con = pymysql.connect(host='localhost', port=3306,
database='hrs', charset='utf8',
user='yourname', password='yourpass')
try:
with con.cursor(cursor=DictCursor) as cursor:
cursor.execute('select dno as no, dname as name, dloc as loc from tb_dept')
results = cursor.fetchall()
print(results)
print('编号\t名称\t\t所在地')
for dept in results:
print(dept['no'], end='\t')
print(dept['name'], end='\t')
print(dept['loc'])
finally:
con.close()
if __name__ == '__main__':
main()
class Emp(object):
def __init__(self, no, name, job, sal):
self.no = no
self.name = name
self.job = job
self.sal = sal
def __str__(self):
return f'\n编号:{self.no}\n姓名:{self.name}\n职位:{self.job}\n月薪:{self.sal}\n'
def main():
page = int(input('页码: '))
size = int(input('大小: '))
con = pymysql.connect(host='localhost', port=3306,
database='hrs', charset='utf8',
user='yourname', password='yourpass')
try:
with con.cursor() as cursor:
cursor.execute(
'select eno, ename, job, sal from tb_emp limit %s,%s',
((page - 1) * size, size)
)
results = cursor.fetchall()
for emp_tuple in results:
emp = Emp(*emp_tuple)
print(emp)
finally:
con.close()
if __name__ == '__main__':
main()