【问题标题】:How do I solve mysql kivy operator validation problem:如何解决 mysql kivy 运算符验证问题:
【发布时间】:2020-08-25 21:47:45
【问题描述】:

以下是我从 python kivy 代码中得到的错误。请帮我解决这个问题。我实际上想使用 MySQl 命令从操作员窗口访问我的数据库股票。

第 41 行,在 update_purchases 中 target_code = self.stocks.find_one({'product_code': pcode}) AttributeError: 'str' 对象没有属性 'find_one'

from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.label import Label
import mysql.connector
import re
from kivy.lang import Builder


class OperatorWindow(BoxLayout):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self.mydb = mysql.connector.connect(
            host='localhost',
            user='root',
            passwd='Livingstone2#',
            database='pos'
        )
        self.mycursor = self.mydb.cursor()
      

        self.stocks = ''
        self.cart = []
        self.qty = []
        self.total = 0.00

    def logout(self):
        self.parent.parent.current = 'scrn_si'

    def update_purchases(self):
        pcode = self.ids.code_inp.text
        products_container = self.ids.products
        #target_code = self.mycursor.fetchall({'product_code': pcode})
        target_code = self.stocks.find_one({'product_code': pcode})
        if target_code == None:
            pass
        else:
            details = BoxLayout(size_hint_y=None, height=30, pos_hint={'top': 
            1})
            products_container.add_widget(details)

            code = Label(text=pcode, size_hint_x=.2, color=(.06, .45, .45, 1))
            name = Label(text=target_code['product_name'], size_hint_x=.3, 
            color=(.06, .45, .45, 1))
            qty = Label(text='1', size_hint_x=.1, color=(.06, .45, .45, 1))
            disc = Label(text='0.00', size_hint_x=.1, color=(.06, .45, .45, 
            1))
            price = Label(text=target_code['product_price'], size_hint_x=.1, 
            color=(.06, .45, .45, 1))
            total = Label(text='0.00', size_hint_x=.2, color=(.06, .45, .45, 
            1))
            details.add_widget(code)
            details.add_widget(name)
            details.add_widget(qty)
            details.add_widget(disc)
            details.add_widget(price)
            details.add_widget(total)

            #Update Preview
            pname = name.text
            pprice = float(price.text)
            pqty = str(1)
            self.total += pprice
            purchase_total = '`\n\nTotal\t\t\t\t\t\t\t\t'+str(self.total)
            self.ids.cur_product.text = pname
            self.ids.cur_price.text = str(pprice)
            preview = self.ids.receipt_preview
            prev_text = preview.text
            _prev = prev_text.find('`')
            if _prev >0:
                prev_text = prev_text[:_prev]

            ptarget = -1
            for i, c in enumerate(self.cart):
                if c == pcode:
                    ptarget = i

            if ptarget >= 0:
                pqty = self.qty[ptarget]+1
                self.qty[ptarget] = pqty
                expr = '%s\t\tx\d\t' % (pname)
                rexpr = pname + '\t\tx' + str(pqty)+'\t'
                nu_text = re.sub(expr, rexpr, prev_text)
                preview.text = nu_text + purchase_total
            else:
                self.cart.append(pcode)
                self.qty.append(1)
                nu_preview = '\n'.join([prev_text, pname + '\t\tx' + pqty + 
        '\t\t' + str(pprice), purchase_total])
                preview.text = nu_preview

        self.ids.disc_input.text = '0.00'
        self.ids.disc_perc_input.text = '0.00'
        self.ids.qty_input.text = str(pqty)
        self.ids.price_input.text = str(pprice)
        self.ids.vat_input.text = '15%'
        self.ids.total_input.text = str(pprice)

【问题讨论】:

  • 在我发布答案之前,您已经添加了代码。我可以在这里看到两件事。首先,您没有对数据库执行任何查询,只是试图检索结果。其次,该代码从视图类内部从数据库中检索结果,这不符合良好的编程模型。

标签: python kivy


【解决方案1】:

正如错误消息所述,fetchall 方法在只需要一个位置参数时被调用时使用了两个位置参数。

但是下一行的第二个参数在哪里?

 target_code = self.mycursor.fetchall({'product_code': pcode})

嗯,第二个参数是字典{'product_code': pcode}。第一个是对象的self。我建议您阅读this article 以便更好地理解它,但问题是每个类的方法都需要对对象的引用才能知道谁在执行一个方法。该参考是self

注意the documentation 方法fetchall 不接受任何其他位置参数。解决方案是使用以下行获取所有行:

 target_code = self.mycursor.fetchall()

最后,注意到您传递给fetchall 的字典,也许您打算在execute 上使用它,它会在运行查询之前使用它在查询中设置值pcode

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-02-05
    • 1970-01-01
    • 2022-08-04
    • 1970-01-01
    • 2016-10-10
    • 2022-01-19
    相关资源
    最近更新 更多