[英]How do I insert values into a sql table using python3?
I'm trying to setup a MQTT client using the Paho library.我正在尝试使用 Paho 库设置 MQTT 客户端。 With that there are no problems, but I'm trying to insert the received publishes in a sql database.
这样就没有问题了,但我正在尝试将收到的发布插入 sql 数据库中。 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.
在 insert_values 方法中,我试图从表主题中获取 topic_id 并将其写入有效负载。
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))
Your payload has 6 keys, and then you add another 2 that makes it 8 In your query you have only 7 arguments I'm not sure but i think you forgot to add "Average Humidity" EDIT : After seeing your table DESC您的有效负载有 6 个键,然后您添加另外 2 个使其成为 8 在您的查询中,您只有 7 个参数,我不确定,但我认为您忘记添加“平均湿度”编辑:看到您的表 DESC 后
query_2 = """INSERT INTO measurements (start_time, end_time, value, max_value, min_value, time_received, topic_id)
VALUES (%s,%s,%s,%s,%s,%s,%s)"""
cursor = db_connection.cursor(prepared=True)
topic_id = cursor.execute(query_1, topic)
payload["time_received"] = time_received
payload["topic_id"] = topic_id
payload.pop("Average Humidity:", None)
cursor.executemany(query_2, payload)
db_connection.commit()
db_disconnect(db_connection, cursor)
Here:这里:
"""VALUES (%(start_time)s, %(end_time)s, %(value)s, %(max_value)s, %(min_value)s, %(time_received)s, %(topic_id)s)"""
and here:和这里:
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)
}
Your payload
keys must match the query's placeholders names - or your placeholders match the payload
keys.您的
payload
键必须与查询的占位符名称匹配 - 或者您的占位符与payload
键匹配。 Your db client will definitly not try and guess that "start_time" and "Starttime" are actually supposed to be the same thing.您的数据库客户端绝对不会尝试猜测“start_time”和“Starttime”实际上应该是同一件事。 And you of course must have mathcing keys for all the query's placeholders.
而且您当然必须为所有查询的占位符设置数学键。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.