Python,mysql-connector,error:ReferenceError: weakly-referenced object no longer exists
在通过Python的mysql-connector访问MySQL数据库的时候,你可能会遇到如题所示的错误。
具体如下:
1 2 3 4 5 6 7 8 9 10 11 |
C:\Users\adamhuan\venv\Scripts\python.exe H:/PyCharm_data/Analyze_MySQL_Option_File/MySQL_Analyze_and_Collect.py MySQL: Connect Successful. Traceback (most recent call last): File "H:/PyCharm_data/Analyze_MySQL_Option_File/MySQL_Analyze_and_Collect.py", line 72, in <module> connect_session.execute('select user,host from mysql.user;') File "C:\Users\adamhuan\venv\lib\site-packages\mysql\connector\cursor.py", line 518, in execute if not self._connection: ReferenceError: weakly-referenced object no longer exists Process finished with exit code 1 </module> |
造成这种错误的原因是:
我的Cursor是通过函数生成并返回的,但是MySQL-Connector模块中的Cursor似乎不能这么使用。
【错误的代码】:
获得Cursor的函数:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# 返回数据库连接对象 def return_mysql_connect(config): # do try: # do: connect obj_connect = mysql.connector.connect(**config) # display print "MySQL: Connect Successful." # do: cursor obj_cursor = obj_connect.cursor() # return return obj_cursor #return obj_connect except mysql.connector.Error as err: print "MySQL: Connect Failed.{}".format(err) # default: pass pass |
使用函数的地方:
1 2 3 4 5 6 7 8 9 10 11 |
if __name__ == "__main__": # variable connect_session = return_mysql_connect(config) # do connect_session.execute('select user,host from mysql.user;') # display for line in connect_session: print(line) |
【正确的代码】:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# -*- coding: UTF-8 -*- # ========================== # Imports # MySQL import mysql.connector # ConfigParser from configparser import * # ========================== # Functions # 0000000000000000 # 函数:数据库(MySQL) # 返回数据库连接对象 def return_mysql_connect(config): # do try: # do: connect obj_connect = mysql.connector.connect(**config) # display print "MySQL: Connect Successful." # do: cursor #obj_cursor = obj_connect.cursor() # do: execute #obj_cursor.execute('select user,host from mysql.user;') # display #for line in obj_cursor: # print(line) # return #return obj_cursor return obj_connect except mysql.connector.Error as err: print "MySQL: Connect Failed.{}".format(err) # default: pass pass # 0000000000000000 # ========================== # Variables config = { 'host': '192.168.230.129', 'user': 'root', 'password': '********', 'port': '3306', 'database': 'mysql', 'charset': 'utf8' } # ========================== # Main if __name__ == "__main__": # variable connect_session = return_mysql_connect(config) cursor_session = connect_session.cursor() # do cursor_session.execute('select user,host from mysql.user;') # display for line in cursor_session: print(line) # ========================== # Finished |
正确的执行结果:
1 2 3 4 5 6 7 8 9 |
C:\Users\adamhuan\venv\Scripts\python.exe H:/PyCharm_data/Analyze_MySQL_Option_File/MySQL_Analyze_and_Collect.py MySQL: Connect Successful. (bytearray(b'repluser'), bytearray(b'%')) (bytearray(b'root'), bytearray(b'%')) (bytearray(b'mysql.session'), bytearray(b'localhost')) (bytearray(b'mysql.sys'), bytearray(b'localhost')) (bytearray(b'root'), bytearray(b'localhost')) Process finished with exit code 0 |
————————————————————
Done。