我正在编写一个与 MySQL 数据库交互的程序,但我遇到了问题。如您所见,我编写了一个查询,它将在产品表中查找与用户输入的条形码相对应的产品。
如果在产品表中找到用户输入的条形码,我想在股票表中将“金额”字段增加 1,其中与输入的条形码对应的产品与股票中的产品相同桌子。
如您所见,我尝试将变量分配给 for 循环以尝试使其以这种方式工作,但它不起作用。有人知道怎么做吗?
import MySQLdb
def look_up_product():
db = MySQLdb.connect(host='localhost', user = 'root', passwd='$$', db='fillmyfridge')
cursor = db.cursor (MySQLdb.cursors.DictCursor)
user_input=raw_input('please enter the product barcode that you wish to checkin to the fridge: \n')
if cursor.execute("""select * from products where product = %s""", (user_input)):
db.commit()
result_set = cursor.fetchall ()
#i want here to assign a variable to this for loop and the line below = for product in result_set:
print "%s" % (row["product"])
cursor.execute('update stocks set amount = amount + 1 where product = %s', (#here i want the result of the for loop))
db.commit()
else:
print 'no not in products table'
太感谢了。