简体   繁体   English

使用 MySQL 连接时的 Python OOP 编程问题

[英]Python OOP programming issue when using MySQL connect

I am trying to write a backend that will be used for an IOS app.我正在尝试编写一个将用于 IOS 应用程序的后端。 I know it will be technically the wrong way to do it but it wont be deployed.我知道这在技术上是错误的方法,但它不会被部署。

My issues is i get the error我的问题是我收到错误

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

This happens when i run the following from main.py当我从main.py运行以下main.py时会发生这种情况

import database


db = database.database()

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

Finally this is my database.py code最后这是我的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()

Instead of:代替:

self.cursor(query)

Try this:尝试这个:

self.cursor.execute(query)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM