ModuleNotFoundError: No module named 'MySQLdb'

后端 未结 5 474
眼角桃花
眼角桃花 2020-12-10 05:23

After finishing of one of my Flask projects, I uploaded it on github just like everybody else. after a 2-3 months period I downloaded the entire githube repository on anothe

5条回答
  •  半阙折子戏
    2020-12-10 06:15

    I have the same problem like you. Here is my solution:

    Reason: python3.X does not support MySQLdb,so you need to change to pymysql model

    Solution: Change the content of import.

    1):

    replace all MySQLdb with pymysql
    

    2):

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

    3)

    if pymysql is not None:
        # Fix the access conversions to properly recognize unicode/binary
        FIELD_TYPE = pymysql.connections.FIELD_TYPE # MySQLdb.constants.FIELD_TYPE
        FLAG = pymysql.constants.FLAG# MySQLdb.constants.FLAG
        CONVERSIONS = copy.copy (pymysql.converters.conversions)# (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]
            CONVERSIONS[field_type] = [(FLAG.BINARY, str)].append(CONVERSIONS[field_type])
    
        # Alias some common MySQL exceptions
        IntegrityError = pymysql.IntegrityError# MySQLdb.IntegrityError
        OperationalError = pymysql.OperationalError# MySQLdb.OperationalError
    

    4):

    def __init__(self, host, database, user=None, password=None,
                     max_idle_time=7 * 3600, connect_timeout=10,# 设置连接超时时间,时间是秒
                     time_zone="+0:00", charset = "utf8", sql_mode="TRADITIONAL"):
    

    5):

    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.zip_longest(column_names, row)) for row in cursor]
        finally:
            cursor.close()
    

提交回复
热议问题