import pymysql
# from pymysql import connect
class wigglee(object):
def __init__(self):
self.is_login = False
# 創(chuàng)建Connection連接
self.conn = pymysql.connect(host='localhost',port=3306,database='wiggle',user='root',password='123456',charset='utf8')
# 獲得Cursor對(duì)象
self.cursor = self.conn.cursor()
# 關(guān)閉cursor對(duì)象與連接 是個(gè)長(zhǎng)連接
def __del__(self):
#關(guān)閉Cursor對(duì)象
self.cursor.close()
? ? # 關(guān)閉Connection對(duì)象
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):
# goods_brands沒(méi)有這個(gè)字段
sql = 'select name from goods_brands;'
self.execute_sql(sql)
def add_cates(self):
item_name = input("請(qǐng)輸入新商品分類(lèi)的名稱(chēng):")
sql = """insert into goods_cates (name) values("%s");""" %item_name
self.cursor.execute(sql)
self.conn.commit()?
def get_info_by_name(self):
find_name = input("請(qǐng)輸入要查詢(xún)的商品的名字:")
sql ="select * from goods where name=%s;"
self.cursor.execute(sql,[find_name])
print(self.cursor.fetchall())
def delete_cates(self):
del_id = int(input("請(qǐng)輸入您要?jiǎng)h除的id號(hào):"))
sql = """delete from goods_cates where id=("%d");"""%del_id
self.cursor.execute(sql)
self.conn.commit()
def register(self):
username = input('請(qǐng)輸入用戶(hù)名:')
pwd = input('請(qǐng)輸入密碼:')
addr = input('請(qǐng)輸入地址:')
tel = input('請(qǐng)輸入電話(huà):')
sql = 'insert into customers values(0,%s,%s,%s,password(%s));'
self.cursor.execute(sql,[username,addr,tel,pwd])
self.conn.commit()
print('注冊(cè)成功!逊桦!')
def login(self):
username = input('請(qǐng)輸入用戶(hù)名:')
pwd = input('請(qǐng)輸入密碼:')
sql = 'select id from customers where name=%s and passwd=password(%s)'
self.uid = self.cursor.fetchone()
if self.uid:
self.uid = self.uid[0]
self.is_login = True
return self.is_login
def create_order(self):
pid = input('請(qǐng)輸入商品的編號(hào):')
num = input('請(qǐng)輸入商品的數(shù)量:')
sql = 'select * from goods where id=%s;'
result = self.cursor.execute(sql,[pid])
if result:
sql = 'insert into orders values(0,now(),%s);'
self.cursor.execute(sql,[self.uid])
sql = 'insert into order_detail values(0,%s,%s,%s,);'
self.cursor.execute(sql,[self.cursor.lastrowid,pid,num])
self.conn.commit()
print('下單成功眨猎!')
@staticmethod
def sys_menu():
print('--------京東---------')
print('1 >>> 注冊(cè)')
print('2 >>> 登錄')
print('3 >>> 退出')
return input('請(qǐng)輸入功能對(duì)應(yīng)的序號(hào):')
@staticmethod
def print_menu():
print('------wiggle------')
print('1:看所有商品')
print('2:所有商品分類(lèi)')
print('3:所有商品品牌')
print('4:增加數(shù)據(jù)')
print('5:查詢(xún)商品')
print('6:刪除商品')
print('7:下單')
return input('請(qǐng)輸入功能對(duì)應(yīng)的序號(hào): ')
def run(self):
while True:
num = self.sys_menu()
if num == '1':
# 注冊(cè)
self.register()
elif num == '2':
# 登錄
if self.login():
print('登錄成功!')
break
else:
print('用戶(hù)名或密碼輸入錯(cuò)誤卫袒,請(qǐng)重新輸入....')
elif num == '3':
break
else:
print('輸入有誤宵呛,請(qǐng)重新輸入....')
if self.is_login:
while True:
num = self.print_menu()
if num == '1':
# 查詢(xún)所有商品
self.show_all_items()
elif num == '2':
# 查詢(xún)分類(lèi)
self.show_cates()
elif num == '3':
# 查詢(xún)品牌
self.show_brands()
elif num == '4':
self.add_cates()
elif num == '5':
self.get_info_by_name()
elif num =='6':
self.delete_cates()
elif num == '7':
self.create_order()
else:
print('--------再見(jiàn)--------')
break
else:
print('--------再見(jiàn)--------')
def main():
# 創(chuàng)建一個(gè)項(xiàng)目商城對(duì)象
wiggle = wigglee()
# 調(diào)用這個(gè)對(duì)象的run方法,讓其運(yùn)行
wiggle.run()
if __name__ == '__main__':
main()