Python PYODBC--未找到数据源名称且未指定默认驱动程序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/46045834/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PYODBC--Data source name not found and no default driver specified
提问by user8560985
import pyodbc
connection = pyodbc.connect('Driver = {SQL Server};Server=SIWSQL43A\SIMSSPROD43A;'
'Database=CSM_reporting;Trusted_Connection=yes;')
Error:
错误:
connection = pyodbc.connect('Driver = {SQL Server};Server=SIWSQL43A\SIMSSPROD43A;'
pyodbc.Error: ('IM002', '[IM002] [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified (0) (SQLDriverConnect)')
回答by Gord Thompson
Do not put a space after the Driver
keyword in the connection string.
不要Driver
在连接字符串中的关键字后放置空格。
This fails on Windows ...
这在 Windows 上失败...
conn_str = (
r'DRIVER = {SQL Server};'
r'SERVER=(local)\SQLEXPRESS;'
r'DATABASE=myDb;'
r'Trusted_Connection=yes;'
)
cnxn = pyodbc.connect(conn_str)
... but this works:
...但这有效:
conn_str = (
r'DRIVER={SQL Server};'
r'SERVER=(local)\SQLEXPRESS;'
r'DATABASE=myDb;'
r'Trusted_Connection=yes;'
)
cnxn = pyodbc.connect(conn_str)
回答by Avnish alok
I'm using
我正在使用
Django 2.2
Django 2.2
and got the same error while connecting to sql-server 2012. Spent lot of time to solve this issue and finally this worked.
并在连接到sql-server 2012时遇到相同的错误。花了很多时间来解决这个问题,终于成功了。
I changed
我变了
'driver': 'ODBC Driver 13 for SQL Server'
“驱动程序”:“用于 SQL Server 的 ODBC 驱动程序 13”
to
到
'driver': 'SQL Server Native Client 11.0'
“驱动程序”:“SQL Server 本机客户端 11.0”
and it worked.
它奏效了。
回答by Kashq
I've met same problem and fixed it changing connection string like below. Write
我遇到了同样的问题并修复了它改变连接字符串,如下所示。写
'DRIVER={ODBC Driver 13 for SQL Server}'
instead of
代替
'DRIVER={SQL Server}'
回答by dady7749
You could try:
你可以试试:
import pyodbc
# Using a DSN
cnxn = pyodbc.connect('DSN=odbc_datasource_name;UID=db_user_id;PWD=db_password')
Note: You will need to know the "odbc_datasource_name". In Windows you can search for ODBC Data Sources. The name will look something like this:
注意:您需要知道“odbc_datasource_name”。在 Windows 中,您可以搜索 ODBC 数据源。该名称将如下所示:
回答by Ustin
I faced this issue and was looking for the solution. Finally I was trying all the options from the https://github.com/mkleehammer/pyodbc/wiki/Connecting-to-SQL-Server-from-Windows, and for my MSSQL 12 only "{ODBC Driver 11 for SQL Server}" works. Just try it one by one. And the second important thing you have to get correct server name, because I thought preciously that I need to set \SQLEXPRESS in all of the cases, but found out that you have to set EXACTLY what you see in the server properties. Example on the screenshot:
我遇到了这个问题并正在寻找解决方案。最后,我尝试了https://github.com/mkleehammer/pyodbc/wiki/Connecting-to-SQL-Server-from-Windows 中的所有选项,仅适用于我的 MSSQL 12“{ODBC Driver 11 for SQL Server} "作品。就一一试试吧。第二件重要的事情你必须得到正确的服务器名称,因为我认为我需要在所有情况下设置 \SQLEXPRESS,但发现你必须完全设置你在服务器属性中看到的内容。屏幕截图示例:
回答by Tarek Salha
Apart from the other answers, that considered the connection string itself, it might simply be necessary to download the correct odbc driver. My client just faced this issue when executing a python app, that required it. you can check this by pressing windows + typing "odbc". the correct driver should appear in the drivers tab.
除了考虑连接字符串本身的其他答案之外,可能只需要下载正确的 odbc 驱动程序。我的客户在执行需要它的 python 应用程序时刚刚遇到了这个问题。您可以通过按 windows + 键入“odbc”来检查这一点。正确的驱动程序应出现在驱动程序选项卡中。
回答by crazyX
Have you installed any product of SQL in your system machine ? You can download and install "ODBC Driver 13(or any version) for SQL Server" and try to run if you havent alerady done.
你的系统机器上有没有安装过SQL的任何产品?您可以下载并安装“用于 SQL Server 的 ODBC 驱动程序 13(或任何版本)”,如果您还没有完成,请尝试运行。
回答by Patrick
The below code works magic.
下面的代码很神奇。
SQLALCHEMY_DATABASE_URI = "mssql+pyodbc://<servername>/<dbname>?driver=SQL Server Native Client 11.0?trusted_connection=yes?UID" \
"=<db_name>?PWD=<pass>"
回答by Srinivasan
Try below:
试试下面:
import pyodbc
server = 'servername'
database = 'DB'
username = 'UserName'
password = 'Password'
cnxn = pyodbc.connect('DRIVER={ODBC Driver 13 for SQL Server};SERVER='+server+';DATABASE='+database+';UID='+username+';PWD='+ password)
cursor = cnxn.cursor()
cursor.execute('SELECT * FROM Tbl')
for row in cursor:
print('row = %r' % (row,))
回答by Gurpreet Deol
Below connection string is working
下面的连接字符串正在工作
import pandas as pd
import pyodbc as odbc
sql_conn = odbc.connect('DRIVER={ODBC Driver 13 for SQL Server};SERVER=SERVER_NAME;DATABASE=DATABASE_NAME;UID=USERNAME;PWD=PASSWORD;')
query = "SELECT * FROM admin.TABLE_NAME"
df = pd.read_sql(query, sql_conn)
df.head()