我目前这样做:
#!/usr/bin/env python
# 3rd party modules
from sqlalchemy import create_engine # requires pymssql
# local modules
from config import cfg
connection_string = 'mssql+pymssql://{user}:{password}@{host}:{port}/{db}'
engine = create_engine(connection_string
.format(host=cfg['db']['host'],
db=cfg['db']['database'],
user=cfg['db']['user'],
password=cfg['db']['password'],
port=cfg['db']['port'],
schema=cfg['db']['schema']))
with engine.begin() as conn:
sql = ('SELECT foo FROM bar;')
rows = conn.execute(sql)
print(rows)
但我明白了
File "/usr/local/lib/python3.5/dist-packages/sqlalchemy/engine/default.py", line 470, in do_execute
cursor.execute(statement, parameters)
File "pymssql.pyx", line 464, in pymssql.Cursor.execute (pymssql.c:7491)
sqlalchemy.exc.ProgrammingError: (pymssql.ProgrammingError) (208, b"Invalid object name 'bar'.DB-Lib error message 20018, severity 16:\nGeneral SQL Server error: Check messages from the SQL Server\n") [SQL: 'SELECT foo FROM bar;']
我认为问题是我必须使用架构exampleschema.我可以通过DBeaver使用相同的凭据访问架构exampleschema中带有列foo的表栏.
但是当我将/ {schema}添加到连接字符串时,我得到了
sqlalchemy.exc.OperationalError: (pymssql.OperationalError)
(18456,
b"Login failed for user 'exampleuser'.DB-Lib error message 20018,
severity 14:\nGeneral SQL Server error: Check messages from the SQL Server\nDB-Lib error message 20002,
severity 9:\nAdaptive Server connection failed (192.168.123.456:1433)\n")
如何设置架构?