我在编写一个程序时遇到了一个小问题。我有一张桌子 - 包含储存在冰箱里的物品的信息(产品,条形码等)。然后,我又有另外一家餐馆,它的作用就像一家商店,里面装载着大量的产品和他们的条形码。目前商店里的一些产品在库存表中,还有一个叫做库存的布尔南领域,它告诉我们是否该产品是否在库存表中,如果它等于1,那么它在冰箱中,如果它等于0,则它不在冰箱中。库存表中的两个字段是金额和数量。数量是目前在冰箱里的数量,数量是冰箱里随时都有的。从冰箱中取出物品时,该产品的金额将下降1.库存表中的每个条形码在商店表中都有相应的条形码。我需要从一个python程序中查询数据库,当数量(冰箱里的物品)小于数量(冰箱里所有时间意味着什么)时,它将从商店表中订购产品。因此,您需要获取库存表中行数小于数量的行的条形码,并与商店表中的条形码匹配。然后在该匹配条码的行中,您需要设置库存= 1.使一个表的值等于另一个表中的另一个值
如果有人可以帮助我,我真的很高兴,因为我真的很难写这个函数。如果有帮助,以下是签入和签出功能。
签
def check_in():
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')
cursor.execute("""update shop set stock = 1 where barcode = %s""", (user_input))
db.commit()
numrows = int(cursor.rowcount)
if numrows >= 1:
row = cursor.fetchone()
print row["product"]
cursor.execute('update stock set amount = amount + 1 where product = %s', row["product"])
db.commit()
cursor.execute('udpate shop set stock = 1 where barcode = user_input')
db.commit()
else:
new_prodname = raw_input('what is the name of the product and press enter: \n')
cursor.execute('insert into shop (product, barcode, category) values (%s, %s, %s)', (new_prodname, user_input, new_prodname))
cursor = db.cursor()
query = ('select * from shop where product = %s', (new_prodname))
cursor.execute(query):
db.commit()
numrows = int(cursor.rowcount)
if numrows<1:
cursor.execute('insert into atock (barcode, quantity, amount, product) values (%s, 1, 1, %s)', (user_input, new_prodname))
db.commit()
cursor.execute('insert into shop (product, barcode, category, stock) values (%s, %s, %s, 1)', (new_prodname, user_input, new_prodname))
print new_prodname
print 'has been added to the fridge stock'
else:
cursor.execute('update atock set amount = amount + 1 where product = %s', (new_prodname))
db.commit()
cursor.execute('insert into shop (product, barcode, category, stock) values (%s, %s, %s, 1)', (new_prodname, user_input, new_prodname))
print new_prodname
print 'has been added to the fridge stock'
结账
import MySQLdb
def check_out():
db = MySQLdb.connect(host='localhost', user='root', passwd='$$', db='fillmyfridge')
cursor=db.cursor()
user_input=raw_input('please enter the product barcode you wish to remove from the fridge: \n')
query = cursor.execute('update stock set instock=0, howmanytoorder=howmanytoorder + 1, amount = amount - 1 where barcode = %s', (user_input))
if cursor.execute(query):
db.commit()
print 'the following product has been removed from the fridge nd needs to be ordered'
cursor.execute('update shop set stock = 0 where barcode = %s' (user_input)
db.commit()
else:
return 0
你应该改掉这个问题到一些段落......它会很难理解什么实际的问题是。 – 2008-12-31 16:58:21
我可以看到表格名称“插入到atock” - “atock”而不是`stock`中的结构错误,冗余和非工作代码。你能否像Simon建议的那样打破你的问题,并粘贴一些工作代码,如果有的话。另外,如果合适,我会建议使用ORM而不是DBAPI。 – 2008-12-31 17:17:46