python笔记-mysql命令使用示例(使用pymysql执行)

一、mysql命令使用

学习完mysql现在来进行一些测试

1.1、进入mysql

终端输入一下命令,进入数据库

mysql -uusername -p

1.2 进入数据库

在mysql终端输入如下命令, 进入数据库

use database_name;


成功进入数据库

二、python程序中使用mysql命令

2.1 建立链接

import pymysql

db_config = {
    "host": "127.0.0.1",
    "user": "dyp",
    "password": "dyp1996",
    "database": "test",
    "charset": "utf8",
    "port": 3306
}
# 连接数据库
connection = pymysql.connect(**db_config)
# 建立游标, 关于执行sql语句, 获取数据, 都要通过游标来完成
cursor = connection.cursor()

2.2 创建相关表

以学生选课为例

2.2.1 建表, 修改

# 创建学生表
sql_student = "create table if not exists student(id int primary key auto_increment, name varchar (20) not null) charset='utf8';"
cursor.execute(sql_student)
# 插入学生数据
insert_student = "insert into student(name) values ('小明'),('小化'),('小花'),('小蓝'),('小芳'),('小七')"
cursor.execute(insert_student)
connection.commit()

# 创建学生详细信息表
sql_student_details = "create table if not exists student_detail(id int primary key auto_increment, sid int not null,\
sex ENUM('男', '女') default '男', no int not null, age int not null ,\
foreign key(sid) references student(id)) charset='utf8';"
cursor.execute(sql_student_details)

# 插入学生的详细信息
insert_student_details = "insert into student_detail(sid, sex, no, age) values(1, '男', 100101, 19), (2, '男', 100102, 18), (3, '女', 100102, 20), (4, '女', 100103, 20), (5, '女', 100104, 18), (6, '男', 100105, 20)"
cursor.execute(insert_student_details)
connection.commit()

# 创建课程表
sql_course = "create table if not exists course(id int primary key auto_increment, c_name varchar(50) not null)"
cursor.execute(sql_course)
# 插入课程数据
insert_course = "insert into course(c_name) values ('Java'), ('Python'), ('人工智能'), ('高等数学')"
cursor.execute(insert_course)
connection.commit()

# 创建选课表
sql_choose = "create table choose(sid int not null, cid int, grade float(4,1), primary key(sid, cid), \
foreign key(sid) references student(id), foreign key(cid) references course(id))"
cursor.execute(sql_choose)
# 插入选课
insert_choose = "insert into choose(sid, cid) values(1, 2), (1, 3), (2, 1), (2, 4), (3, 3), (3, 2), (4, 3), (4, 1), (5, 4), (5, 2), (6, 1), (6, 3)"
cursor.execute(insert_choose)
connection.commit()

# 修改数据
sql_update = "update choose set grade=96 where sid=6 and cid=3"
cursor.execute(sql_update)
connection.commit()

执行后结果如下图:




2.2.2 查询数据

# 1. 查看学生选课情况, id, s_name, no, cid, c_name
sql_select1 = "select student.id, student.name, student_detail.no, choose.cid, course.c_name from student, student_detail, course, choose \
where student.id=student_detail.sid and student.id=choose.sid and course.id=choose.cid"

cursor.execute(sql_select1)
for item in cursor.fetchall():
    print(item)

print("****************************************************************")
# 2. 查看学生选课的成绩, 并按降序排列
sql_select2 = "select student.id, student.name, student_detail.no, choose.cid, course.c_name, choose.grade \
from student, student_detail, course, choose where student.id=student_detail.sid and student.id=choose.sid and course.id=choose.cid order by choose.grade desc "
cursor.execute(sql_select2)
for item in cursor.fetchall():
    print(item)


print("*****************************************************************")
# 3. 连接查询
sql_select3 = "select student.id, student.name, student_detail.no, course.c_name, course.id, choose.grade from student " \
              "left join student_detail on student.id=student_detail.sid " \
              "left join choose on choose.sid=student.id " \
              "left join course on choose.cid=course.id " \

cursor.execute(sql_select3)
for item in cursor.fetchall():
    print(item)


print("********************************************************")

# 4. 子查询, 查询成家在70-80之间的学生的id, no, c_name
sql_select4 = "select student.*, student_detail.no, course.c_name from student " \
              "left join student_detail on student_detail.sid=student.id " \
              "left join choose on choose.sid=student.id " \
              "left join course on course.id=choose.cid " \
              "where choose.grade in "\
              "(select choose.grade from choose where choose.grade between 70 and 80) " \

cursor.execute(sql_select4)
for item in cursor.fetchall():
    print(item)

print("**************************************")

# 5. 聚合函数的使用
# 5.1 统计选课表中每节课的选课次数, 选课名称, 平均成绩
sql_select5 = "select count(choose.cid), course.c_name, avg(choose.grade) from choose, course where choose.cid=course.id group by choose.cid"
cursor.execute(sql_select5)
for item in cursor.fetchall():
    print(item)

执行结果如下

(1, '小明', 100101, 2, 'Python')
(1, '小明', 100101, 3, '人工智能')
(2, '小化', 100102, 1, 'Java')
(2, '小化', 100102, 4, '高等数学')
(3, '小花', 100102, 2, 'Python')
(3, '小花', 100102, 3, '人工智能')
(4, '小蓝', 100103, 1, 'Java')
(4, '小蓝', 100103, 3, '人工智能')
(5, '小芳', 100104, 2, 'Python')
(5, '小芳', 100104, 4, '高等数学')
(6, '小七', 100105, 1, 'Java')
(6, '小七', 100105, 3, '人工智能')
****************************************************************
(6, '小七', 100105, 3, '人工智能', 96.0)
(5, '小芳', 100104, 2, 'Python', 90.0)
(6, '小七', 100105, 1, 'Java', 90.0)
(3, '小花', 100102, 3, '人工智能', 87.0)
(5, '小芳', 100104, 4, '高等数学', 87.0)
(3, '小花', 100102, 2, 'Python', 80.0)
(1, '小明', 100101, 3, '人工智能', 79.0)
(2, '小化', 100102, 1, 'Java', 77.0)
(2, '小化', 100102, 4, '高等数学', 77.0)
(4, '小蓝', 100103, 3, '人工智能', 75.0)
(1, '小明', 100101, 2, 'Python', 70.0)
(4, '小蓝', 100103, 1, 'Java', 70.0)
*****************************************************************
(1, '小明', 100101, 'Python', 2, 70.0)
(1, '小明', 100101, '人工智能', 3, 79.0)
(2, '小化', 100102, 'Java', 1, 77.0)
(2, '小化', 100102, '高等数学', 4, 77.0)
(3, '小花', 100102, 'Python', 2, 80.0)
(3, '小花', 100102, '人工智能', 3, 87.0)
(4, '小蓝', 100103, 'Java', 1, 70.0)
(4, '小蓝', 100103, '人工智能', 3, 75.0)
(5, '小芳', 100104, 'Python', 2, 90.0)
(5, '小芳', 100104, '高等数学', 4, 87.0)
(6, '小七', 100105, 'Java', 1, 90.0)
(6, '小七', 100105, '人工智能', 3, 96.0)
********************************************************
(1, '小明', 100101, 'Python')
(1, '小明', 100101, '人工智能')
(2, '小化', 100102, 'Java')
(2, '小化', 100102, '高等数学')
(3, '小花', 100102, 'Python')
(4, '小蓝', 100103, 'Java')
(4, '小蓝', 100103, '人工智能')
**************************************
(3, 'Java', 79.0)
(3, 'Python', 80.0)
(4, '人工智能', 84.25)
(2, '高等数学', 82.0)

猜你喜欢

转载自www.cnblogs.com/duyupeng/p/13190616.html