python - ConfigParser pyodc 生成错误'无效 - 用户名 (12) (SQLDriverConnect)

标签 python configparser

抱歉,我是 Python 新手,正在尝试使用 ConfigParser 模块。我正在尝试通过配置文件将 netezza 连接到 python。我收到错误 invalid user ,这是不正确的,因为我已经验证了用户 ID 和密码。如果我直接在脚本中使用它而不是使用 ConfigParser 模块,下面格式中提到的 conn 可以正常工作

conn = pyodbc.connect("DRIVER= 
       {NetezzaSQL};SERVER=netezzadev02.xxx.com; 
       PORT=5460;DATABASE=EDWxx; 
       UID=anxxx;PWD=kkkkk;")

但是当我使用 configparser,ini 文件并开始创建下面的代码时,我收到错误' pyodbc.OperationalError: ('08001', u'[08001] Invalid - user name (12) (SQLDriverConnect)')' 。 我在下面解释我的代码。 ---先创建ini文件

import pyodbc                                                    
import configparser
config = configparser.ConfigParser()
config['NETEZZA'] = {'DRIVER': 'NetezzaSQL',
                    'SERVER': 'netezzadev02.xxx.com',
                    'DATABASE': 'EDWxx',
                    'PORT': '5460',
                    'UID': 'anxxx',
                    'PWD': 'kkkkk;',
}
with open('db_connect.ini', 'w') as configfile:
config.write(configfile)

在主 Python 脚本中添加 ini 文件以加载 netezza 日志记录凭据。

import configparser
print('\nEstablishing DB Connection..........')
config = configparser.ConfigParser()    
config.read('db_connect.ini')
constr = 'DRIVER={{{drv}}};SERVER={srv};DATABASE= 
                 {db};PORT={prt},UID={uid},PWD={pwd};'\
                  .format(drv=config['NETEZZA'['DRIVER'],
                  srv=config['NETEZZA']['SERVER'],
                  db=config['NETEZZA']['DATABASE'],
                  prt=config['NETEZZA']['PORT'],
                  uid=config['NETEZZA']['UID'],
                  pwd=config['NETEZZA']['PWD'])
conn = pyodbc.connect(constr)

请帮助我解决此错误或指出我犯了什么错误。

最佳答案

您可能想尝试 configparser 的内置 get(section, option) 函数,因为我不确定您实现它的方式是否有效。

因此,不要使用 config['NETEZZA'['DRIVER'] 而是 config.get('NETEZZA','DRIVER')

另外,我不知道你是否意识到这一点,但你在这里缺少一个“]”drv=config['NETEZZA'

关于python - ConfigParser pyodc 生成错误'无效 - 用户名 (12) (SQLDriverConnect),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55167984/

相关文章:

python - NLTK最大熵分类器原始分数

python - 如何修复 Django 文件上传中的 "Iterators should return strings, not bytes"

python - pandas 中具有多索引的 groupby 的平均值

python - 没有部分的 Configparser 集

python - 忽略配置文件解析器python中不存在的属性

python - Django REST 框架 : method PUT not allowed in ViewSet with def update()

python - 更新 3D 数组时的意外行为

Python Config Parser 找不到部分?

python - 有没有办法让 ConfigParser 'not' 返回列表

python - 在 python 包中添加和读取 config.ini 文件