Forked from shuson/description-mysql_weakly-referenced object
Created
June 25, 2014 10:34
-
-
Save ppdouble/ab633f577338df040b3c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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