使用pymysql连接到mysql数据库时出现结构ubyte错误

0sgqnhkj  于 2021-06-15  发布在  Mysql
关注(0)|答案(0)|浏览(171)

在使用pymysql时,我的行为变得非常怪异。
我的连接类如下。

class MySqlConnection:
def __init__(self):
    self.host = conf.DB_HOST
    self.port = conf.DB_PORT
    self.user = conf.DB_USER
    self.passwd = conf.DB_PASSWD
    self.db = conf.DB_DEFAULT
    self.connection = None
    self.cursor = None
    #self.__connect()

def connect(self):
    self.connection = pymysql.connect(host=self.host,
                                      user=self.user,
                                      password=self.passwd,
                                      db=self.db)

    self.cursor = self.connection.cursor()

上面有一个 Package 器,它执行查询。

class QueryExecutor:
def __init__(self, table_name_for_query, table_attributes):
    self.conn = MySqlConnection()
    self.table_for_query = table_name_for_query
    self.table_attributes = table_attributes

def create_connection(self):
    self.conn.connect()

def close_connection(self):
    self.conn.close()

还有一个table类。

class SupportedTables:
def __init__(self, table_name, attributes):
    self.query_executor = QueryExecutor(table_name, attributes)

def save(self):
    '''
    Inserts query for saving data in DB.
    '''
    self.query_executor.create_connection()
    value_list = []
    for attrib in self.attributes:
        if 'AUTO_INCREMENT' in attrib[1]:
            continue
        col_name = attrib[0]
        value_list.append(getattr(self, col_name))
    insert_data = [value_list]
    if len(value_list) != 0:
        self.query_executor.insert_data(data_to_insert=insert_data)
    self.query_executor.close_connection()

现在,当我对supportedtables及其派生类执行测试时,代码运行良好。我没有任何问题。
但是,当我尝试从测试类执行代码时,会出现如下错误:
self.query\u executor.create\u connection()“/home/priyesh/db/queryexecutor\u v2.py”,第17行,
在创建\u连接中

self.conn.connect()   File "/home/priyesh/db/connection_handler.py", line 24, in connect

db=self.db)   File "/home/priyesh/virtualenvs/virtualenv_python3_ibsync/lib/python3.6/site-packages/pymysql/__init__.py",

94号线,接入

return Connection(*args,**kwargs)   File "/home/priyesh/virtualenvs/virtualenv_python3_ibsync/lib/python3.6/site-packages/pymysql/connections.py",

第327行,在init中

self.connect()   File "/home/priyesh/virtualenvs/virtualenv_python3_ibsync/lib/python3.6/site-packages/pymysql/connections.py",

598线,连接

self._request_authentication()

文件“/home/priyesh/virtualenvs/virtualenv\u python3\u ibsync/lib/python3.6/site packages/pymysql/connections.py”,
第849行,在\u request \u authentication data+=struct.pack('b',len(connect \u attrs))+connect \u attrs struct.error:ubyte格式要求0<=number<=255
知道为什么会发生这种情况吗?代码非常简单,可以从测试类工作,但不能从主执行脚本工作。

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题