import pymysql
import time
class Count:
def __init__(self, count_name, count_password, money=0, id=0):
self.id = id
self.cont = count_name
self.count_password = count_password
self.money = 0
class ShopContrller:
__init = 0
def __init__(self):
self.db = pymysql.connect(host='127.0.0.1', user='root', password='123456', port=3306, database='shop')
self.cursor = self.db.cursor()
def add_count(self, count):
count.id = self.__generate_id()
print(count.id)
nowtime = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime())
sql = """insert into user values ({count},\'{name}\',\'{password}\',\'{money}\',\'{nowtime}\');""".format(
count=count.id, name=count.cont, password=count.count_password,money=count.money, nowtime=nowtime)
self.cursor.execute(sql)
self.db.commit()
def __generate_id(self):
"""
id自增一方法
:return:
"""
self.cursor.execute('select count from count')
self.count = self.cursor.fetchall()[0][0]
self.count+=1
self.cursor.execute('update count set count={allcount}'.format(allcount=self.count))
self.db.commit()
return self.count
class classManagerView:
def __init__(self):
self.__manager = ShopContrller()
def __display_memu(self):
print("1)添加账号")
print("2)显示全部账号")
print("3)删除账号")
print("4)修改账号密码")
print("5)充值金币")
def __select_memu(self):
item = input("请输入选择:")
if item == "1":
self.__input_stu()
elif item == "2":
self.__outprint(self.__manager)
elif item == "3":
self.__del_stu()
elif item == "4":
self.__modify_student()
elif item == "5":
self.__ascend_score()
def __input_stu(self):
name = input("请输入账号:")
password = input("请输入密码:")
count = Count(name, password)
self.__manager.add_count(count)
print(f'添加账号{name}成功')
def __outprint(self,manager):
sql = "select * from user"
manager.cursor.execute(sql)
x = manager.cursor.fetchall()
print("--------------------------------------")
print("id name pass money now")
for i in x:
print(str(i[0])+' '*3,i[1]+' '*3,i[2]+' '*3,i[3]+' '*3,str(i[4])+' '*3)
print("--------------------------------------")
def __ascend_score(self):
money = int(input("请输入需要充值的金币"))
id =int(input("请输入要充值的id"))
self.__manager.cursor.execute(f'select money from user where id={id}')
ex_money = int(self.__manager.cursor.fetchall()[0][0])
money+=ex_money
sql = "update {table} set money={money} where id={id}".format(table='user', money=money, id=id)
self.__manager.cursor.execute(sql)
self.__manager.db.commit()
print('账号充值成功')
def __del_stu(self):
id = int(input("请输入需要删除的账号id"))
sql = "delete from {table} where id={id}".format(table='user',id=id ) # 删除数据
self.__manager.cursor.execute(sql)
self.__manager.db.commit()
print('删除成功')
def __modify_student(self):
id = int(input('请输入要修改密码的账号id:'))
password = input(":请输入要修改的密码:")
sql = "update {table} set password={password} where id={id}".format(table='user', password=password, id=id)
self.__manager.cursor.execute(sql)
self.__manager.db.commit()
print('修改成功')
def main(self):
while True:
self.__display_memu()
self.__select_memu()
if __name__ == "__main__":
view = classManagerView()
view.main()
基于控制台实现的一个充值系统
最新推荐文章于 2024-08-29 17:59:52 发布