2008-12-31 95 views
-1

我在编写一个程序时遇到了一个小问题。我有一张桌子 - 包含储存在冰箱里的物品的信息(产品,条形码等)。然后,我又有另外一家餐馆,它的作用就像一家商店,里面装载着大量的产品和他们的条形码。目前商店里的一些产品在库存表中,还有一个叫做库存的布尔南领域,它告诉我们是否该产品是否在库存表中,如果它等于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 
+0

你应该改掉这个问题到一些段落......它会很难理解什么实际的问题是。 – 2008-12-31 16:58:21

+0

我可以看到表格名称“插入到atock” - “atock”而不是`stock`中的结构错误,冗余和非工作代码。你能否像Simon建议的那样打破你的问题,并粘贴一些工作代码,如果有的话。另外,如果合适,我会建议使用ORM而不是DBAPI。 – 2008-12-31 17:17:46

回答

0

尽量使问题更短。我猜,这会产生更多的回应。

1

所以,如果我说对你有如下表:

股票至少一个条形码数量

至少一个条码和一个股票

我不明白为什么你需要的股票列在店内表,因为你可以用它来轻松加入这样得到其中有库存产品:

SELECT barcode, ... FROM Stock ST JOIN Shop SH ON ST.barcode = SH.barcode; 

显然你也应该从Shop表中选择一些其他列,否则,您可以从Stock表中选择所有内容。

你可以得到的,其需要订购产品的列表(其中量小于数量):

SELECT barcode FROM Stock ST WHERE ST.amount < ST.quantity; 
相关问题