无法连接sqlalchemy

时间:2019-04-04 19:58:41

标签: sql-server sqlalchemy pyodbc

我正在尝试以最简单的方式来建立和测试关系数据库。我可以通过pyodbc通过

进行连接
conn = pyodbc.connect(
    'DRIVER={ODBC Driver 17 for SQL Server};SERVER=USER-LT\SQLEXPRESS'
    'DATABASE=DBTEST;Trusted_Connection=yes;')

但是当我在sqlalchemy中做同样的事情时,

params = urllib.parse.quote_plus(r'DRIVER={ODBC Driver 17 for SQL Server};'
    'SERVER=USER-LT\SQLEXPRESS;DATABASE=DBTEST;Trusted_Connection=yes;')
conn_str = 'mssql+pyodbc:///?odbc_connect={}'.format(params)
engine = create_engine(conn_str)
engine.connect()

我得到了错误

sqlalchemy.exc.ProgrammingError: (raised as a result of Query-invoked autoflush;
consider using a session.no_autoflush block if this flush is occurring prematurely)
(pyodbc.ProgrammingError) ('42S02', "[42S02] [Microsoft][ODBC Driver 17 for SQL Server]
[SQL Server]Invalid object name 'test'. (208) (SQLExecDirectW); 
[42S02] [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]
Statement(s) could not be prepared. (8180)")

我也尝试过

engine = create_engine('mssql+pyodbc://@USER-LT/SQLEXPRESS/RAMF_SM?trusted_connection'
    '=yes&driver=ODBC+Driver+17+for+SQL+Server', echo=True)

没有成功。

0 个答案:

没有答案