MySQL与python的交互!Python就得这样学!

create database jing_dong charset=utf-8;

use jing_dong;

MySQL与python的交互!Python就得这样学!

创建商品goods的数据表;

mysql> create table goods(

-> id int unsigned primary key auto_increment not null,

-> name varchar(150) not null,

-> cate_name varchar(40) not null,

-> brand_name varchar(40) not null,

-> price decimal(10,3) not null default 0,

-> is_show bit not null default 1,

-> is_saleoff bit not null default 0

-> );

MySQL与python的交互!Python就得这样学!

select * from goods;

MySQL与python的交互!Python就得这样学!

创建商品分类表

create table if not exists goods_cates(

id int unsigned primary key auto_increment,

name varchar(40) not null

);

MySQL与python的交互!Python就得这样学!

insert into goods_cates(name) select cate_name from goods group by cate_name;

MySQL与python的交互!Python就得这样学!

MySQL与python的交互!Python就得这样学!

修改外键,添加外键约束

alter table goods add foreign key (cate_id) references goods_brands(id);

使用Python DB API访问数据库流程

开始

创建connection

获取cursor

执行查询等命令

关闭cursor

关闭connection

结束

# -*- coding:utf-8 -*-
# Author : XuQiao
from pymysql import *
class JD(object):
 def __init__(self):
 # 创建connection连接
 self.conn = connect(host='localhost',port=3306,user='root',password='123456',database='jing_dong',charset='utf8')
 # 获取cursor对象
 self.cursor = self.conn.cursor()
 def __del__(self):
 # 关闭cursor对象
 self.cursor.close()
 self.conn.close()
 def execute_sql(self,sql):
 self.cursor.execute(sql)
 for temp in self.cursor.fetchall():
 print(temp)
 def show_all_items(self):
 # 显示所有的商品
 sql = "select * from goods;"
 self.execute_sql(sql)
 def show_cates(self):
 sql = "select name from goods_cates;"
 self.execute_sql(sql)
 def show_brands(self):
 sql = "select name from goods_brands;"
 self.execute_sql(sql)
 def add_brands(self):
 item_name = input("请输入新商品分类的名称:")
 sql = "insert into goods_brands(name) values('%s')" % item_name
 self.cursor.execute(sql)
 self.conn.commit()
 def get_info_by_name(self):
 find_name = input("请输入要查询商品的名字:")
 # sql = "select * from goods where name='%s';" % find_name
 # print("----------->%s<----------"%sql)
 # self.execute_sql(sql)
 sql = "select * from goods where name=%s" # 防止sql注入
 self.cursor.execute(sql,[find_name])
 print(self.cursor.fetchall())
 @staticmethod
 def print_menu():
 print("------京东------")
 print("1:所有的商品")
 print("2:所有的商品分类")
 print("3:所有的是商品品牌分类")
 print("4:添加一个商品分类")
 print("5:根据名字查询一个商品")
 return input("请输入功能对应的序号:")
 def run(self):
 while True:
 num = self.print_menu()
 if num=="1":
 # 查询所有商品
 self.show_all_items()
 elif num=="2":
 # 查询分类
 self.show_cates()
 elif num=="3":
 # 查询品牌分类
 self.show_brands()
 elif num=="4":
 # 添加数据
 self.add_brands()
 elif num=="5":
 # 根据名字查询商品
 self.get_info_by_name()
 else:
 print("输入有误,重新输入...")
def main():
 # 1、创建一个京东商城对象
 jd = JD()
 # 2、调用这个对象的run方法,让其运行
 jd.run()
if __name__ == "__main__":
 main()
# conn.commit() commit 提交数据库的修改操作

输出结果

MySQL与python的交互!Python就得这样学!

MySQL与python的交互!Python就得这样学!

MySQL与python的交互!Python就得这样学!

MySQL与python的交互!Python就得这样学!

MySQL与python的交互!Python就得这样学!

进群:960410445 即可获取苏十套PDF!

猜你喜欢

转载自blog.csdn.net/qq_42156420/article/details/86063262