【问题标题】:sqlalchemy.exc.NoSuchColumnError: "Could not locate column in row for column '0'"sqlalchemy.exc.NoSuchColumnError:“无法在行中找到列‘0’的列”
【发布时间】:2017-07-22 18:56:03
【问题描述】:

所以我有这段代码可以使用来自 linux 服务器的 python 连接到 redshift。 当我尝试从 redshift 服务器获取表时,它向我抛出 sqlalchemy.exc.NoSuchColumnError: "Could not locate column in row for column '0'" 错误。

其他经历过类似错误的人通过更新python包解决了这个问题,我今天早些时候做了所有的事情,但我仍然收到错误。

版本:

Python 2.7.12

SQLAlchemy 1.1.6

sqlalchemy-redshift 0.5.0

psycopg2 2.7

这是代码,我使用的是 console_login:

from sqlalchemy import create_engine
import getpass
import json
def getredshiftconnect(console_login=True, login_credentials_file=None):
try:
    if console_login:
        login = getpass.getpass("Username: ")
        password = getpass.getpass("Password: ")
        server = input("Server: ")
        database = input("Database connecting to: ")
        schema = input("Schema connecting to: ")
        engine = create_engine("redshift+psycopg2://{0}:{1}@{2}:<PortNumber>/{3}"\
                                   .format(login, password, server, database))
    else:
        # Read JSON file with login credentials
        # Make sure that fields are referenced correctly, i.e.:
        # login, password, server, database
        login_credentials = json.load(open(login_credentials_file,"r"))
        engine = create_engine("redshift+psycopg2://{0}:{1}@{2}:<PortNumber>/{3}"\
                                   .format(login_credentials["login"],
                                           login_credentials["password"],
                                           login_credentials["server"],
                                           login_credentials["database"]))
    # To test connection, if able to list tables then login was succesful
    # Pick a schema that actually has tables
    if engine.table_names(schema): # Explodes at this call
        print ("Successfully connected")
        return engine
    else:
        print ("Not properly connected")
except Exception as e:
    print ("Error Connecting: ", e)

【问题讨论】:

    标签: python sql sqlalchemy psycopg2


    【解决方案1】:

    我确实发现 SQLAlchemy-1.1.6 是问题的原因。 降级到 1.1.5 后,在我的情况下它又可以正常工作了。

    pip install SQLAlchemy==1.1.5
    

    注意:我发现最新版本的 SQLAlchemy 有更多错误。 此外,我必须在某些服务器上删除并重新安装 SQLAlchemy 才能解决问题(例如 pip uninstall SQLAlchemy)。

    【讨论】:

    • 也解决了我的问题。但就我而言,我使用的是 sqlalchemy 1.1.9。谢谢
    • 2020 年更新。遇到了同样的问题。通过从 1.3.18 降级解决它 ==> 1.2.15
    猜你喜欢
    • 1970-01-01
    • 2021-08-09
    • 1970-01-01
    • 1970-01-01
    • 2013-02-20
    • 1970-01-01
    • 1970-01-01
    • 2014-12-19
    • 2017-02-13
    相关资源
    最近更新 更多