使用MySQL connect时的Python OOP编程问题

2024-04-29 13:52:06 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试编写一个用于IOS应用程序的后端。我知道这在技术上是错误的,但它不会被部署

我的问题是我得到了错误

self.cursor(query)
TypeError: 'CMySQLCursor' object is not callable

当我从main.py运行以下命令时会发生这种情况

import database


db = database.database()

staff = db.getData("SELECT * FROM timesheets.staff")

最后,这是我的database.py代码

import mysql.connector

class database :
    conn = ""
    cursor = ""

    def __init__(self):
        self.conn = mysql.connector.connect(user='james',
                                       password='timeismoney',
                                       host='hallfamily.mycrestron.com',
                                       database='timesheets',
                                       port='6033')

        self.cursor = self.conn.cursor()

        print("Done")

    def getData(self, query):

        #Checking if the user has applied a string
        if isinstance(query, str):
            self.cursor(query)
        else:
            return "You have provided a request that cant be processed"

        #Fetching all the results
        result = self.cursor.fetchall()

        #Returning back to the user
        return result

    def postData(self):
        print("Coming soon")


    def close(self):
        self.conn.close()

Tags: thepyimportselfdbdef错误conn