【问题标题】:Pyodbc Error when creating SQLAlchemy Engine创建 SQLAlchemy 引擎时出现 Pyodbc 错误
【发布时间】:2015-09-04 11:01:52
【问题描述】:

我正在尝试将名为 df 的 Pandas 数据框写入 SQL Express 中的表中,如下面的代码所示,但在 engine = sqlalchemy.create_engine('mssql://LENOVO-PC\SQlEXPRESS\\SQLEXPRESS/Databasewithinfo?trusted_connection=yes') 行中出现错误 DBAPIError: (pyodbc.Error) ('IM002', '[IM002] [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified (0) (SQLDriverConnect)')。我在this post 中看到了答案,并试图遵循它。我知道我的server_name = LENOVO-PC\SQlEXPRESSdatabase_name = Databasewithinfo,因此我很难理解我哪里出错了。

import sqlalchemy
from sqlalchemy import create_engine
engine = sqlalchemy.create_engine('mssql://LENOVO-PC\SQlEXPRESS\\SQLEXPRESS/Databasewithinfo?trusted_connection=yes')
df.to_sql('JPY_data', engine, chunksize=1000)

谢谢

【问题讨论】:

    标签: python sql sqlalchemy connection-string


    【解决方案1】:

    这不是一个直接的答案,而是一个测试连接变体直到它工作的工具包。

    您真的想尽可能多地抛出连接字符串变体,直到某些东西起作用。我已经放了两个。

    我确实注意到你提到的帖子在连接字符串中只有一次 SQLEXPRESS,不像你。

    import sqlalchemy
    from sqlalchemy import create_engine
    
    def test_it(t_connect_string):
    
        #these are your connection setting, they are constant
        di = dict(server="LENOVO-PC",database="Databasewithinfo")    
    
        connect_string = t_connect_string % di
    
        try:
            engine = sqlalchemy.create_engine(connect_string)
            print "%s Success!" %  (connect_string)
            return True
        except Exception, e:
            print "%s Exception=>%s" %  (connect_string, e)
            return False
    
    #put as many possible templates as you need until it connects, then you're good
    li_test = [
        """mssql://%(server)s\SQlEXPRESS\\SQLEXPRESS/%(database)s?trusted_connection=yes""",
    
        #the post you refer to seems to have this format instead...
        """mssql://%(server)s\\SQLEXPRESS/%(database)s?trusted_connection=yes """,
    
    ]
    
    #test them until something works.
    for test in li_test:
        result = test_it(test)
        if result:
            break
    

    这对我来说很糟糕,因为我没有安装 odbc,但希望你会得到更多相关的错误。

    mssql://LENOVO-PC\SQlEXPRESS\SQLEXPRESS/Databasewithinfo?trusted_connection=yes Exception=>No module named pyodbc
    mssql://LENOVO-PC\SQLEXPRESS/Databasewithinfo?trusted_connection=yes  Exception=>No module named pyodbc
    

    【讨论】:

      猜你喜欢
      • 2021-07-12
      • 1970-01-01
      • 1970-01-01
      • 2020-07-02
      • 2020-03-31
      • 2023-01-24
      • 2016-01-18
      • 2012-02-10
      • 1970-01-01
      相关资源
      最近更新 更多