python - 如何从 psycopg2 请求表大小?
问题描述
实验上,以下工作:
from psycopg2 import sql
def table_size(conn, table):
with conn.cursor() as cu:
cu.execute(sql.SQL("SELECT pg_table_size({t}), pg_indexes_size({t});").format(t=sql.Literal(table)))
return cu.fetchall()[0]
但是,我想知道我是否正确使用sql.SQL
/ 。sql.Literal
例如,如何将表名和列名与值结合起来?
def get(conn,table,key,value,column):
with conn.cursor() as cu:
cu.execute(sql.SQL("SELECT {c} FROM {t} WHERE {k} = %s;").format(
c=sql.Identifier(column),t=sql.Identifier(table),k=sql.Identifier(key)),
(value, ))
return cu.fetchall()
解决方案
推荐阅读
- python - Flask 在 render_template 后追加结果的问题
- python - 从 python 运行 shell 命令,以及如何将结果存储在文件中(文本或 Excel 或 csv 等)
- reactjs - 当我使用语义-ui-react Popup 触发器时,前消息和当前消息都显示
- python - 使用 pipenv 安装 pymssql 失败
- sql - 我的存储过程只执行我的第一个选择语句,而我写了三个?
- android - 强制关闭应用程序后,工作经理定期工作无法在中文 ROM 上工作
- sql - 自嵌套 SQL 连接
- spring - 在 Spring 中以测试模式运行时如何禁用方法?
- java - 如何将 Hibernate QueryList 转换为自定义类
- php - Composer 在调用 autoload.php 后执行脚本