python 中的 sqlite withdraw/deposit 函数

sqlite withdraw/deposit function in python

我一直在处理的银行项目的最后一部分是 deposit/withdraw 函数。我已经让其他大部分工作(清理代码之外)我不完全理解如何从 python 内的 sql 语句中添加和减去一个数字...所以这是我正在努力处理的代码部分: 这是我的表格:

sqlite_file  = 'banking2_db.sqlite'
table_1      = 'Bartertown'
table_2      = 'PINs'
id_column    = 'Card_Numbers'
column_1     = 'Character_PINs'
column_2     = 'Balances'
column_3     = 'Card_Numbers'
column_4     = 'Characters'

class 登录提示: 定义登录(自己): 而真实的: 打印(菜单[1]) self.Card_number=str(输入('>>')) 打印(菜单[2])

        while True:
            try:
                self.Character_PINs = getpass.getpass('>>  ')
                self.one_row = c.execute('SELECT * FROM {tn} WHERE {cn}=? and {cnn}=?'.\
                            format(tn=table_1, cn=column_1, cnn=column_3), (self.Character_PINs, self.Card_number,))
                for row in self.one_row.fetchone():
                    print('Welcome: ', row)
                    input('Press any key to continue... ')
                    return
            except:
                    print('PIN incorrect; try again')
                    break 
            #MINOR ISSUE, ONLY QUERIES CHAR COLUMN
def loginMenu(self):
    while True:
        print(menu[5])
        print("\n1 - Deposit funds")
        print("2 - Withdraw funds")
        print("3 - Check balance")
        print("4 - Reset Pin")
        print("5 - Exit")            

        while True:
            try:
                choice = int(input("Please enter a number: "))
            except ValueError:
                print("Please choose a valid entry")
            if choice >= 1 and choice <=5:              
                choice == 1:
                    amount = input("\nPlease enter the deposit amount: ")
                    if amount != '' and amount.isdigit():
                        int(amount)

                        balance = c.execute('UPDATE {tn} SET {cn} = Balances +:amount WHERE Card_Numbers =:self.Card_number' .\
                                      format(tn=table_1, cn=column_2,))
                        new_bal = balance + (int(amount))
                        print('${} has been deposited to account {} and the new balance is ${}'.\
                              format(amount, self.Card_number, balance + (int(amount))))
                        for row in self.Balances.fetchone():
                            print('Your new balance is: ', new_bal)
                            return self.loginMenu()

基本上,我试图确保程序只能在指定 PIN 和卡号的地方提取余额。它选择天平(这部分工作,它是菜单中的另一个选项)但是,更新功能对我来说仍然是个谜。我了解如何在意外情况下更新整个列......以及如何将余额字段中显示的值更改为用户提交的值,即:用户选择存入 100 个上限,然后他们的余额变为 100 个上限.我要更新的列称为余额,Card_Numbers 是包含用户 "credit card" 的列,金额是用户刚刚输入的值。 感谢您的帮助。

编辑:添加表格,并初始输入数据。

如果您想更新列 Balances 那么您的声明应该是:

...SET Balances = Balances + :amount...

所以这样做:

c.execute("UPDATE " + table_1 + " SET Balances = Balances + ? WHERE Card_Numbers = ?", (amount, self.Card_number,))

终于想通了,傻我

            choice == 1:
                amount = input("\nPlease enter the deposit amount: ")
                if amount != '' and amount.isdigit():
                    int(amount)

                    balance = c.execute('UPDATE {tn} SET {cn} = Balances +:amount WHERE Card_Numbers =:self.Card_number' .\
                                  format(tn=table_1, cn=column_2,))
                    new_bal = balance + (int(amount))

我试图通过在数据库尚未提交时打印其输出来引用它,:eyeroll: 感谢您对 pas 的帮助,它非常有效。