我想从Pytnon创建一个表:
import psycopg2 as pg
from psycopg2 import sql
conn = pg.connect("dbname=test user=test")
table_name = "testDB"
column_name = "mykey"
column_type = "bigint"
cu = conn.cursor()
cu.execute(sql.SQL("CREATE TABLE {t} ({c} {y})").format(
t=sql.Identifier(table_name),
c=sql.Identifier(column_name),
y=sql.Literal(column_type)))
唉,这散发出
CREATE TABLE "testDB" ("mykey" 'bigint')
失败的原因是
当然,我可以做一些
cu.execute(sql.SQL("CREATE TABLE {t} ({c} %s)" % (column_name)).format(
t=sql.Identifier(table_name),
c=sql.Identifier(column_name)))
但我怀疑有一个更优雅(和安全!)解决方案。
How to make psycopg2 emit nested quotes?