#Python 用Sqlite3做模拟银行系统(2)

该文接着上一篇直接上干货:

#系统基本操作4-创建转账功能:

def transfer(from_account, to_account, amount, note=None):
    conn = sqlite3.connect(‘bank_system.db’)
    cursor = conn.cursor()
    cursor.execute(”’
    SELECT balance FROM accounts WHERE account_name = ?
    ”’, (from_account,))
    from_balance = cursor.fetchone()
    if from_balance and from_balance[0] >= amount:
        cursor.execute(”’
        UPDATE accounts SET balance = balance – ? WHERE account_name = ?
        ”’, (amount, from_account))
        cursor.execute(”’
        UPDATE accounts SET balance = balance + ? WHERE account_name = ?
        ”’, (amount, to_account))
        cursor.execute(”’
        INSERT INTO transactions (account_name, ty

作者:Flash The Dash

物联沃分享整理
物联沃-IOTWORD物联网 » #Python 用Sqlite3做模拟银行系统(2)

发表回复