简体   繁体   English

pyodbc.Error:('HY000','驱动程序未提供错误!')

[英]pyodbc.Error: ('HY000', 'The driver did not supply an error!')

I have two Mysql connection via pyodbc declared as: 我有两个通过pyodbc的Mysql连接,声明为:

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

I create a CSV with pandas caling the connection like this: 我用大熊猫创建了一个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()   

Then I call this function to upload the CSV 然后我调用此函数上传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()

If I call the second function first (with a pre created CSV by the first function) it uploads the data perfectly, if I call the function after the first I get: pyodbc.Error: ('HY000', 'The driver did not supply an error!') 如果我首先调用第二个函数(第一个函数使用预先创建的CSV格式),则会完美地上传数据,如果我在第一个函数之后调用该函数,则会得到:pyodbc.Error:('HY000','驱动程序未提供一个错误!')

Any hints of what Im doing wrong? 我在做什么错的任何暗示吗? Thank you! 谢谢!

It might be that the connection created by the first function is still active after you call it. 调用后,第一个函数创建的连接可能仍处于活动状态。 You are not closing that connection so the cursor will still be active. 您没有关闭该连接,因此光标将仍然处于活动状态。

You don't need two connections. 您不需要两个连接。 You should ideally open the connection, perform the action, then close the connection. 理想情况下,您应该打开连接,执行操作,然后关闭连接。

Try something like this: 尝试这样的事情:

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

This still isn't ideal and I would recommend creating a class to handle your SQL connections. 这仍然不是理想的,我建议创建一个类来处理您的SQL连接。 Something like this would be better: 这样的事情会更好:

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

Which you could then call like this: 然后可以这样调用:

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

This way, as long as you always use the 'with' statement, you will always connect and disconnect for each action. 这样,只要您始终使用“ with”语句,就将始终为每个操作连接和断开连接。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么我收到错误 pyodbc.Error: ('HY000', '驱动程序没有提供错误!') - Why I receive error pyodbc.Error: ('HY000', 'The driver did not supply an error!') Python SQLAlchemy pyodbc.Error: ('HY000', '驱动程序没有提供错误!') - Python SQLAlchemy pyodbc.Error: ('HY000', 'The driver did not supply an error!') pyodbc MERGE INTO 错误:HY000:驱动程序未提供错误 - pyodbc MERGE INTO error: HY000: The driver did not supply an error pyodbc.Error:('HY000','[HY000][Microsoft][ODBCDriver18forSQL Server]SSPIProvider:NoKerberoscredentialsavailable(默认缓存:FILE:/tmp/krb5cc_1051) - pyodbc.Error:('HY000','[HY000][Microsoft][ODBCDriver18forSQL Server]SSPIProvider:NoKerberoscredentialsavailable(default cache: FILE:/tmp/krb5cc_1051) Netsuite ODBC错误:pyodbc execute正在引发`[HY000]目录名称无效。”错误 - Netsuite ODBC Error: pyodbc execute is raising `[HY000] The directory name is invalid.` error 为什么我的查询出现HY000 pyodbc错误? - why do I get HY000 pyodbc error for my query? Pyodbc 错误“TVP 的行必须是序列对象。”,'HY000' - Pyodbc Error "A TVP's rows must be Sequence objects.", 'HY000' pyodbc.Error:找不到数据源名称,并且没有指定默认驱动程序(0)(SQLDriverConnect)') - pyodbc.Error: Data source name not found, and no default driver specified (0) (SQLDriverConnect)') 在Docker容器中使用Python和Mssql服务器时,如何解决“ pyodbc.Error:未指定驱动程序名称;” - How to fix “ pyodbc.Error: No driver name specified;” when using Python and Mssql server in docker container pyodbc.Error:('IM002','[IM002] [unixODBC] [驱动程序管理器]未找到数据源名称,并且未指定默认驱动程序(0)(SQLDriverConnect)') - pyodbc.Error: ('IM002', '[IM002] [unixODBC][Driver Manager]Data source name not found, and no default driver specified (0) (SQLDriverConnect)')
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM