通过DSN连接失败,用"无效的用户名或密码",但DSN确实拥有正确的用户名和密码

时间:2015-05-05 17:38:39

标签: python pyodbc vertica

我尝试使用以下小脚本从OS / X 10.10.2连接到vertica数据库:

#!/usr/bin/python

import sys

import pyodbc

def usage(retval):
    if retval == 0:
        file_ = sys.stdout
    else:
        file_ = sys.stderr

    file_.write('Usage: {} --query sql_query --dsn data_source_name\n'.format(sys.argv[0]))

    sys.exit(retval)

def main():
    query = None
    dsn = None
    while sys.argv[1:]:
        if sys.argv[1] == '--query':
            query = sys.argv[2]
            del sys.argv[1]
        elif sys.argv[1] == '--dsn':
            dsn = sys.argv[2]
            del sys.argv[1]
        elif sys.argv[1] in {'-h', '--help'}:
            usage(0)
        else:
            sys.stderr.write('{}: Unrecognized option: {}\n'.format(sys.argv[0], sys.argv[1]))
            usage(1)
        del sys.argv[1]

    if query is None:
        sys.stderr.write('{}: --query is a required option\n'.format(sys.argv[0]))
        usage(1)

    if dsn is None:
        sys.stderr.write('{}: --dsn is a required option\n'.format(sys.argv[0]))
        usage(1)

    connection_string = 'DSN={}'.format(dsn)
    sys.stderr.write('connection_string is {}\n'.format(connection_string))
    connection = pyodbc.connect(connection_string)
    cursor = connection.cursor()
    cursor.execute(query)

    # Fetchall might be faster, but it also has memory limit issues
    for row in cursor:
        print(row)


main()

像这样调用这个脚本:

ODBCINI=/Library/ODBC/odbc.ini /simple-connect --query 'select * from GLITCH.METRIC' --dsn GLITCH_ADMIN

...失败了:

21L, 1L, u"FAILED: ('28000', '[28000] FATAL 3781:  Invalid username or password\\n (3781) (SQLDriverConnect)')", u'unique_seconds', ...

...在同一行加上一些文字,还有一行我已删除以保护。

我有另一个(更大)程序使用相同的DSN连接正常,而不提示输入用户名或密码。

/Library/ODBC/odbc.ini中的相关DSN(主机名,用户名和密码已更改以保护):

Driver      = Vertica
Description = Glitch Vertica Meta Data DSN
Database    = Analytics
Servername  = hostname
UID         = username
PWD         = password
PORT        = 5433
SSLMode     = require

我搜索了脚本,发现它正在/Library/ODBC/odbc.ini中查找 - 或者更确切地说,它适当地设置了ODBCINI环境变量。没有那个变量,它似乎忽略了我的odbc.ini。

有谁知道为什么它无法进行身份验证?

谢谢!

0 个答案:

没有答案