sql - 在python中查询多个postgres表
问题描述
我正在尝试查询多个 sql 表并将它们存储为 pandas 数据框。
cur = conn.cursor()
cur.execute("select relname from pg_class where relkind='r' and relname !~ '^(pg_|sql_)';")
tables_df = cur.fetchall()
##table_name_list = tables_df.table_name
select_template = ' SELECT * FROM {table_name}'
frames_dict = {}
for tname in tables_df :
query = select_template.format(table_name = tname)
frames_dict [tname] = pd.read_sql ( query , conn)
但我收到如下错误:
DatabaseError: Execution failed on sql ' SELECT * FROM ('customer',)': syntax
error at or near "'yesbank'"
`enter code here`LINE 1: SELECT * FROM ('customer',)
客户是我从行获得的数据库中的表名
tables_df = cur.fetchall()
解决方案
根据您的错误,看起来您的 word 格式有错字:
AttributeError: 'str' object has no attribute 'formate'
尝试
query = select_template.format(table_name = tname)
推荐阅读
- reactjs - 带有样式的材料 UI 与反应测试库抛出警告
- javascript - useReducer 中第三个参数的目的是什么?
- sql - 根据列出所需值的表列确定每个 ID 的缺失值
- spring-boot - Spring Boot - Json 到 Java 嵌套对象
- java - java电话簿数组如何添加新信息和显示旧信息
- python - sqlalchemy:父表和子表之间的“同步”jsonb列
- html - Mat-card 图像放置在 mat-card-content 旁边
- c++ - 英特尔组装与内部,AVX
- vue.js - how to check is location service enabled in vue
- c - 在函数结构上静默 WCast-function-type