无法为查询 psycopg2 传入 Table 名称
Unable to Pass in Table Name for Query psycopg2
我正在尝试为我的查询传递 table 名称,但是当我使用下面的代码时它不起作用。
def retrieve_data_single(table_name, date):
conn = psycopg2.connect(host="localhost", port = 5432, database="db", user="user")
cur = conn.cursor()
date = date + "%"
cur.execute("""SELECT open, date FROM table_name WHERE date LIKE (%s)""" , [date])
retrieve_data_single(table_1, '2022-12-12'):
根据:
Passing table name as a parameter in psycopg2
你应该使用这个
模板:
from psycopg2 import sql
cur.execute(
sql.SQL("insert into {table} values (%s, %s)")
.format(table=sql.Identifier('my_table')), # table name here
[10, 20]) ## other parameters here
我正在尝试为我的查询传递 table 名称,但是当我使用下面的代码时它不起作用。
def retrieve_data_single(table_name, date):
conn = psycopg2.connect(host="localhost", port = 5432, database="db", user="user")
cur = conn.cursor()
date = date + "%"
cur.execute("""SELECT open, date FROM table_name WHERE date LIKE (%s)""" , [date])
retrieve_data_single(table_1, '2022-12-12'):
根据:
Passing table name as a parameter in psycopg2
你应该使用这个
模板:
from psycopg2 import sql
cur.execute(
sql.SQL("insert into {table} values (%s, %s)")
.format(table=sql.Identifier('my_table')), # table name here
[10, 20]) ## other parameters here