在通过Python的mysql-connector访问MySQL数据库的时候,你可能会遇到如题所示的错误。
具体如下:

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 
    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

造成这种错误的原因是:
我的Cursor是通过函数生成并返回的,但是MySQL-Connector模块中的Cursor似乎不能这么使用。

【错误的代码】:
获得Cursor的函数:

# 返回数据库连接对象
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

使用函数的地方:

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)

【正确的代码】:

# -*- 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

正确的执行结果:

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。

说点什么

avatar

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据

  Subscribe  
提醒