I need insert values in may DB... if i try this way it works:
INSERT INTO mqtt_recvd_log(recvd_cmd_id,mqtt_topic,mqtt_payload)VALUES(1,'d/status/3C61050259A4','A4 21 07 40 F5 20 85 F4 08 00 00 00 08 00 08 00 08 00 05 09 16 12 1C ');
But I want to insert by variable. So I tried several ways and none was successful:
topic = 'd/status/3C61050259A4'
payload= 'A4 21 07 40 F5 20 85 F4 08 00 00 00 08 00 08 00 08 00 05 09 16 12 1C'
conn = psycopg2.connect("dbname='test' user='postgres' password='admin' host='localhost'")
cur = conn.cursor()
cur.execute("""
INSERT INTO mqtt_recvd_log (recvd_cmd_id,mqtt_topic,mqtt_payload)
VALUES (1, %s, %s)
""", (topic, payload))
records = cur.fetchall()
print(records)
and:
sql_command = "INSERT INTO mqtt_recvd_log(recvd_cmd_id,mqtt_topic,mqtt_payload)VALUES(1,'%s','%s');"
conn = psycopg2.connect("dbname='test' user='postgres' password='admin' host='localhost'")
cur = conn.cursor()
cur.execute(sql_command, topic, payload)
records = cur.fetchall()
print(records)
and
cur.execute(f"INSERT INTO mqtt_recvd_log(recvd_cmd_id,mqtt_topic,mqtt_payload)VALUES(1,'{topic}','{payload}');")
in all cases returns the error:
records = cur.fetchall()
psycopg2.ProgrammingError: no results to fetch
I've looked here and elsewhere on how to fix this error but haven't been successful. can anybody help me?