【问题标题】:_mysql_connector.MySQLInterfaceError: Python type method cannot be converted_mysql_connector.MySQLInterfaceError: Python类型方法无法转换
【发布时间】:2021-12-24 03:28:06
【问题描述】:
##GUI FILE
root = Tk()
root.title("Inventory Management")
root.geometry('1000x600')

def add_inventory():
    myInventory = product.Product()
    myInventory.setProductID(productID.get())
    myInventory.setProdName(productName.get())
    myInventory.addProduct()

productID = IntVar()
productName = StringVar()

prodIDentry = Entry(productframe, width=22, textvariable=productID)
prodNameentry = Entry(productframe, width=22, textvariable=productName)

btn_add = Button(productframe, text="Add", font="Verdana 10 bold", command = add_inventory)

##product file
class Product:
    def __init__(self):
        self.ProductID = ''
        self.ProdName = ''

    prodID = self.getProductID()
    prodname = self.getProdName()
    
    def getProductID(self):
        return self.ProductID
    def setProductID(self, value):
        self.ProductID = value

    def getProdName(self):
        return self.getProdName
    def setProdName(self, value):
        self.ProdName = value

    def addProduct(self):
    mydb = mysql.connector.connect(
        host='localhost',
        database='PHP',
        user='root',
        password=''
    )

    mycursor = mydb.cursor()

    sql1 = "INSERT INTO product (ProductID, ProdName) VALUES (%s,%s)"
    val = (prodID, prodname)

    mycursor.execute(sql1, val)
    mydb.commit()

我的程序有一个需要用户输入的 GUI,然后它会通过单击按钮将输入输入数据库。这些是我要插入的几个变量。 但是一直返回:_mysql_connector.MySQLInterfaceError: Python类型方法无法转换

【问题讨论】:

  • 如果您在执行前print(val) 会发生什么?
  • @AKX 嗨,对不起,我刚刚在我的问题中添加了更多代码,请看一下:/我需要帮助
  • 你的代码没有意义。一方面,类主体中现在有 MySQL 代码,在任何函数之外?代码中的任何地方也没有getProductID() 函数。
  • @AKX 我刚刚又编辑了一次,很抱歉我不太熟悉如何显示问题...现在你理解更好了吗?
  • 对不起,没有。如果您有一个调用 self.getProductID() 函数的有问题的段,您还应该在帖子中包含该函数。

标签: python mysql mysql-python


【解决方案1】:

你的问题是函数

def getProdName(self):
    return self.getProdName

它返回自身 (getProdName),而不是产品名称变量。

MySQL 连接器说

MySQLInterfaceError: Python 类型方法无法转换

因为它不能将方法 (getProdName) 转换为 MySQL 可以理解的东西。

getProdName()方法改成返回ProdName

def getProdName(self):
    return self.ProdName

(或完全摆脱它;setter 和 getter 在 Python 中不是很流行。)

【讨论】:

  • 啊,我现在看到了...谢谢!!!!
猜你喜欢
  • 2021-11-14
  • 2020-04-13
  • 2020-07-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-02
  • 1970-01-01
相关资源
最近更新 更多