[英]I'm trying to connect to a SQL Express Server on my machine using sqlalchemy but I'm getting an error
I'm trying to connect to a SQL Express Server on my machine using sqlalchemy but I'm getting an error.我正在尝试使用 sqlalchemy 连接到我机器上的 SQL Express 服务器,但出现错误。 Here is the code I have:
这是我的代码:
import sqlalchemy as sal
import pandas as pd
sqlcon=sal.create_engine('mssql+pyodbc://@' + 'DESKTOP-A1BUCDR\SQLEXPRESS' + '/' + 'Northwind' + '?driver=ODBC+Driver+13+for+SQL+Server')
df = pd.read_sql_query('select * from Orders', sqlcon)
And here is the error I'm getting:这是我得到的错误:
InterfaceError: (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: https://sqlalche.me/e/14/rvf5)
Does anyone have any ideas on what I may be getting wrong here?有没有人对我在这里可能出错的事情有任何想法?
Your URL evaluates to:您的 URL 评估为:
mssql+pyodbc://@DESKTOP-A1BUCDR\\SQLEXPRESS/Northwind?driver=ODBC+Driver+13
+for+SQL+Server
The errors points to not finding the data source name (DSN), which I think you must not want to use since you give what looks like a hostname and a driver parameter.错误指向找不到数据源名称 (DSN),我认为您一定不想使用它,因为您提供了看起来像主机名和驱动程序参数的东西。
That's fine too:这也很好:
mssql+pyodbc://<UID>:<PWD>@DESKTOP-A1BUCDR:1433/Northwind?driver=ODBC+Driver+17+for+SQL+Server
If your port is the default (1433), you don't really need to add it.如果您的端口是默认端口 (1433),则您实际上不需要添加它。
Why is your hostname DESKTOP-A1BUCDR\SQLEXPRESS
?为什么您的主机名是
DESKTOP-A1BUCDR\SQLEXPRESS
? and why are you using ODBC Driver 13 for SQL Server
and not a more recent version ?为什么你使用
ODBC Driver 13 for SQL Server
而不是更新的版本?
You could instead try using a URL built for your hostname
or directly the pyODBC string
.您可以尝试使用
URL built for your hostname
或直接使用pyODBC string
。
The format to connect to a SQL Server Express using Windows Authentication is:使用 Windows 身份验证连接到 SQL Server Express 的格式是:
import sqlalchemy
sqlcon = sqlalchemy.create_engine('mssql://DESKTOP-A1BUCDR\SQLEXPRESS/AdventureWorks?driver=ODBC+Driver+17+for+SQL+Server')
DESKTOP-A1BUCDR\SQLEXPRESS is the servername DESKTOP-A1BUCDR\SQLEXPRESS 是服务器名
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.