SQL 炼金术 SQL 服务器

SQL Alchemy with SQL Server

我不确定我的 post 的标题是什么,如果您有更好的想法,请随时编辑标题。

我之前没有使用过 SQL Alchemy,我查看的位于以下位置的文档没有帮助:

这是我使用的代码:

import sqlalchemy as sal
from sqlalchemy import create_engine
#Here are the parameters I am using:

 - server = 'Q-20/fake_example'
 - database = 'AdventureWorks2017'
 - driver = 'ODBC Driver 17 for SQL Server' 
 - trusted_connection='yes'

DATABASE_CONNECTION = 'mssql+pyodbc://@server = ' + server + '/database = ' + database + '?trusted_connection = ' + trusted_connection + '&driver=' + driver

engine = sal.create_engine(DATABASE_CONNECTION)

所有这些似乎工作正常,没有任何问题;然而,当我添加这一行时:

connection=engine.connect()

我收到以下错误消息:

sqlalchemy.exc.OperationalError: (pyodbc.OperationalError) ('08001', '[08001] [Microsoft][ODBC Driver 17 for SQL Server]Named Pipes Provider: Could not open a connection to SQL Server [53]. (53) (SQLDriverConnect); [08001] [Microsoft][ODBC Driver 17 for SQL Server]Login timeout expired (0); [08001] [Microsoft][ODBC Driver 17 for SQL Server]Invalid connection string attribute (0); [08001] [Microsoft][ODBC Driver 17 for SQL Server]A network-related or instance-specific error has occurred while establishing a connection to SQL Server. Server is not found or not accessible. Check if instance name is correct and if SQL Server is configured to allow remote connections. For more information see SQL Server Books Online. (53)')

我不确定我在做什么有什么问题,有人有什么建议吗?

到目前为止我已经尝试过:

  1. 我已确认 SQL 服务器配置为允许远程连接。我按照说明 here
  2. 进行了此项检查
  3. 删除服务器前的“@”符号,但这只会生成相同的错误消息。

我想出了我需要做的部分事情。我需要更改我的参数。

旧参数:

  • 服务器='Q-20/fake_example'
  • 数据库='AdventureWorks2017'
  • driver = 'ODBC Driver 17 for SQL Server'
  • trusted_connection='yes'

新参数:

  • 服务器='Q-20'
  • 数据库='AdventureWorks2017'
  • driver = 'SQL+SERVER+NATIVE+CLIENT+11.0'
  • trusted_connection='yes'

这是我的代码最终的样子:

database_connection =  'mssql+pyodbc://Q-20/AdventureWorks2017?trusted_connection=yes&driver=SQL+SERVER+NATIVE+CLIENT+11.0'