Python - psycopg2.sql – 调用函数
Python - psycopg2.sql – calling function
我尝试在动态 SQL 中调用子字符串函数。
from psycopg2 import sql as ps_sql
sql = ps_sql.SQL('select {}').format(ps_sql.Identifier("substring('TEST',1,2)"))
with db.conn_ogis.cursor() as cursor:
cursor.execute(sql)
我有错误:psycopg2.errors.UndefinedColumn:错误:列 "substring('TEST',1,2)" 不存在
如何正确调用函数?
仅将 Identifier()
函数用于 Postgres 标识符(即 table、列、视图、函数等的名称)。所以你可以这样使用它:
sql = ps_sql.SQL("select {}('TEST',1,2)").format(ps_sql.Identifier("substring"))
实际上,在这种特殊情况下,标识符 substring
不需要引号(因为它是小写的):
sql = ps_sql.SQL("select substring('TEST',1,2)")
我尝试在动态 SQL 中调用子字符串函数。
from psycopg2 import sql as ps_sql
sql = ps_sql.SQL('select {}').format(ps_sql.Identifier("substring('TEST',1,2)"))
with db.conn_ogis.cursor() as cursor:
cursor.execute(sql)
我有错误:psycopg2.errors.UndefinedColumn:错误:列 "substring('TEST',1,2)" 不存在
如何正确调用函数?
仅将 Identifier()
函数用于 Postgres 标识符(即 table、列、视图、函数等的名称)。所以你可以这样使用它:
sql = ps_sql.SQL("select {}('TEST',1,2)").format(ps_sql.Identifier("substring"))
实际上,在这种特殊情况下,标识符 substring
不需要引号(因为它是小写的):
sql = ps_sql.SQL("select substring('TEST',1,2)")