Home > Mobile >  Why? in execute if not self._cnx: ReferenceError: weakly-referenced object no longer exists
Why? in execute if not self._cnx: ReferenceError: weakly-referenced object no longer exists

Time:08-02

When I am trying to call Db cursor method from DbConnection.py class it is saying in execute method call line the following. I want to call Db curson from Database class and use it in any python class. Here I want to use it in Review.py. From Database end table is present in the database.

 File "C:\Users\ASUS\AppData\Local\Programs\Python\Python310\lib\tkinter\__init__.py", line 1921, in __call__
    return self.func(*args)
  File "d:\Python\Report-monitoring-system\com\acc\report\main\Main.py", line 33, in <lambda>
    review = ttk.Button(name="",text="Review",command=lambda: reviewObj.reviewReport(self.program),style="C.TButton")
  File "D:\Python/Report-monitoring-system\com\acc\report\report\review\Review.py", line 25, in reviewReport
    print(sqlCursor.execute("SELECT * FROM edp_report"))
  File "C:\Users\ASUS\AppData\Local\Programs\Python\Python310\lib\site-packages\mysql\connector\cursor_cext.py", line 232, in execute
    if not self._cnx:
ReferenceError: weakly-referenced object no longer exists

Here's my code below,

DbConnection.py

class DbConnection:

    def getConnection(self):
        myDb = mysql.connector.connect(

            host="localhost",
            user = "root",
            password = "#####",
            database = "edp_report_vision"
        )
        return myDb.cursor(buffered=True)

Review.py

class Review:
    # Report Display Window
    def reviewReport(self,program):
        if (program!=None):
            print("Not null")
            win = Toplevel(program)
            win.title("New Window")
            win.geometry("626x431")
            style = ttk.Style()
            dbConnection = DbConnection()
            # print(dbConnection.getConnection)
            sqlCursor = dbConnection.getConnection()
            if (dbConnection.getConnection!=None):
                print("Db not null")
                print(sqlCursor.execute("SELECT * FROM edp_report"))
                style.configure("BW.TLabel", foreground="black", background="white")
                l1 = ttk.Label(win,text="Test1", style="BW.TLabel")
                l2 = ttk.Label(win,text="Test", style="BW.TLabel")
                
                l1.pack()
            else:
                print("Null")
        else:
            print("Program null")

Also Main class,this is the invoking class Main.py

class Main:

    program = Tk()

    def Show_Page(self):
        # program = Tk()
        self.program.title("My Tkinter app")
        style = ttk.Style()
        style.map("C.TButton",
        foreground=[('pressed', 'red'), ('active', 'blue')],
        background=[('pressed', '!disabled', 'black'), ('active', 'white')]
        )
        self.program.geometry("626x431")
        reviewObj = Review()
        monitor = ttk.Button(name="",text="Monitor",command=self.selectReports,style="C.TButton")
        monitor.pack(pady=100)
        review = ttk.Button(name="",text="Review",command=lambda: reviewObj.reviewReport(self.program),style="C.TButton")
        review.pack(pady=0)
        self.program.mainloop()
    
# Main method 
if __name__ == "__main__":
    objectMain = Main()
    objectMain.Show_Page()

CodePudding user response:

As myDb is a local variable inside getConnection(), so it will be garbage collected after the function exits.

My suggestion is to change myDb to instance variable and initialize it inside __init__() instead:

class DbConnection:
    def __init__(self):
        self.myDb = mysql.connector.connect(
            host="localhost",
            user = "root",
            password = "#####",
            database = "edp_report_vision"
        )

    def getConnection(self):
        return self.myDb.cursor(buffered=True)
  • Related