You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

在Replit中使用SQLAlchemy报错:‘Engine’对象无‘table_names’属性

解决SQLAlchemy中'Engine' object has no attribute 'table_names'错误

你的问题源于SQLAlchemy的版本差异:旧版本(1.3及更早)中Engine对象提供table_names()方法,但1.4+的新版本已经移除了这个API,改用Inspector类来获取数据库表信息。DataCamp的教程可能基于旧版SQLAlchemy,所以直接照搬会报错。

以下是两种可行的解决方法:

方法一:使用官方推荐的Inspector类(优先选择)

修改代码为:

from sqlalchemy import create_engine
from sqlalchemy.inspect import Inspector

engine = create_engine('sqlite:///census_nyc.sqlite')
connection = engine.connect()

# 初始化Inspector实例
inspector = Inspector.from_engine(engine)
# 获取所有表名并打印
print(inspector.get_table_names())

方法二:直接执行SQL查询获取表名(兼容SQLite)

如果不想引入Inspector类,也可以通过执行原生SQL查询来获取表名:

from sqlalchemy import create_engine

engine = create_engine('sqlite:///census_nyc.sqlite')
connection = engine.connect()

# 针对SQLite的表名查询语句
result = connection.execute("SELECT name FROM sqlite_master WHERE type='table';")
tables = [row[0] for row in result]
print(tables)

内容的提问来源于stack exchange,提问作者Luke

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.24 04:14:55