-
建库和建表
--创建数据库
create database orders charset=utf8;
--创建数据表
create table orders(
id int not null auto_increment primary key,
count int not null,
price decimal(10,2) not null,
freight decimal(10,2) not null,
user varchar(50) not null,
status enum('待支付','待发货','待收货') default '待支付' not null,
time date not null
);
-
插入测试数据
① cd素材的目录下
② 登录mysql客户端:mysql -uroot -p
③ 切换数据库: use orders
④ 导入测试数据:source orders.sql
-
实现订单查询功能
① 导包pymysql
② 连接mysql服务
③ 创建游标对象
④ 编写查询所有订单SQL语句
⑤ 执行SQL语句
⑥ 获取所有查询的结果
⑦ 将获取结果转换成列表字典格式
⑧ 对Mysql的操作加Try处理
# 1 导包
import pymysql
def order()
# 2 链接mysql的服务端
conn = pymysql.Connect(
user='root',
password='mysql',
host='192.168.90.172',
database='orders',
port=3306,
charset='utf8'
)
# 3 创建游标对象
cur = conn.cursor()
try:
# 4 编写 查询orders表的所有数据SQL
sql = 'select * from orders;'
# 5 使用游标对象执行SQL
cur.execute(sql)
# 6 获取查询的所有数据 fetchall() ==>元组
result = cur.fetchall()
print('查询数据:', result)
# 打印结果如下:
# 查询数据:((1, 2, Decimal('100.00'), Decimal('10.00'), '老王', ‘待收货', datetime.data(2020, 1, 1)), (2, 3, Decimal('200.00'), Decimal('25.00'), '钱红', ’待支付', datatime.date(2020, 4,1)))
# 7 将数据转换成[{}, {}]
data_lst = []
for row in result:
data_lst.append({
'id': row[0],
'count': row[1],
'price': str(row[2]),
'freight': str(row[3]),
'user': row[4],
'status': row[5],
'time': str(row[6])
})
# 8 加个try优化下
except Exception as e:
print('报错信息:', e)
finally:
# 关闭游标对象
cur.close()
# 关闭连接
conn.close()
-
实现订单增加功能
① 导包pymysql
② 连接mysql服务
③ 创建游标对象
④ 编写增加订单的SQL语句
⑤ 执行SQL语句并提交
⑥ 对Mysql的操作加Try处理
# 1 导包
import pymysql
def add(data):
# 2 链接mysql的服务端
conn = pymysql.Connect(
user='root',
password='mysql',
host='192.168.90.172',
database='orders',
port=3306,
charset='utf8'
)
# 3 创建游标对象
cur =conn.cursor()
try:
# 4 编写 增加数据的SQL
sql = 'insert into orders values(%s,%s,%s,%s,%s,%s,%s)'
# 5 使用游标对象执行SQL并提交
cur.execute(sql, data)
conn.commit()
# 6 加个try优化下
except Exception as e:
print('报错信息:', e)
# 回滚数据
conn.rollback()
finally:
# 关闭游标对象
cur.close()
# 关闭连接
conn.close()
-
实现封装功能函数
查询和增加的功能重复度比较高,均有:导包、连接mysql、创建游标对象、执行SQL语句、Try语句。实现封装函数的操作:
① 定义封装函数的名字
② 粘贴重复度高的代码
③ 提交参数
④ 测试
# 1 导包
import pymysql
# 封装一个执行CRUD的函数
def execute_crud_sql(sql,data):
# 2 链接mysql的服务端
conn = pymysql.Connect(
user='root',
password='mysql',
host='192.168.90.172',
database='orders',
port=3306,
charset='utf8'
)
# 3 创建游标对象
cur = conn.cursor()
try:
# 4 编写 查询orders表的所有数据SQL
# 5 使用游标对象执行SQL
cur.execute(sql, data)
# 6 提交操作
conn.commit()
# 8 加个try优化下
except Exception as e:
print('报错信息:', e)
# 回滚数据
conn.rollback()
finally:
# 关闭游标对象
cur.close()
# 关闭连接
conn.close()
def add(data):
sql = 'insert into orders values(%s,%s,%s,%s,%s,%s,%s)'
execute_crud_sql(sql, data)
-
实现订单修改和删除功能
# 修改的SQL语句
update orders set count=%s, price=%s, freight=%s, user=%s, status=%s, time=%s where id=%s;
# 将参数中id的顺序从第一个颠倒到最后一个
data.append(data.pop(0))
# 删除的SQL语句
delete from orders where id=%s;
# 1 导包
import pymysql
# 封装一个执行CRUD的函数
def execute_crud_sql(sql,data):
# 2 链接mysql的服务端
conn = pymysql.Connect(
user='root',
password='mysql',
host='192.168.90.172',
database='orders',
port=3306,
charset='utf8'
)
# 3 创建游标对象
cur = conn.cursor()
try:
# 4 编写 查询orders表的所有数据SQL
# 5 使用游标对象执行SQL
cur.execute(sql, data)
# 6 提交操作
conn.commit()
# 8 加个try优化下
except Exception as e:
print('报错信息:', e)
# 回滚数据
conn.rollback()
finally:
# 关闭游标对象
cur.close()
# 关闭连接
conn.close()
# 修改订单数据
def update(data):
print('修改的数据:', data) # ['7', '99', '9.90', '1.00', '明明','待收货', '2020-03-29']
data.append(data.pop(0))
# 1. 修改的SQL语句
sql = 'update orders set count=%s, price=%s, freight=%s, user=%s, status=%s, time=%s where id=%s'
# 2. 执行调用
execute_crud_sql(sql, data)
# 删除订单数据
def delete(data):
# 1. 删除的SQL数据
sql = 'delete from orders where id=%s;'
# 2. 执行
execute_crud_sql(sql, data)