登录,注册,下单

import pymysql

# from pymysql import connect

class wigglee(object):

def __init__(self):

self.is_login = False

# 创建Connection连接

self.conn = pymysql.connect(host='localhost',port=3306,database='wiggle',user='root',password='123456',charset='utf8')

# 获得Cursor对象

self.cursor = self.conn.cursor()

# 关闭cursor对象与连接 是个长连接

def __del__(self):

#关闭Cursor对象

self.cursor.close()

    # 关闭Connection对象

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没有这个字段

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 get_info_by_name(self):

find_name = input("请输入要查询的商品的名字:")

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("请输入您要删除的id号:"))

sql = """delete from goods_cates where id=("%d");"""%del_id

self.cursor.execute(sql)

self.conn.commit()

def register(self):

username = input('请输入用户名:')

pwd = input('请输入密码:')

addr = input('请输入地址:')

tel = input('请输入电话:')

sql = 'insert into customers values(0,%s,%s,%s,password(%s));'

self.cursor.execute(sql,[username,addr,tel,pwd])

self.conn.commit()

print('注册成功!!')

def login(self):

username = input('请输入用户名:')

pwd = input('请输入密码:')

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('请输入商品的编号:')

num = input('请输入商品的数量:')

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 >>> 注册')

print('2 >>> 登录')

print('3 >>> 退出')

return input('请输入功能对应的序号:')

@staticmethod

def print_menu():

print('------wiggle------')

print('1:看所有商品')

print('2:所有商品分类')

print('3:所有商品品牌')

print('4:增加数据')

print('5:查询商品')

print('6:删除商品')

print('7:下单')

return input('请输入功能对应的序号: ')

def run(self):

while True:

num = self.sys_menu()

if num == '1':

# 注册

self.register()

elif num == '2':

# 登录

if self.login():

print('登录成功!')

break

else:

print('用户名或密码输入错误,请重新输入....')

elif num == '3':

break

else:

print('输入有误,请重新输入....')

if self.is_login:

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_cates()

elif num == '5':

self.get_info_by_name()

elif num =='6':

self.delete_cates()

elif num == '7':

self.create_order()

else:

print('--------再见--------')

break

else:

print('--------再见--------')

def main():

# 创建一个项目商城对象

wiggle = wigglee()

# 调用这个对象的run方法,让其运行

wiggle.run()

if __name__ == '__main__':

main()

你可能感兴趣的:(登录,注册,下单)