之前沒(méi)有記著邊學(xué)邊寫(xiě)筆記,沒(méi)有這樣的習(xí)慣蛤织,現(xiàn)在來(lái)培養(yǎng)下赴叹。之前寫(xiě)了不少了,先貼上代碼指蚜,之后再來(lái)補(bǔ)上具體的其他的乞巧。而且對(duì)于這里的筆記,不在考慮結(jié)構(gòu)內(nèi)容啥的摊鸡,寫(xiě)完一個(gè)功能就會(huì)來(lái)總結(jié)下绽媒。還會(huì)有些突發(fā)奇想在里面,就像是得知自己癌癥晚期后免猾,想要記錄自己的一切的那種想法是一樣的想法是辕。
User類(lèi)
將所有的屬性設(shè)置為非公開(kāi)的屬性,然后寫(xiě)get和set公開(kāi)方法來(lái)提供公共的借口猎提,但是我還沒(méi)有寫(xiě)完获三,
class User:
"""用戶(hù)"""
def __init__(self, name: str, number: str, password: str, balance: float):
self._user_name = name
self._user_number = number
self._password = password
self._balance = balance
def get_name(self) -> str:
return self._user_name
def set_name(self, new_name) -> bool:
self._user_name = new_name
return True
def get_number(self) -> str:
return self._user_number
def get_balance(self) -> float:
return self._balance
def set_balance(self, new_balance):
self._balance = new_balance
return True
def get_password(self):
return self._password
def __str__(self):
s = "用戶(hù)名:" + self.get_name() + "\t卡號(hào):" + self.get_number() + \
"\t余額:" + str(self.get_balance())
return s
if __name__ == '__main__':
user = User("張三", "123421", "124422", 12.0)
print(user)
Item類(lèi)
from datetime import datetime
from User import User
class Item:
"""定義日志類(lèi)"""
def __init__(self, user, type, money, status):
self._time = str(datetime.now())
self._user = user
self._type = type
self._money = money
self._status = status
def get_time(self):
return self._time
def get_user(self):
return self._user
def get_type(self):
return self._type
def get_money(self):
return self._money
def get_status(self):
return self._status
def __str__(self):
s = "[" + self.get_time() + "] " + self.get_user().get_name()\
+ " | " + self.get_type() + " | " + self.get_money() \
+ " | " + self.get_status()
return s
if __name__ == '__main__':
user = User("DK", "1433223", "123456", 1000)
item = Item(user, "存款", 100, "成功")
print(item)
DBHelper類(lèi)
這類(lèi)還挺復(fù)雜的
from config import *
import pymysql as msql
from Item import Item
from User import User
class DBHelper:
"""數(shù)據(jù)庫(kù)助手,存數(shù)據(jù)锨苏,取數(shù)據(jù)"""
# -----------------------------默認(rèn)直接鏈接數(shù)據(jù)庫(kù)-------------------------
def __init__(self):
self.connect = msql.connect(host=MYSQL_HOST, user=MYSQL_USER,
port=MYSQL_PORT, password=MYSQL_PASSWORD,
db=MYSQL_DATABASE,
charset=MYSQL_DATABASE_CHARSET)
self.cursor = self.connect.cursor()
# ----------------------user對(duì)象的增刪改查--------------------------------
def get_user(self, number: str) -> User:
"""查"""
self.connect.ping(reconnect=True)
sql = 'SELECT * FROM user ' \
'WHERE number=(%s)' % number
# self.connect.begin()
self.cursor.execute(sql)
result = self.cursor.fetchone()
if result:
user = User(number=result[0], name=result[1], password=result[2],
balance=result[3])
return user
def insert_user(self, user: User):
"""增"""
number = user.get_number()
name = user.get_name()
password = user.get_password()
balance = user.get_balance()
# print(number, name, password, balance) #
self.connect.ping(reconnect=True)
sql = "INSERT INTO user(number, name疙教,password, balance)" \
"VALUES('%s', '%s', '%s', '%f')"\
% (number, name, password, balance)
try:
self.connect.begin()
self.cursor.execute(sql)
self.connect.commit()
except Exception as e:
print("新用戶(hù)注冊(cè)失敗")
print(e.args)
self.connect.rollback()
return False
return True
def del_user(self, number: str):
"""刪"""
sql = " DELETE FROM user WHERE number=%s" % number
try:
self.connect.begin()
self.cursor.execute(sql)
self.connect.commit()
except Exception as e:
print("用戶(hù)銷(xiāo)戶(hù)失敗")
print(e.args)
self.connect.rollback()
return False
return True
def update_user(self, change_item: str, change_value: str):
"""改"""
sql = "UPDATE user SET " + change_item + "=" + change_value
try:
self.connect.begin()
self.cursor.execute(sql)
self.connect.commit()
except Exception as e:
print("用戶(hù)信息更改失敗")
print(e.args)
self.connect.rollback()
return False
return True
# --------------------item記錄的增刪查-----------------------------
def insert_log(self, item: Item):
"""新加一條記錄"""
number = item.get_user().get_number()
time = item.get_time()
name = item.get_user().get_name()
type = item.get_type()
money = item.get_money()
status = item.get_status()
balance = item.get_user().get_balance()
sql = "INSERT INTO log (number, time, name, type, money, status, " \
"balance)" \
"VALUES('%s', '%s', '%s','%s', '%f', '%s', '%f')" \
% (number, time, name, type, money, status, balance)
try:
self.connect.begin()
self.cursor.execute(sql)
self.connect.commit()
except Exception as e:
print("日志記錄失敗")
print(e.args)
self.connect.rollback()
return False
return True
def del_all_logs(self, number):
"""刪除用戶(hù)的所有記錄,僅用于銷(xiāo)戶(hù)"""
sql = "DELETE FROM lOG WHERE numbers="+number
try:
self.connect.begin()
self.cursor.execute(sql)
self.connect.commit()
except Exception as e:
print("刪除用戶(hù)操作記錄失敗")
self.connect.rollback()
print(e.args)
return False
return True
# --------------------關(guān)閉數(shù)據(jù)庫(kù)的連接-------------------------------
def close(self):
self.connect.close()
if __name__ == '__main__':
db = DBHelper()
user = User("張三", "123321", "971221", 1200)
item = Item(user, "存款", 1000, "成功")
db.insert_log(item)
db.close()
除此之外伞租,還有數(shù)據(jù)庫(kù)的相關(guān)配置贞谓,用來(lái)連接數(shù)據(jù)庫(kù)。
# 用來(lái)保存相關(guān)的設(shè)置葵诈,數(shù)據(jù)庫(kù)的相關(guān)信息等
MYSQL_HOST = 'localhost'
MYSQL_PORT = 3306
MYSQL_PASSWORD = 'password'
MYSQL_USER = 'root'
MYSQL_DATABASE = 'atm'
MYSQL_DATABASE_USER = "user"
MYSQL_DATABASE_LOG = 'log'
MYSQL_DATABASE_CHARSET = 'utf8'