I'm trying to make a small database where each company has its own table, but I need to make sure that when using the INSERT I can put a variable that will be the name of the specific table, but an error keeps occurring that I don't know about, I've tried several ways to write the code but if it's not an error in the table name it's time to put the values.
@app.route('/AdicionarDia',methods=["POST"])
def AddDia():
json_data = request.get_json()
nome_tabela_dia = json_data['nome_tabela_dia']
dia_aser_adicionado = str("1")
email_da_empressa = str("1")
nota_do_dia = str("2")
conn = psycopg2.connect(
host = hostname,
dbname = database,
user = nomeUsuario,
password = senha,
port =portId)
cur = conn.cursor(cursor_factory=psycopg2.extras.DictCursor)
# --------------------------
# script_adicionar_dia = f'''INSERT INTO {nome_tabela_dia} (dia_mes_ano,email_empressa,nota_dia)
# VALUES ({dia_aser_adicionado},{email_da_empressa},{nota_do_dia})'''
# script_adicionar_dia = 'INSERT INTO {} (dia_mes_ano,email_empressa,nota_dia) VALUES ({},{},{})'.format(nome_tabela_dia,dia_aser_adicionado,email_da_empressa,nota_do_dia)
# script_adicionar_dia = "INSERT INTO " + nome_tabela_dia + " (dia_mes_ano,email_empressa,nota_dia) VALUES ("+dia_aser_adicionado+","+email_da_empressa+","+nota_do_dia+")"
# script_adicionar_dia = "INSERT INTO %s (dia_mes_ano,email_empressa,nota_dia) VALUES (%s,%s,%s)"
# script_adicionar_dia_value = (nome_tabela_dia,dia_aser_adicionado,email_da_empressa,nota_do_dia)
# cur.execute("INSERT INTO %s (dia_mes_ano,email_empressa,nota_dia) VALUES (%s,%s,%s);",(nome_tabela_dia,dia_aser_adicionado,email_da_empressa,nota_do_dia))
# --------------------------
script_adicionar_dia = """INSERT INTO %s (dia_mes_ano,email_empressa,nota_dia) VALUES (%s,%s,%s)"""
script_adicionar_dia_value = (json_data['nome_tabela_dia'],dia_aser_adicionado,email_da_empressa,nota_do_dia)
cur.execute(script_adicionar_dia,script_adicionar_dia_value)
conn.commit()
conn.close()
return {}, 200
I hope to find a way to make it possible for me to use variables both in the table name and in the values that will be placed inside it.