Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save ppdouble/ab633f577338df040b3c to your computer and use it in GitHub Desktop.
Save ppdouble/ab633f577338df040b3c to your computer and use it in GitHub Desktop.
I came across this issue using the following code file
the error message is ReferenceError: weakly-referenced object no longer exists
reason is in the mysql/connector/cursor.py file
see these lines:
def _set_connection(self, connection):
"""Set the connection"""
try:
self._connection = weakref.proxy(connection)
self._connection._protocol # pylint: disable=W0212,W0104
except (AttributeError, TypeError):
raise errors.InterfaceError(errno=2048)
because the connection is weakref, so when we use its reference, it is unsafe.
To avoid this issue, use the test1 method.
import mysql.connector as connector
def connection():
config = {
"user": "root",
"password": "password",
"host": "localhost",
"port": 3306,
"database": "pydb"
}
try:
c = connector.connect(**config)
return c
except:
print "connection error"
exit(1)
def test(): # error method
cur= connection().cursor()
cur.execute("select version();")
print cur.fetchone()
def test1(): #no error method
cn = connection()
cur = cn.cursor()
cur.execute("select version;")
print cur.fetchone()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment