【问题标题】:Value substitution using encode/databases fecthall method使用编码/数据库 fecthall 方法进行值替换
【发布时间】:2021-01-02 08:57:02
【问题描述】:

我正在使用已配置为使用连接的 encode/databases(https://www.encode.io/databases/database_queries/) postgresql+aiopg://DBUSER:DBPASS@1.2.3.4:1234/SOMEDB 取如下函数:

async def get_table_sample(table_name: str, db: Database):
    result = await db.fetch_all("SELECT * FROM :table_name limit 100", values={"table_name": table_name})
    return result

运行时,抛出以下错误:

psycopg2.errors.SyntaxError: at or near "rally_iteration": syntax error
DETAIL:  source SQL:
SELECT * FROM 'rally_iteration' limit 100

注意 subbed 变量周围的引号。如果我将查询硬编码为"SELECT * from rally_iteration limit 100" ,一切都会按预期进行。

感谢任何帮助。谢谢!

【问题讨论】:

    标签: python psycopg2 aiopg


    【解决方案1】:

    asyncpg 和许多其他 sql 库不支持 FROM 子句中的变量替换。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-09-22
      • 1970-01-01
      • 2017-08-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多