【问题标题】:sqlite withdraw/deposit function in pythonpython中的sqlite提款/存款功能
【发布时间】:2020-04-07 06:51:33
【问题描述】:

我从事的银行项目的最后一部分是存款/取款功能。我已经让大部分其他位工作(清理代码之外)米挣扎于: 这是我的桌子:

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'

类登录提示: def 登录(自我): 而真: 打印(菜单[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 是包含用户“信用卡”的列,金额是用户刚刚输入的值。 感谢您的帮助。

编辑:添加表格和数据的初始输入。

【问题讨论】:

    标签: python sqlite banking


    【解决方案1】:

    终于想通了,傻我。

                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: 谢谢你的帮助,它就像一个魅力。

    【讨论】:

      【解决方案2】:

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

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

      这样做:

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

      【讨论】:

      • 我现在得到的只是一个语法错误,稍后会报告。
      • 我并没有变得更好。在“。”附近不断出现语法错误
      • @cooperalan 我认为您的代码在语法上是正确的,但没有进行更新。尝试编辑后的代码。此代码更新表格。
      • 不,这是一个非常简单的sql语句。尝试不使用循环。
      • 我认为你是对的。我现在正在检查它。 nonetype 或 db 被锁定......废话。谢谢你的帮助。
      猜你喜欢
      • 2022-12-11
      • 2016-07-28
      • 2019-11-04
      • 2012-11-25
      • 1970-01-01
      • 2013-12-23
      • 2015-04-29
      • 2020-07-31
      • 2016-06-06
      相关资源
      最近更新 更多