赞
踩
1.连接数据库
安装pymysql模块
pip install pymysql
- import pymysql
-
- # 连接mysql
- # 参数1:表示主机或ip地址
- # 参数2:表述端口号
- # 参数3:表示mysql的用户名
- # 参数4:表示mysql的密码
- # 参数5:表示mysql的数据库名
-
- conn = pymysql.connect(host='127.0.0.1',port=3306,user='root', password='root', database='mydb2')
-
- # 创建游标对象: 可以执行sql语句
- cursor = conn.cursor()
-
- # sql语句
- sql = 'show tables'
-
- # 执行sql语句
- cursor.execute(sql)
-
- res = cursor.fetchall()
- print(res)
-
- # 关闭游标对象
- cursor.close()
-
- # 关闭mysql的连接
- conn.close()
2.增删改mysql数据
- import pymysql
- db = pymysql.connect(host='127.0.0.1', port=3306, user="root", password="root", database="数据库名")
-
- '''
- cursor = db.cursor()
- cursor.close()
- '''
- #使用with关键词可以不用手动关闭游标,会自动关闭
- with db.cursor() as cur:
- # 增
- # sql = "insert into stu values(1005,'美女')"
-
- # 删
- # sql = "delete from stu where stuid = 1001"
-
- # 改
- sql = "update stu set stuname = '小丽' where stuid = 1002"
-
- try:
- cur.execute(sql)
- # 成功后提交事务
- db.commit()
- except:
- # 失败后 回滚
- db.rollback()
-
- db.close()
3.查询数据
- import pymysql
-
- db = pymysql.connect(host='127.0.0.1', port=3306, user="root", password="root", database="数据库名")
-
- with db.cursor() as cur:
- try:
- sql = "select * from student"
- cur.execute(sql)
-
- # 获取查询的结果
- '''
- fetchall() 获取所有数据
- fetchone() 获取一条数据 调用一次输出一条数据
- fetchmany(n) 获取n条数据
- '''
- # print(cur.fetchall())
- # print(cur.fetchone())
- # print(cur.fetchone())
- # print(cur.fetchone())
-
- print(cur.fetchmany(4)) # 获取4条数据
- print(cur.fetchmany(14)) # 获取14条数据
- except:
- print("查询失败")
-
- db.close()
4.总结
1)基本步骤:
(1)引入第三方模块:import pymysql
(2)连接数据库:db = pymysql.connect(host='127.0.0.1', port=3306, user='用户名', password='数据库密码', database='数据库名')
(3)创建游标:cursor = db.cursor()
(4)准备sql语句对数据库进行操作(用try...except..语句捕获和抛出异常,如果出现错误对数据库进行回滚)
(5)关闭游标(如果使用with...as就不用关闭):cursor.close()
(6)关闭数据库:db.close()
2)增删改操作都需要对数据进行提交操作commit(),而查询操作不需要进行提交操作
5.练习
1)创建表格, 并添加数据
- 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);
-
2)添加一个部门
- 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:
- # 回滚
- con.rollback()
-
- finally:
- # 5. 关闭连接释放资源
- con.close()
-
- if __name__ == '__main__':
- main()
3)删除一个部门
- 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()
说明:如果不希望每次SQL操作之后手动提交或回滚事务,可以像上面的代码那样,在创建连接的时候多加一个名为autocommit的参数并将它的值设置为True,表示每次执行SQL之后自动提交。如果程序中不需要使用事务环境也不希望手动的提交或回滚就可以这么做。
4)更新一个部门
- 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()
5)查询所有部门
- 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()
6)分页查询员工信息
- import pymysql
- 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()
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。