Python 创建SQLAlchemy引擎时出现Pyodbc错误

Python 创建SQLAlchemy引擎时出现Pyodbc错误,python,sql,sqlalchemy,connection-string,Python,Sql,Sqlalchemy,Connection String,我正试图将名为df的数据帧写入SQL Express中的一个表中,如下代码所示,但我得到的错误是DBAPIError:(pyodbc.error)('IM002','[IM002][Microsoft][ODBC Driver Manager]未找到数据源名称,并且未指定默认驱动程序(0)(SQLDriverConnect)'))行中的engine=sqlalchemy.create\u engine('mssql://LENOVO-PC\SQlEXPRESS\\SQlEXPRESS/Datab

我正试图将名为
df
的数据帧写入SQL Express中的一个表中,如下代码所示,但我得到的错误是
DBAPIError:(pyodbc.error)('IM002','[IM002][Microsoft][ODBC Driver Manager]未找到数据源名称,并且未指定默认驱动程序(0)(SQLDriverConnect)'))
行中的
engine=sqlalchemy.create\u engine('mssql://LENOVO-PC\SQlEXPRESS\\SQlEXPRESS/Databasewithinfo?trusted_connection=yes')
。我从中看到了答案,并试着去理解。我知道我的
server\u name=LENOVO-PC\SQlEXPRESS
database\u 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)

谢谢

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

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

我注意到您提到的帖子在连接字符串中只包含一次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

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

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

我注意到您提到的帖子在连接字符串中只包含一次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