I am trying to pass in the table name for my queries but it doesn't work when I use the code below.
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'):
CodePudding user response:
According to:
Passing table name as a parameter in psycopg2
You should be using this
template:
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