我正在嘗試為我的查詢傳遞表名,但是當我使用下面的代碼時它不起作用。
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'):
uj5u.com熱心網友回復:
根據:
在 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
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/476022.html
標籤:Python sql PostgreSQL 心理咨询师2
下一篇:Rust柴油有條件地更新欄位
