sqlalchemy.exc.ArgumentError: 列 参数 select() 必须是 Python 列表或其他



Python 和 SQLquery 的新功能。我正在尝试查询 mssql 中的现有表。第一个任务是显示表中的内容,我还想做的另一个 tasK 是做一个 exists(( 语句来检查表中的数据是否存在。到目前为止,这些是我的代码片段:

def dbhandler():
params = urllib.parse.quote_plus(db_string)
engine = db.create_engine("mssql+pyodbc:///?odbc_connect={}".format(params))
connection = engine.connect()
metadata = db.MetaData()
odfs_tester_history_files = db.Table('odfs_tester_history_files', metadata, autoload=True, autoload_with=engine)
odfs_tester_history = db.Table('odfs_tester_history', metadata, autoload=True, autoload_with=engine)
tables_dict = {'odfs_tester_history_files': {'engine': engine, 'connection': connection, 'table': odfs_tester_history_files}, 'odfs_tester_history': {'engine': engine, 'connection': connection, 'table': odfs_tester_history}}
#return {'engine': engine, 'connection': connection, 'table': odfs_tester_history_files}
return tables_dict
db_instance = dbhandler()
odfs_tabletest_dict = db_instance['odfs_tester_history']
foo_col = db.sql.column('CSV_FILENAME')
sql = db.select(odfs_tabletest_dict['table']).where(odfs_tabletest_dict['odfs_tester_history'].c.foo_col == '06_16_2020_FMGN519.csv')
df = pd.read_sql(sql, odfs_tabletest_dict['connection'])
print(df)

但是,当我运行代码时,我收到以下错误:sqlalchemy.exc.ArgumentError:列参数到select((必须是Python列表或其他可迭代对象。我该如何解决这个问题?另外,我如何使用 exists 语句来检查有问题的数据,.csv文件是否在表中,并只返回一个 true 或 false 值供我用作重复检查?

如果有人遇到此问题,我通过在表对象周围放置方括号来修复它。我不知道为什么会这样,我的导师也不知道。他有类似的代码,它对他有用,不需要 [ ]。

sql = db.select([odfs_tabletest_dict['table']]).where(odfs_tabletest_dict['table'].c.CSV_FILENAME == '06_16_2020_FMGN519.csv')
df = pd.read_sql(sql, odfs_tabletest_dict['connection'])

Gord Thompson对此的评论帮助我解决了这个问题。我升级到SQLAlchemy 1.4.11并摆脱了这个问题。

如果有人仍然面临这个问题,正如前面的答案所提到的,请注意版本,

我使用的是"1.3.10"版本,您需要的是将 model.columns 传递给 select((

model = db.Table('my_table_name', metadata, autoload=True, autoload_with=engine)
query = db.select(model.columns)

最新更新