(1)mysql数据库数据表:jd(商品数据总库),customer(所有用户信息),detail_order(每次购买的数据商品信息)、pp(所有品牌的信息取两个字母)、goods_cate(所有商品的种类)、buy(每个订单的信息)
(2)windows10 下python开发环境
from pymysql import connect
import datetime
class JD(object):
def __init__(self):
self.con=connect(host="localhost",port=3306,user="root",database="python_jd",charset="utf8")
self.cur=self.con.cursor()
def __del__(self):
#关闭mysql以及游标
self.cur.close()
self.con.close()
def all_many_good(self):
#查询所有商品信息
sql="select * from jd;"
self.show_data(sql)
def show_data(self,sql):
#处理所有的查询信息
self.cur.execute(sql)
for t in self.cur.fetchall():
print(t)
def cate_name(self):
#查询所有的商品种类
sql="select * from goods_cate;"
self.show_data(sql)
def pp(self):
#查询所有的商品的品牌
sql="select * from pp;"
self.show_data(sql)
def insert_data(self):
#添加一条商品分类
add=input("请输入添加的分类:")
sql="insert into pp (name) values ('%s')"%add
self.cur.execute(sql)
self.con.commit()
print("添加成功")
def insert_one_data(self):
#查询一条信息
name=input("请输入您要查询的商品:")
sql="select * from jd where name=%s;"
self.cur.execute(sql,[name])
print(self.cur.fetchall())
def buy_com(self,custom_id):
# 用户购买需求以及用户购买信息录入
name=input("请输入您的购买:")
sql="select * from jd where name=%s;"
self.cur.execute(sql,[name])
goods_data=self.cur.fetchone()
print(goods_data)
if goods_data is None:
print("没有此产品请重新选择您的购买!")
self.buy_com(custom_id)
else:
print("这是您的购买产品信息:",goods_data)
n=input("您确定好了吗?(y/n)")
if n=="y":
goods_id=goods_data[0]
time=datetime.datetime.now()
self.cur.execute("insert into buy values(null,%s,%s,%s);",[time,custom_id,name])
self.con.commit()
self.cur.execute("select * from buy where name=%s;",name)
buy_id=self.cur.fetchone()[0]
print(buy_id)
self.cur.execute("insert into detail_order values(null,%s,%s);",[buy_id,goods_id])
self.con.commit()
print("购买成功!感谢您的支持!欢迎下次光临")
else:
self.buy_com(id)
@staticmethod
def menu(name):
print("="*30+"欢迎来到京东!"+name+"="*30)
print("----京东电脑自选-----")
print("1、所有电脑")
print("2、所有的电脑分类")
print('3、所有的电脑品牌分类')
print('4、添加一个电脑品牌分类')
print('5、查询一个电脑信息')
print('6、请输入您的购买')
print('7、退出')
return input("请输入您的需求:")
def run(self,custom_id,name):
#用户登录主界面
while True:
xu=self.menu(name)
if xu == "1":
self.all_many_good()
elif xu == "2":
self.cate_name()
elif xu == "3":
self.pp()
elif xu == "4":
self.insert_data()
elif xu == "5":
self.insert_one_data()
elif xu == "6":
self.buy_com(custom_id)
elif xu=="7":
self.login()
else:
print("无此项功能,请重新输入----")
self.run(custom_id,name)
def sign_in(self,tel,pwd):
#用户信息查询
self.cur.execute("select id,name,tel,password from customer where tel=%s;",[tel])
return self.cur.fetchone()
def Registered_in(self,name,pwd,tel,address):
#用户信息录入
self.cur.execute("insert into customer values(null,%s,%s,%s,%s);",[name,pwd,tel,address])
self.con.commit()
def login(self):
#实现登录注册
print("="*30+"亲爱的用户,欢迎来到京东"+"="*30)
print("您尚未登录,请您先登录!")
print("请选择:1、登录 2、注册")
custom= input("请选择:")
if custom=="1":
tel=input(" 请输入用户名(tel):")
pwd=input(" 请输入密码(至少6位密码):")
data_sql=self.sign_in(tel,pwd)
data_tel="%s"%tel
data_pwd="%s"%pwd
data=(data_tel,data_pwd)
custom_id=data_sql[0]
name=data_sql[1]
if data_tel==data_sql[2] and data_pwd==data_sql[3]: #对用户的信息进行判断
print("登录成功!")
jd=JD()
jd.run(custom_id,name)
else:
print("对不起!登陆失败!请重试")
self.login()
elif custom=="2":
name=input(" 请输入您注册用户名(tel\email):")
pwd=input(" 请输入注册密码(至少6位密码):")
tel=input(" 请输入您的注册手机号:")
address=input(" 请输入您的地址:")
self.Registered_in(name,pwd,tel,address)
print("注册成功")
self.login()
def main():
jd=JD()
jd.login()
jd.run()
if __name__ == "__main__":
main()