简体   繁体   English

使用 Windows 身份验证在 Python 中使用 SQL Alchemy 连接到网络上的 MS SQL

[英]Connect to MS SQL on a network with SQL Alchemy in Python using Windows Authentication

I am trying to use pandas.read_sql_table to get data from MS SQL Server (the server is on a network).我正在尝试使用 pandas.read_sql_table 从 MS SQL Server 获取数据(服务器在网络上)。 I use Windows authentication to access the server.我使用 Windows 身份验证来访问服务器。 Pandas read_sql_table takes a SQL Alchemy connection as an argument for “connection.” Pandas read_sql_table 将 SQL Alchemy 连接作为“连接”的参数。 I am having a difficult time finding an example that combines:我很难找到一个结合了以下内容的示例:

  1. SQL Alchemy SQL 炼金术
  2. MS SQL Server微软 SQL 服务器
  3. DSN (the “preferred” specification according to SQL Alchemy) DSN(根据 SQL Alchemy 的“首选”规范)
  4. Windows authentication Windows 身份验证

I've consulted SQL Alchemy, which shows an example using SQL authentication, but not Windows authentication.我咨询了 SQL Alchemy,它显示了一个使用 SQL 身份验证的示例,但没有使用 Windows 身份验证。 http://docs.sqlalchemy.org/en/latest/dialects/mssql.html#connecting-to-pyodbc Here are various options I have tried. http://docs.sqlalchemy.org/en/latest/dialects/mssql.html#connecting-to-pyodbc以下是我尝试过的各种选项。 All return an error.都返回错误。

import pandas as pd
from sqlalchemy import create_engine
import pyodbc
# set some variables
dbname = 'mydbname'
schemaname = 'myschemaname'
servername = 'myservername'
tablename = ‘mytablename’

sqlcon = create_engine('mssql+pyodbc://@' + servername)
#sqlcon = create_engine('mssql+pyodbc://' + servername + '/' + dbname)
#sqlcon = create_engine('mssql+pyodbc://' + servername)
#sqlcon = create_engine('mssql://' + servername + '/' + dbname + '?trusted_connection=yes')
#sqlcon = create_engine('mssql+pyodbc://' + servername + '/' + dbname + '?trusted_connection=yes')
mydataframe = pd.read_sql_table(tablename,con=sqlcon,schema=schemaname)

The error I get is this:我得到的错误是这样的:

(pyodbc.InterfaceError) ('IM002', '[IM002] [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified (0) (SQLDriverConnect)') (Background on this error at: http://sqlalche.me/e/rvf5 ) (pyodbc.InterfaceError) ('IM002', '[IM002] [Microsoft][ODBC Driver Manager] 未找到数据源名称且未指定默认驱动程序 (0) (SQLDriverConnect)')(此错误的背景: http:// /sqlalche.me/e/rvf5 )

What is especially perplexing is the comment about no default driver being specified.特别令人困惑的是关于没有指定默认驱动程序的评论。 None of the examples refer to specifying a default driver when I use this DSN format.当我使用此 DSN 格式时,没有一个示例涉及指定默认驱动程序。

I have consulted this example, which also fails for me: How do I connect to SQL Server via sqlalchemy using Windows Authentication?我已经咨询过这个例子,这对我来说也失败了: How do I connect to SQL Server via sqlalchemy using Windows Authentication?

I can connect fine with SSMS.我可以与 SSMS 正常连接。 I'm using python 3.6.我正在使用 python 3.6。

I found a solution to my question.我找到了我的问题的解决方案。 Posting here for others' reference.贴在这里供其他人参考。

This code worked.此代码有效。 I wasn't able to avoid specifying a driver explicitly, though.不过,我无法避免明确指定驱动程序。

sqlcon = create_engine('mssql+pyodbc://@' + servername + '/' + dbname + '?driver=ODBC+Driver+13+for+SQL+Server')

If you are confused with driver version you can use below code如果您对驱动程序版本感到困惑,可以使用以下代码

username=<username>
password=<password>
server=<servername> | <host:port> 
database=<database>

#below code is with authentication
engine = create_engine('mssql+pyodbc://'+username+':'+password+'@' + server + '/' + database + '?driver=SQL+Server')
query = '''select * from <table>'''
result = engine.execute(query)


#below code is without authentication
engine = create_engine('mssql+pyodbc://@' + server + '/' + database + '?driver=SQL+Server')
query = '''select * from <table>'''
result = engine.execute(query)

暂无
暂无

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

相关问题 如何使用Active Directory集成身份验证通过python SQL炼金术连接到Azure sql数据库 - How to connect to Azure sql database with python SQL alchemy using Active directory integrated authentication 如何连接到 sql azure 数据库与 python Z9778840A0100CB30C982876741B0B5yA 使用集成的身份验证 - How to connect to the sql azure database with python SQL alchemy using active directory integrated authentication 如何使用 Python 和 Windows 身份验证从 WSL2 连接到非本地 MS SQL 服务器? - How to connect to non-local MS SQL Server from WSL2 using Python and Windows Authentication? 无法在Windows 7上的Python中使用PYODBC连接到远程MS SQL服务器 - Cant Connect to remote MS SQL server using PYODBC on Windows 7 in Python 如何使用Windows身份验证从Python中的另一个域的Windows工作站连接到MS SQL服务器 - How to use windows authentication to connect to MS SQL server from windows workstation in another domain with Python 使用pyodbc将Python连接到MS SQL Server - Connect Python to MS SQL Server using pyodbc 如何从 Python + SQL Alchemy 连接到高可用性 SQL Server - How to connect to a high availability SQL Server from Python + SQL Alchemy 使用SQL alchemy连接到SQL Server而不使用DATABASE名称 - connect to SQL Server using SQL alchemy without using DATABASE name 如何使用AD通用身份验证将Python的pyodbc连接到MS SQL? - How to connect Python's pyodbc to MS SQL using AD Universal Authentication? SQL Alchemy:无法使用 engine.connect() 进行连接 - SQL Alchemy : cannot connect using engine.connect()
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM