所以我有这段代码可以使用 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/