python - sqlalchemy.exc.NoSuchColumnError : "Could not locate column in row for column ' 0'"

标签 python sql sqlalchemy psycopg2

所以我有这段代码可以使用 python 从 Linux 服务器连接到 redshift。 当我尝试从 Redshift 服务器获取表时,它向我抛出 sqlalchemy.exc.NoSuchColumnError:“无法在行中找到列 '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)

最佳答案

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

pip install SQLAlchemy==1.1.5

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

关于python - sqlalchemy.exc.NoSuchColumnError : "Could not locate column in row for column ' 0'",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42565756/

相关文章:

sql - ORA-00933 与内部联接混淆和 "as"

sql - Postgres : aggregation function that returns a column

python - "This session is in ' 在线程 mod_wsgi 应用程序中使用 scoped_session 准备 ' state; no further"错误与 SQLAlchemy

database - SQLAlchemy - 将一个类映射到多个表,其中一个是只读的

python - 如何使用 MySql 上的 Python Django 让应用程序在 Heroku 上运行? KeyError : '' , 代码=H10 描述 ="App crashed"

python - xlsxwriter 未将格式应用于数据框的标题行 - Python Pandas

mysql - SQL查询删除

sql-server - SQLAlchemy 和 mssql : how to create unique constraint with multiple null value

python - 如何从 FastAPI 文档中隐藏 Pydantic 鉴别器字段

python - 删除新换行