我在編寫一個程序時遇到了一個小問題。我有一張桌子 - 包含儲存在冰箱裏的物品的信息(產品,條形碼等)。然後,我又有另外一家餐館,它的作用就像一家商店,裏面裝載着大量的產品和他們的條形碼。目前商店裏的一些產品在庫存表中,還有一個叫做庫存的布爾南領域,它告訴我們是否該產品是否在庫存表中,如果它等於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