I'm trying to setup a MQTT client using the Paho library. With that there are no problems, but I'm trying to insert the received publishes in a sql database. I'm converting the received payload string into a dictionary and add a few entries. When executing the following code:
def insert_values(db_connection=None, topic=None, payload=None, time_received=None):
query_1 = "SELECT topic_id FROM topics WHERE topic = %s"
query_2 = """INSERT INTO measurements (start_time, end_time, value, max_value, min_value, time_received, topic_id)
VALUES (%(start_time)s, %(end_time)s, %(value)s, %(max_value)s, %(min_value)s, %(time_received)s,
%(topic_id)s)"""
cursor = db_connection.cursor(prepared=True)
topic_id = cursor.execute(query_1, topic)
payload["time_received"] = time_received
payload["topic_id"] = topic_id
cursor.executemany(query_2, payload)
db_connection.commit()
db_disconnect(db_connection, cursor)
I get the following error:
Caught exception in on_message: 1210: Incorrect number of arguments executing prepared statement
The payload looks like this:
payload = {
"Starttime:": 2020-02-18 10:11:22.2145563,
"Endtime:": 2020-02-18 10:12:22.2145563,
"Average Humidity:": 44.256241,
"Number of Values:": 22,
"Max Humidity:": 44.586214,
"Min Humidity:": 44.012148521)
}
Plus some additional info, like the time the payload was received. In the insert_values method I'm trying to get the topic_id from the table topics and write it into the payload.
Edit: The table in which the measurements are to be written looks like this:
CREATE TABLE IF NOT EXISTS measurements
(measurement_id INT AUTO_INCREMENT,
start_time DATETIME,
end_time DATETIME,
value FLOAT,
max_value FLOAT,
min_value FLOAT,
time_received DATETIME,
topic_id INT,
PRIMARY KEY (measurement_id),
FOREIGN KEY (topic_id) REFERENCES topics(topic_id))