【问题标题】:pyodbc.Error: ('HY000', 'The driver did not supply an error!')pyodbc.Error: ('HY000', '驱动程序没有提供错误!')
【发布时间】:2016-10-27 14:02:29
【问题描述】:

我有两个 Mysql 连接,通过 pyodbc 声明为:

connMy1 = pyodbc.connect('DRIVER={MySQL ODBC 5.3 Unicode Driver};SERVER=***;UID=***;PWD=***')
connMy1.autocommit = True
cursorMy1 = connMy1.cursor()

connMy2 = pyodbc.connect('DRIVER={MySQL ODBC 5.3 Unicode Driver};SERVER=***;UID=***;PWD=***')
connMy2.autocommit = True
cursorMy2 = connMy2.cursor()

我创建了一个带有 pandas 的 CSV,如下所示:

def bajar(sql,tabla,ruta):
    print ("bajando datos")
    chunk = 10 ** 5 
    chunks = pandas.read_sql(sql, connMy1, chunksize=chunk)
    eliminarArchivo(ruta)
    print ("creando CSV")
    with open(ruta, 'w') as output:
        for n, df in enumerate(chunks):
            write_header = n == 0
            df.to_csv(output, sep=';', index=False, header=False, na_rep='NULL')
    connMy1.commit()   

然后我调用这个函数来上传 CSV

def subir(ruta,tabla):
    print ("Subiendo datos")
    sqlMy2 = "load data local infile '"+ruta+"' into table "+tabla+" fields terminated by ';' lines terminated by '\r\n';"
    print (sqlMy2)
    cursorMy2.execute(sqlMy2)
    connMy2.commit()

如果我首先调用第二个函数(使用第一个函数预先创建的 CSV),它会完美地上传数据,如果我在第一个函数之后调用该函数,我得到: pyodbc.Error: ('HY000', '驱动程序没有提供错误!')

我做错了什么的任何提示? 谢谢!

【问题讨论】:

    标签: python mysql pyodbc


    【解决方案1】:

    可能是第一个函数创建的连接在您调用它后仍然处于活动状态。您没有关闭该连接,因此光标仍将处于活动状态。

    您不需要两个连接。理想情况下,您应该打开连接,执行操作,然后关闭连接。

    试试这样的:

    def bajar(sql,tabla,ruta):
        connMy1 = pyodbc.connect('DRIVER={MySQL ODBC 5.3 Unicode Driver};SERVER=***;UID=***;PWD=***')
        connMy1.autocommit = True
        cursorMy1 = connMy1.cursor()
        print ("bajando datos")
        chunk = 10 ** 5 
        chunks = pandas.read_sql(sql, connMy1, chunksize=chunk)
        eliminarArchivo(ruta)
        print ("creando CSV")
        with open(ruta, 'w') as output:
            for n, df in enumerate(chunks):
                write_header = n == 0
                df.to_csv(output, sep=';', index=False, header=False, na_rep='NULL')
        connMy1.commit()  
        connMy1.close()
    
    def subir(ruta,tabla):
        connMy1 = pyodbc.connect('DRIVER={MySQL ODBC 5.3 Unicode Driver};SERVER=***;UID=***;PWD=***')
        connMy1.autocommit = True
        cursorMy1 = connMy1.cursor()
        print ("Subiendo datos")
        sqlMy2 = "load data local infile '"+ruta+"' into table "+tabla+" fields terminated by ';' lines terminated by '\r\n';"
        print (sqlMy2)
        cursorMy1.execute(sqlMy2)
        connMy1.commit()
        connMy1.close()
    

    这仍然不理想,我建议创建一个类来处理您的 SQL 连接。这样的东西会更好:

    class MySQL:
        def __init__(self):
            self.conn = None
    
        def __enter__(self):
            self.conn = pyodbc.connect('DRIVER={MySQL ODBC 5.3 Unicode Driver};SERVER=***;UID=***;PWD=***')
            self.conn.autocommit = True
    
        def __exit__(self, *args):
            if self.conn:
                self.conn.close()
                self.conn = None
    

    然后你可以这样调用:

    def bajar(sql,tabla,ruta):
        mysql = MySQL()
        with mysql:
            print ("bajando datos")
            chunk = 10 ** 5 
            chunks = pandas.read_sql(sql, mysql.conn, chunksize=chunk)
            eliminarArchivo(ruta)
            print ("creando CSV")
            with open(ruta, 'w') as output:
                for n, df in enumerate(chunks):
                    write_header = n == 0
                    df.to_csv(output, sep=';', index=False, header=False, na_rep='NULL')
            mysql.conn.commit()
    
    
    def subir(ruta,tabla):
        mysql = MySQL()
        with mysql:
            print ("Subiendo datos")
            sqlMy2 = "load data local infile '"+ruta+"' into table "+tabla+" fields terminated by ';' lines terminated by '\r\n';"
            print (sqlMy2)
            mysql.conn.cursor().execute(sqlMy2)
            mysql.conn.commit()
    

    这样一来,只要你总是使用'​​with'语句,你总是会为每个动作连接和断开连接。

    【讨论】:

    • 我选择了第二种解决方案,我使用一个文件在顶部声明所有要使用的函数的连接,并在仅调用创建 CSV 的函数时从另一个文件调用它们,购买我ii. 尝试上传时出错。您的解决方案效果很好!谢谢
    • 没问题。乐于助人。
    • 这个答案非常好,但您可能还想考虑使用 MySQL 驱动程序而不是 pyodbc
    • 我很快就会将此脚本迁移到 linux 并使用 MySQL 驱动程序,同时我需要它以这种方式工作。谢谢!
    猜你喜欢
    • 2018-11-14
    • 1970-01-01
    • 2021-06-12
    • 2018-03-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多