学习Python(14)Python操作MySQL数据库进行增删改查操作

学习Python(14)Python操作MySQL数据库进行增删改查操作

一.连接MySQL

首先需要引包:import pymysql
连接进行数据库操作需要3

  1. 连接mysql
  2. 创建游标
  3. 操作数据库
  4. 获取查询结果
  5. 断开连接

连接数据库:

# 连接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()

七.练习

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:
    	# 回滚
    	conn.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()

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 分页查询员工信息

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()
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值