我正在尝试使用Python来记录为显示包裹而扫描的条形码。我希望最终使用OpenCV作为输入,但不想限制每个包裹的扫描次数,因此我希望测试一下包裹是否已经在MariaDB中标记为已发货。
输入工作(从手持扫描仪现在),它正在查找数据库,看看条形码是否已被扫描之前罚款,但当它谈到一个没有,它似乎跳过了其他语句。我已经盯着这个代码多年了!任何帮助感谢。谢谢
import mariadb
import sys
from datetime import datetime
now = datetime.now()
dt_string = now.strftime("%d/%m/%Y %H:%M:%S")
print("date and time =", dt_string)
conn = mariadb.connect(
user="root",
password="password",
host="localhost",
database="pythontest")
cur = conn.cursor()
while True:
barcode=input("---> ")
if barcode.startswith("%"):
courier = ("DPD")
elif barcode.startswith("JD"):
courier = ("Royal Mail")
else:
courier = ("Unknown Courier")
print("Unknown Courier")
cur.execute("SELECT trackingNumber,status,shipTime,courier FROM opencvdb WHERE trackingNumber=?", (barcode,))
for trackingNumber, status, shipTime, courier in cur:
if trackingNumber in barcode:
print(f"Already Shipped at {shipTime} with {courier} - Tracking Number: {barcode}")
else:
try:
print("Shipping Now")
cur.execute("INSERT INTO opencvdb (trackingNumber,status,shipTime,courier) VALUES (?, ?, ?, ?)", (barcode, "Done", dt_string,courier))
except mariadb.Error as e:
print(f"Error: {e}")
conn.commit()
print(f"Last Inserted ID: {cur.lastrowid}")
conn.close()
1条答案
按热度按时间rggaifut1#
如果此查询未返回任何结果(因为没有匹配的记录)
cur.execute("SELECT trackingNumber,status,shipTime,courier FROM opencvdb WHERE trackingNumber=?", (barcode,))
那么这个for语句就没有要循环的内容
for trackingNumber, status, shipTime, courier in cur:
个并且永远不会执行此语句
if trackingNumber in barcode:
更新:我认为这可能是您想要做的: