对不起,我是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',你'[08001]无效 - 用户名(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'