from pymysql import *
# 在进行删增改操作的时候要进行comit提交
class JD(object):
"""docstring for JD"""
def __init__(self):
self.conn = connect(database = 'jd',password = '123456',charset = 'utf8',host = 'localhost',port = 3306 , user = 'root')
self.cursor = self.conn.cursor()
self.tc = False
def __del__(self):
self.cursor.close()
self.conn.close()
def execute_sql(self,sql):
self.cursor.execute(sql)
for i in self.cursor.fetchall():
print(i)
# 登录
def login(self):
while True:
self.account = input("请输入你的账号")
passwd = input('请输入你的密码')
sql = "select * from customers where tel = '%s'" %(self.account)
YZ = self.cursor.execute(sql)
if YZ == 0:
print('您没有登录,请您先注册后在的登录')
opt = input('1:注册,2:退出')
if opt == '1':
self.register()
else:
self.tc = True
break
else:
acc = str(self.cursor.fetchone())
b = acc.split(',')
if b[3][1:] == str(self.account) and b[4][2:-2] == str(passwd):
break
else:
print('账号或者密码不正确,请从新输入')
opt = input('1:重输,2:退出')
if opt == '2':
self.tc = True
break
# 注册
def register(self):
name = input('请输入你的用户名')
address = input("请输入你的地址")
tel = int(input('请输入你的账号:以后该手机号将作为你的账号进行登录'))
passwd = input('请输入你的密码:')
passwd2 = input('请您重新在输入一次密码')
if passwd == passwd2:
sql = 'insert into customers values (null,"%s","%s",%d,"%s")' %(name,address,tel,passwd)
self.cursor.execute(sql)
self.conn.commit()
else:
print('两次密码输入不正确,请您重新输入')
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_crands(self):
sql = 'select name from goods_brands;'
self.execute_sql(sql)
def add_cates(self):
item_name = input('请输入新商品分类的名称;')
sql = """insert into goods_cates(name) values ('%s'); """ %item_name
self.cursor.execute(sql)
self.conn.commit()
def del_cate(self):
item_name = input('请输入你要删除的商品分类的\n')
sql = """ delete from goods_cates where name = ('%s')""" %item_name
self.cursor.execute(sql)
self.conn.commit()
def update_cates(self):
while True:
item_name = input('请输入你要修改的数据\n')
data = input('请输入你要更新的数据\n')
try:
sql = """ upate goods_cates set name = ('%s') where name = '%s'""" %(data,item_name)
self.cursor.execute(sql)
self.conn.commit()
break
except:
print('没有这个数据,请检查从新输入')
def find_shop(self):
try:
shop = input('请输入你要查找的商品的名字\n')
# sql = """ select * from goods where name = '%s'""" %shop
# self.execute_sql(sql)
# 防止sql注入
sql = " select * from goods where name = %s"
self.cursor.execute(sql,[shop])
print(self.cursor.fetchall())
except:
shop = False
return shop
def buy_shop(self):
# 往订单表里面添加数据 orders
sql = 'select id from customers where tel = %s' %self.account
self.cursor.execute(sql)
id = str(self.cursor.fetchone())
id = id[1:-2]
# print(id[1:-2])
# 将顾客表customers 的id填到订单表里面
sql = 'insert into orders( customer_id) values ("%s")'%id
self.cursor.execute(sql)
# 获取订单表自己的id
sql = 'select id from orders where customer_id = "%s"' %id
self.cursor.execute(sql)
orders_id = str(self.cursor.fetchone())
orders_id = orders_id[1:-2]
# print(orders_id)
# 往订单详情表里面添加数据
num = int(input('请输入你要购买的个数'))
shop = input('请输入你要购买的商品')
# 查找商品表的ID
sql = 'select id from goods where name = "%s"'%(shop)
# print(sql)
self.cursor.execute(sql)
goods_id = str(self.cursor.fetchone())
goods_id = goods_id[1:-2]
# print(type(goods_id))
# print(goods_id)
# 查看商品表里面有没有这个商品,如果有返回他的id,如果没有就会返回o
if goods_id == 'o':
print('请您检查您要购买的商品,该商店没有此商品')
# print("这里是商品的id",goods_id)
# print(goods_id)
# print(goods_id[1:-2])
else:
# 往订单详情表里面添加数据
sql = 'insert into order_detail values ( null ,"%s", "%s", %d)' %(orders_id,goods_id,num)
# print("------>",sql,"<-----")
# print(type(orders_id))
# print(type(goods_id))
self.cursor.execute(sql)
self.conn.commit()
return
def add_brand(self):
brand = input('请输入你要增加的品牌')
sql = "insert into goods_brands(name) values ('%s')" %brand
print(sql)
self.cursor.execute(sql)
self.conn.commit()
def minus_brand(self):
brand = input('请输入你要删除的品牌')
sql = "delete from goods_brands where name = '%s' " %brand
print(sql)
self.cursor.execute(sql)
self.conn.commit()
def update_brand(self):
brand = input('请输入你要更改的品牌')
new_brand = input('请输入你更改后的品牌')
sql = 'update goods_brands set name = "%s" where name = "%s" '%(brand,new_brand)
print(sql)
self.cursor.execute(sql)
self.conn.commit()
@staticmethod
def print_menu():
print('----京东----')
print('1:所有的商品')
print('2:所有的商品分类')
print('3:所有的商品品牌分类')
print('4:插入商品分类的名称')
print('5:删除商品分类的名称')
print('6:更改商品分类的名称')
print('7:根据名字查询商品')
print('8:购买商品')
# input收到的数据是字符串
print('9:添加品牌')
print('10:删除品牌')
print('11:更新品牌')
print('12:退出')
num = input('请输入功能相对应的序号:\n')
return num
def run(self):
self.login()
while True:
if self.tc :
break
else:
num = self.print_menu()
if num == '1':
self.show_all_items()
elif num == '2':
# 查询分类
self.show_cates()
elif num == '3':
# 查询品牌分类
self.show_crands()
elif num == '4':
self.add_cates()
elif num == '5':
self.del_cate()
elif num == '6':
self.update_cates()
elif num == '7':
self.find_shop()
elif num == '8':
self.buy_shop()
elif num == '9':
self.add_brand()
elif num == '10':
self.minus_brand()
elif num == '11':
self.update_brand()
elif num == '12':
# print(self.tc)
break
else:
print('输入有误,请重新输入……')
def main():
jd = JD()
jd.run()
if __name__ == '__main__':
main()