Notes on Python stepping on a hole

1, Python 2. X migration to 3.x problem

1. torndb does not support 3.x. error reporting solution in 3.x environment

The torndb module does not support Python 3. X, so you need to modify part of the torndb source code to use it normally

Source code modification:
1. Modify the import module

    import pymysql.connections
    import pymysql.converters
    import pymysql.cursors
    # import MySQLdb.constants
    # import MySQLdb.converters
    # import MySQLdb.cursors

2. Modify the way to connect mysql

    def reconnect(self):
        """Closes the existing database connection and re-opens it."""
        # self.close()
        # self._db = MySQLdb.connect(**self._db_args)
        # self._db.autocommit(True)
        self.close()
        self._db = pymysql.connect(**self._db_args)  # MySQLdb.connect(**self._db_args)
        self._db.autocommit(True)

3. Modify connection parameters

# if MySQLdb is not None:
if pymysql is not None:
    # Fix the access conversions to properly recognize unicode/binary
    # FIELD_TYPE = MySQLdb.constants.FIELD_TYPE
    # FLAG = MySQLdb.constants.FLAG
    # CONVERSIONS = copy.copy(MySQLdb.converters.conversions)
    #
    # field_types = [FIELD_TYPE.BLOB, FIELD_TYPE.STRING, FIELD_TYPE.VAR_STRING]
    # if 'VARCHAR' in vars(FIELD_TYPE):
    #     field_types.append(FIELD_TYPE.VARCHAR)
    #
    # for field_type in field_types:
    #     CONVERSIONS[field_type] = [(FLAG.BINARY, str)] + CONVERSIONS[field_type]
    #
    # # Alias some common MySQL exceptions
    # IntegrityError = MySQLdb.IntegrityError
    # OperationalError = MySQLdb.OperationalError

    FIELD_TYPE = pymysql.connections.FIELD_TYPE  
    FLAG = pymysql.constants.FLAG  
    CONVERSIONS = copy.copy(pymysql.converters.conversions)  

    field_types = [FIELD_TYPE.BLOB, FIELD_TYPE.STRING, FIELD_TYPE.VAR_STRING]

    if 'VARCHAR' in vars(FIELD_TYPE):
        field_types.append(FIELD_TYPE.VARCHAR)

    for field_type in field_types:
        CONVERSIONS[field_type] = [(FLAG.BINARY, str)].append(CONVERSIONS[field_type])

    IntegrityError = pymysql.IntegrityError  
    OperationalError = pymysql.OperationalError

4. Modify the connection timeout, set it in the torndb initialization method, and pass it to pymysq

 def __init__(self, host, database, user=None, password=None,
                 max_idle_time = 7 * 3600, connect_timeout = 10,  # Set the connection timeout in seconds
                 time_zone = "+0:00", charset = "utf8", sql_mode = "TRADITIONAL"):

5. Modify the iterative method in the query method

    def query(self, query, *parameters, **kwparameters):
        # """Returns a row list for the given query and parameters."""
        # cursor = self._cursor()
        # try:
        #     self._execute(cursor, query, parameters, kwparameters)
        #     column_names = [d[0] for d in cursor.description]
        #     return [Row(itertools.izip(column_names, row)) for row in cursor]
        # finally:
        #     cursor.close()
        """Returns a row list for the given query and parameters."""
        cursor = self._cursor()
        try:
            self._execute(cursor, query, parameters, kwparameters)
            column_names = [d[0] for d in cursor.description]
            return [Row(itertools.zip_longest(column_names, row)) for row in cursor]
        finally:
            cursor.close()

So far, python 3. X has been able to use torndb normally

Published 5 original articles, won praise 1, visited 101
Private letter follow

Keywords: Python MySQL Database

Added by doni49 on Sat, 18 Jan 2020 17:12:29 +0200