1 回答
TA贡献2039条经验 获得超7个赞
如前所述,我建议重构您的代码,以免将 UI 代码与数据库访问混合在一起。这样您就可以更轻松地测试程序的每个部分(手动,从单独的模块)或使用单元测试等自动测试。
您还可以自由创建新游标,不要重复使用单个游标。
这是一个这样的重构......
def find_container(db, *, container_no, barcode):
cursor = db.cursor()
cursor.execute(
"select * from inb_container where `container_no` = %s and `sku_code` = %s",
(container_no, barcode),
)
return cursor.fetchone()
def insert_sor_con(db, *, sort_worknumb, container_no, barcode):
cursor = db.cursor()
cursor.execute(
"INSERT INTO inb_sor_con (`work_number`, `container_no`,`sku_barcode`) VALUES (%s, %s, %s)",
(sort_worknumb, container_no, barcode),
)
db.commit()
def get_scan_results(db, *, container_no, barcode):
cursor = db.cursor()
cursor.execute(
"""
SELECT ic.sort_box,ic.sort_pallet
FROM inb_container ic, inb_sor_con ib
WHERE (
ic.container_no = ib.container_no AND
ic.sku_code = ib.sku_barcode AND
ic.container_no = %s AND
ic.sku_code = %s
)""",
(container_no, barcode),
)
return cursor.fetchall()
def show_error(message):
errorsound.play()
popuni.content.text = message
popuni.open()
Clock.schedule_once(popuni.dismiss, 2)
class SomeUI:
def inbsort_btndwn(self, _):
container_no = self.container_no.text.upper()
barcode = self.sku_barcode.text.upper()
sort_worknumb = self.sort_worknumb.text.upper()
if not (barcode and container_no):
errorsound.play()
self.pallet_sku.text = ""
self.number_sku.text = ""
if not barcode:
Clock.schedule_once(self.focus_sku_barcode, 0.2)
else:
Clock.schedule_once(self.focus_container_no, 0.2)
return
try:
with mysql.connector.connect(**config) as mydb:
container_record = find_container(
mydb, container_no=container_no, barcode=barcode
)
if container_record:
correctsound.play()
insert_sor_con(
mydb,
sort_worknumb=sort_worknumb,
container_no=container_no,
barcode=barcode,
)
scan_results = list(
get_scan_results(mydb, container_no=container_no, barcode=barcode)
)
for sort_box, sort_pallet in scan_results:
self.pallet_sku.text = "{}".format(sort_pallet)
self.number_sku.text = "Scanned: {} of: {}".format(
len(scan_results), sort_box
)
else:
show_error("No records for scanned Barcode!")
except Exception as exc:
# may want to use `traceback.print_traceback()` here for more detail
print(exc)
show_error(f"Error: {exc}")
Clock.schedule_once(self.clear_barcode, 0.2)
Clock.schedule_once(self.focus_sku_barcode, 0.21)
添加回答
举报