简体   繁体   English

创建SQLAlchemy引擎时出现Pyodbc错误

[英]Pyodbc Error when creating SQLAlchemy Engine

I am trying to write a Pandas Dataframe called df into a Table in SQL Express as in the code below, but I get the error DBAPIError: (pyodbc.Error) ('IM002', '[IM002] [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified (0) (SQLDriverConnect)') in the line engine = sqlalchemy.create_engine('mssql://LENOVO-PC\\SQlEXPRESS\\\\SQLEXPRESS/Databasewithinfo?trusted_connection=yes') . 我正在尝试将一个名为df的Pandas数据帧写入SQL Express中的表,如下面的代码所示,但出现错误DBAPIError: (pyodbc.Error) ('IM002', '[IM002] [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified (0) (SQLDriverConnect)')在行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)') engine = sqlalchemy.create_engine('mssql://LENOVO-PC\\SQlEXPRESS\\\\SQLEXPRESS/Databasewithinfo?trusted_connection=yes') I saw the answer in this post and tried to follow that. 我在这篇文章中看到了答案,并尝试遵循该答案。 I know that my server_name = LENOVO-PC\\SQlEXPRESS and that database_name = Databasewithinfo and am thus struggling to understand where I'm going wrong. 我知道我的server_name = LENOVO-PC\\SQlEXPRESS ,而database_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)

Thank You 谢谢

This isn't directly an answer, but what a toolkit to test connection variants until it works. 这不是直接的答案,而是测试连接变量直到有效的工具包。

You really want to throw as many connect strings variants as you can at it until something works. 您确实想抛出尽可能多的连接字符串变体,直到有效果为止。 I've put in two already. 我已经放了两个。

I did notice the post you refer to only has SQLEXPRESS once in the connect string, unlike you. 我确实注意到您所引用的帖子在连接字符串中仅包含一次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

It blows up for me, because I don't odbc installed, but you'll get more relevant errors hopefully. 它对我来说很麻烦,因为我没有安装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

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM