简体   繁体   English

通过dsn错误进行ODBC连接

[英]ODBC connection through dsn error

I have an issue trying to call my uid and password through pyodbc.connect 我尝试通过pyodbc.connect调用我的uid和密码时遇到问题

Here is my odbc.ini: 这是我的odbc.ini:

[my_dsn]
Driver= SQL Server
Server=my_server
User=uid_reader
Password=password_reader
MultiSubnetFailover=Yes
Database=master

When I hard code, it works perfectly and I can connect 当我进行硬编码时,它可以完美地工作并且可以连接

test_uid = 'uid_reader'
test_password = 'password_reader'

conn = pyodbc.connect(r'DSN=my_dsn;UID={a};PWD={b}'.format(a=test_uid,b=test_password))

When I call my dsn variables from my odbc.ini, it doesn't work 当我从odbc.ini调用dsn变量时,它不起作用

conn = pyodbc.connect(r'DSN=my_dsn;UID=User;PWD=Password')

Error: ('28000', "[28000] [unixODBC][Microsoft][ODBC Driver 11 for SQL Server][SQL Server]Login failed for user 'User'. (18456) (SQLDriverConnect)") 错误:(“ 28000”,“ [28000] [unixODBC] [Microsoft] [用于SQL Server的ODBC驱动程序11] [SQL Server]用户“用户”的登录失败。(18456)(SQLDriverConnect)”)

I would like to hide the password in my odbc.ini so it doesn't appear when I call pyodbc.connect 我想将密码隐藏在odbc.ini中,这样当我调用pyodbc.connect时它不会出现

The line conn = pyodbc.connect(r'DSN=my_dsn;UID=User;PWD=Password') seems to be using the literal string values of my_dsn , User and Password . conn = pyodbc.connect(r'DSN=my_dsn;UID=User;PWD=Password')似乎正在使用字符串my_dsnUserPassword You can try using keywords like the documentation here , or do a similar string formatting as you did for your hard coded solution but replace it with the loaded config data. 您可以尝试使用此处的文档之类的关键字,也可以像对硬编码解决方案那样使用类似的字符串格式,但是将其替换为已加载的配置数据。

From the documentation it seems you should be able to load the DSN info directly from the ini, but I haven't had much success on my own either. 文档看来,您应该应该可以直接从ini加载DSN信息,但是我自己也没有取得太大的成功。 The caveat is the document only said you can possibly use the login credential, so maybe it depends on the driver? 需要说明的是该文件只说你所能使用的登录凭据,所以也许它取决于驱动程序?

Either way, below is a version using configparser that should work. 无论哪种方式,下面都是使用configparser的版本,该版本应该可以工作。 It might be considered a hack for some. 对于某些人来说,它可能被认为是骇客。

import pyodbc
from configparser import ConfigParser

config = ConfigParser()
config.read(configfile)   # your odbc.ini file path and name
dsn = config['my_dsn']

#If you want to use Keywords:

conn = connect(driver=dsn['driver'], server=dsn['server'], database=dsn['database'], uid=dsn['user'], pwd=dsn['password'])

#If you want to use string formatting with list comprehension instead:
conn = pyodbc.connect(''.join(['{0}={1}; '.format(k, l) for k, l in d.items()])

I had the same issue. 我遇到过同样的问题。

https://github.com/mkleehammer/pyodbc/wiki/Connecting-to-SQL-Server-from-Windows#using-a-dsn https://github.com/mkleehammer/pyodbc/wiki/Connecting-to-SQL-Server-from-Windows#using-a-dsn

You can connect to your SQL Server instance using a trusted connection, ie using your Windows account rather than a login name and password, by using the Trusted_Connection attribute 您可以使用Trusted_Connection属性使用受信任的连接(即使用Windows帐户而不是登录名和密码)连接到SQL Server实例。

conn = pyodbc.connect('DSN=mynewdsn;Trusted_Connection=yes;')

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM