【发布时间】:2019-03-14 16:48:20
【问题描述】:
抱歉,我是 Python 新手,正在尝试使用 ConfigParser 模块。我正在尝试通过配置文件将 netezza 连接到 python。我收到错误无效用户,这是不正确的,因为我已经验证了用户 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)
请帮助我解决此错误或指出我在做什么。
【问题讨论】:
标签: python configparser