Python 如果没有找到空列,如何取消查询并显示错误?
我有一些噱头表,模拟一个架子,每个架子上都有插座 卷盘ID处的空格在InnoDB上为空Python 如果没有找到空列,如何取消查询并显示错误?,python,mysql,sql,Python,Mysql,Sql,我有一些噱头表,模拟一个架子,每个架子上都有插座 卷盘ID处的空格在InnoDB上为空 Rack_ID|Reel_Socket|Reel_ID|Shelf_ID| -------|-----------|-------|--------| 1| 1| |1 | 1| 2| |1 | 1| 3| |1 | 2|
Rack_ID|Reel_Socket|Reel_ID|Shelf_ID|
-------|-----------|-------|--------|
1| 1| |1 |
1| 2| |1 |
1| 3| |1 |
2| 1|5 |1 |
2| 2|6 |1 |
2| 3|2 |1 |
2| 4| |2 |
此查询将新卷筒“放入”满足要求的第一个可能的空插槽中
query_update_shelf = 'UPDATE Storage_Table SET Reel_ID = %s WHERE Shelf_ID = %s AND Reel_ID IS NULL LIMIT 1'
然后,下一个查询将另一个表中的Quantity列按1递减,该列具有相应的Reel\U ID
所有代码逻辑如下:
from tkinter import *
import mysql.connector as mdb
from tkinter import messagebox
query_reel = "SELECT Reel_Manufacturer_ID FROM Delivery_Table WHERE Reel_Manufactuer_ID = %s"
# this one is kinda redundant because I get Reel_ID from input, Ill delete it later
query_quant = "SELECT Quantity FROM Delivery_Table WHERE Reel_Manufacturer_ID = %s"
'connection here, fetching Reel_ID and Quantity using two cursor.execute'
con = mdb.connect(user=self.user, password=self.pswd, database=self.db)
cur = con.cursor()
cur.execute(query_reel, (reel_input_string,))
reel = cur.fetchall()
reel = reel [0][0] # must be a better way
cur.execute(query_quant, (reel__input_string,))
quant = cur.fetchall()
quant = quant[0][0]
shelf_number = 2 # I get shelf_number from a lookup dict actually
if quant != 0:
quant = quant - 1
query_update_shelf = 'UPDATE Storage_Table SET Reel_ID = %s WHERE Shelf_ID = %s AND Reel_ID IS NULL LIMIT 1'
query_update_shelf_to_enter = (reel, shelf_number)
query_update_quant = 'UPDATE Delivery_Table SET Quantity = %s WHERE Reel_Manufacturer_ID = %s'
quant_query_values_to_enter = (quant, reel)
cur.execute(query_update_shelf, query_update_shelf_to_enter)
cur.execute(query_update_quant, quant_query_values_to_enter)
con.commit()
cur.close()
else
messagebox.showinfo('Query Warning', 'Warning: You have scanned all the reels we have ordered!')
# there must be an error throw code and some queries not related to the issue
所以我注意到,如果没有具有适当Shelf_ID的空列,我的查询就会消失在空气中,数量仍然会减少,基本上会导致损失
我怎样才能解决这个问题 如列中所示,您可以使用rowcount检查受影响的行 并且仅当某些行发生更改时才更新第二个查询
quant_query_values_to_enter = (quant, reel)
cur.execute(query_update_shelf, query_update_shelf_to_enter)
if cur.rowcount > 0:
cur.execute(query_update_quant, quant_query_values_to_enter)
con.commit()
cur.close()
我看不到任何代码的钱增加或减少了matter@nbk那只是一些无聊的支票。不过在评论中它看起来并不漂亮<代码>如果数量!=0:quant=quant-1 query\u update\u quant如果quant为0,则抛出错误并删除该行。您始终可以检查受影响的行,如果>0,则减少该行的数量。但是如果没有完整的代码,我不能给你写一个answer@nbk我编辑了我的帖子并添加了所有的代码,希望这能有所帮助。