ReferenceError: weakly-referenced object no longer exists - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: ReferenceError: weakly-referenced object no longer exists (/thread-31471.html) Pages:
1
2
|
RE: ReferenceError: weakly-referenced object no longer exists - deanhystad - Dec-14-2020 If cur object was destroyed I would expect a different error, not something from deep in the bowels of MySQL. I suppose something inside of cur could have gone away. Similar to the way you need to keep a handle for an image that you want to use in Tk. RE: ReferenceError: weakly-referenced object no longer exists - MrBitPythoner - Dec-14-2020 search_entr is a tkinter entry RE: ReferenceError: weakly-referenced object no longer exists - MrBitPythoner - Dec-14-2020 also, why is cur garbage collected. RE: ReferenceError: weakly-referenced object no longer exists - deanhystad - Dec-14-2020 How would we know. You only provide a tiny peek at your code and don't show where cur is created. I am betting the problem is with the text reference returned by Tk.Entry. Try setting your search string to some literal and see if the problem goes away. If it does you know the problem is Tk.Entry. RE: ReferenceError: weakly-referenced object no longer exists - MrBitPythoner - Dec-14-2020 The following code is search_book.py, which will be imported into main.py Full Code: from tkinter import * from tkinter import messagebox from tkinter import StringVar import tkinter.ttk import pyautogui as pg import mysql.connector as mc def search_book(): global password, username usern = pg.prompt("Username", "Enter your username") passw = pg.password("Password", "Enter your password") try: conn = mc.connect(host="localhost", db="Library", user=usern, passwd=passw) except: messagebox.showerror("Incorrect Credentials", "Those credentials are incorrect") exit() global cur cur = conn.cursor() form = Tk() form.geometry("500x200") form.resizable(False, False) form.title("Search for a book") top_lbl = Label(form, text="Search For A Book", font="Courier 26").pack() OptionList = [ "Pick A Search Type", "Title", "Author", "ISBN", ] global variable variable = StringVar(form) variable.set(OptionList[0]) search_dropd = OptionMenu(form, variable, *OptionList) search_dropd.pack() global search_entr search_entr = Entry(form, width=45) search_entr.pack() submit_btn = Button(text="SUBMIT", command=submit, width=34).pack() def submit(): search_words = search_entr.get() search_type = variable.get() if search_type == "Pick A Search Type": messagebox.showerror("Invalid Choice", "'Pick A Search Type' is not a valid search type.") else: global results if search_type == "ISBN": cur.execute("SELECT * FROM Books WHERE ISBN = %s", (search_words,)) results = cur.fetchball() if search_type == 'Title': cur.execute("SELECT * FROM Books where ITLE = %s", (search_words,)) results = cur.fetchball() if search_type == "Author": cur.execute("SELECT * FROM Books WHERE AUTHOR = %s", (search_words,)) results = cur.fetchball() show_results(results) def show_results(results_list): for i in range(len(results_list)): print(results_list[i]) RE: ReferenceError: weakly-referenced object no longer exists - buran - Dec-14-2020 You don't show how you call your functions, but I assume you call search_book() in order to display the GUI.You declare cur as global but actually it does not exists in the global scope. Once it complete the execution of the search_book , there are no references to cur and it is destroyed.So, why do you create cur in search_book in the first place. Why not create it in submit() where it is actually needed and used.Ideally you should refactor your spaghetti code and not use globals at all. As you see it's terrible to debug problem in such code. The other "solution" is to initialize cur in the global namespace, e.g. cur = None at the top, after the imports.As a side note - you use GUI, but print result in stdout??? Not to mention mixing pyautogui and tkinter??? RE: ReferenceError: weakly-referenced object no longer exists - MrBitPythoner - Dec-14-2020 I moved cur to submit(). Works now. Thanks RE: ReferenceError: weakly-referenced object no longer exists - buran - Dec-14-2020 if you look at the mysql.connector source code you will see it uses weakref class CMySQLCursor(MySQLCursorAbstract): """Default cursor for interacting with MySQL using C Extension""" _raw = False _buffered = False _raw_as_string = False def __init__(self, connection): """Initialize""" MySQLCursorAbstract.__init__(self) self._insert_id = 0 self._warning_count = 0 self._warnings = None self._affected_rows = -1 self._rowcount = -1 self._nextrow = (None, None) self._executed = None self._executed_list = [] self._stored_results = [] if not isinstance(connection, MySQLConnectionAbstract): raise errors.InterfaceError(errno=2048) self._cnx = weakref.proxy(connection)So, actually the weakly referenced object is connection and it is destroyed, which leads to error in execute(), line 232 if not self._cnx: |