直接上干货:
#引用各大库以及模块:
from sre_constants import _NamedIntConstant
import tkinter as tk
from tkinter import messagebox, Toplevel
import sqlite3
import datetime
#系统基本操作1-创建记忆储存:
def create_account():
conn = sqlite3.connect('bank_system.db')
cursor = conn.cursor()
cursor.execute('''
CREATE TABLE IF NOT EXISTS accounts (
id INTEGER PRIMARY KEY,
account_name TEXT NOT NULL UNIQUE,
balance REAL NOT NULL
)
''')
cursor.execute('''
CREATE TABLE IF NOT EXISTS transactions (
id INTEGER PRIMARY KEY,
account_name TEXT NOT NULL,
type TEXT NOT NULL,
amount REAL NOT NULL,
note TEXT,
timestamp DATETIME DEFAULT CURRENT_TIMESTAMP
)
''')
conn.commit()
conn.close()
#系统基本操作2-创建存钱功能:
def deposit(account_name, amount, note=None):
conn = sqlite3.connect('bank_system.db')
cursor = conn.cursor()
cursor.execute('''
INSERT OR REPLACE INTO accounts (account_name, balance) VALUES (?, COALESCE((SELECT balance FROM accounts WHERE account_name = ?), 0) + ?)
''', (account_name, account_name, amount))
cursor.execute('''
INSERT INTO transactions (account_name, type, amount, note) VALUES (?, 'deposit', ?, ?)
''', (account_name, amount, note))
conn.commit()
conn.close()
#系统基本操作3-创建取钱功能:
def withdraw(account_name, amount, note=None):
conn = sqlite3.connect('bank_system.db')
cursor = conn.cursor()
cursor.execute('''
UPDATE accounts SET balance = balance - ? WHERE account_name = ?
''', (amount, account_name))
cursor.execute('''
INSERT INTO transactions (account_name, type, amount, note) VALUES (?, 'withdraw', ?, ?)
''', (account_name, amount, note))
conn.commit()
conn.close()
这四个部分只是这段代码的一小部分。下一篇将会接着讲系统的基本配置哦!
喜欢的话,请关注我!
标签:note,account,name,Python,cursor,amount,conn,Sqlite3,模拟 From: https://blog.csdn.net/2301_78933620/article/details/145268337