如何使用sqlite将变量放入UPDATE语句?

时间:2016-03-29 12:33:30

标签: python sqlite

我已经输入了我认为与问题相关的所有代码,而我的问题是if语句中的最后4行。我试过定义术语'作为一个不同的变量,如果问题是两个函数之间的转移,但这仍然无法正常工作。我也在互联网上环顾四周,但所有的答案似乎只与设置值放入语句而不是变量有关。

    def FindItem(n = ''):
        #Finds products by GTIN
        shopping = 'yes'
        while shopping == 'yes':
            find("GTIN8", getNum("Enter the GTIN-8 of the product that you want: "))
            shopping = input('do you want to add any more Items to your shop? ')
        print('This is your recipt:')
        recipt = open('temp.txt')
        for eachline in recipt:
            print(eachline, end = '')
        recipt.close()
        return os.remove('temp.txt')

    def find(search, term):
        # Helper function for the find function.
        with connect("GTINb.db") as db:
            cursor = db.cursor()
            sql = "select * from GTINb where {} = ?".format(search)
            cursor.execute(sql,(term,))
            db.commit()
            results = cursor.fetchall()
            if results:
                results = str(results)
                temp = open('temp.txt', 'a')
                temp.write(results)
                temp.write('\n')
                temp.close
                with connect("GTINb.db") as db:
                    sql = "UPDATE GTINb SET stockcur=stockcur-1 WHERE GTIN8=term"
                    cursor.execute(sql)
                return
            else:
                temp = open('temp.txt', 'a')
                temp.write('Product not found')
                temp.write('\n')
                temp.close
                return 

0 个答案:

没有答案
相关问题