为了账号安全,请及时绑定邮箱和手机立即绑定

Python-MySQL 查询不显示结果,包括同一查询中先前的导入

Python-MySQL 查询不显示结果,包括同一查询中先前的导入

一只斗牛犬 2023-07-11 14:37:23
我有一个功能,在扫描条形码后将新记录添加到 Mysql 数据库中。功能正常,但在应用程序屏幕上返回结果时出现问题。当我第一次扫描条形码时,即使我可以看到新记录已创建,它也不会返回任何结果。但当我第二次扫描条形码时,它只返回 1 条记录。编辑:(添加了有关该问题的更多信息)之后它始终是-1记录,这不会是问题我可以向所有结果添加+1,但问题是第一个,因为它根本不返回任何内容。我尝试在查询之间使用 time.sleep(.3) 但这没有任何效果。我现在想知道 Python 代码是否错误,或者我的 SQL 查询是否应该有所不同。def db_execute3(config, sql, val):    mydb = mysql.connector.connect(**config)    mycursor = mydb.cursor()    try:        mycursor.execute(sql, val)        mydb.commit()    except mysql.connector.Error as err:        if err.errno == errorcode.CR_CONN_HOST_ERROR:            popip.open()            Clock.schedule_once(popip.dismiss, 3)        elif err.errno == errorcode.ER_ACCESS_DENIED_ERROR:            popx.open()            Clock.schedule_once(popx.dismiss, 3)        elif err.errno == errorcode.ER_BAD_DB_ERROR:            popdb.open()            Clock.schedule_once(popdb.dismiss, 3)        elif err.errno == errorcode.ER_NO_REFERENCED_ROW_2:            popbr.open()            Clock.schedule_once(popbr.dismiss, 3)        else:            mycursor.close()def inbsort_btndwn(self, _):   cont = self.container_no.text.upper()   barc = self.sku_barcode.text.upper()   sort_worknumb = self.sort_worknumb.text.upper()   val = (sort_worknumb, cont, barc)   valx = (cont,barc)   if barc is "" and cont is "":       errorsound.play()       self.pallet_sku.text = ""       self.number_sku.text = ""       Clock.schedule_once(self.focus_container_no, 0.2)   elif barc is "" and cont is not "":       errorsound.play()       self.pallet_sku.text = ""       self.number_sku.text = ""       Clock.schedule_once(self.focus_sku_barcode, 0.2)
查看完整描述

1 回答

?
largeQ

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)



查看完整回答
反对 回复 2023-07-11
  • 1 回答
  • 0 关注
  • 112 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信