从sql中检索数据并使用python将其发送到web服务

2024-05-26 17:43:16 发布

您现在位置:Python中文网/ 问答频道 /正文

我尝试在raspberrypi2中从MySQL检索数据,并使用python将这些数据发布到web服务。我能够使用python脚本在raspberry pi中从MySQL中检索数据。但是,当我试图将数据值传递给web服务时,我很难使用单独的python脚本将数据发布到web服务。有人能告诉我我做错了什么吗?或者我有别的办法吗?在

在Raspberry Pi中从MySQL检索数据的Python代码(获取数据.py):

import MySQLdb
import sys
import subprocess

db = MySQLdb.connect("localhost", "root", "1234", "tempdata")
cursor = db.cursor()
sql = "SELECT * FROM webdata"

cursor.execute(sql)
results = cursor.fetchall()
for row in results:
    ID = row[0]
    ChannelID = row[1]
    TimeStamp = row[2]
    RMSVoltage = row[3]

print "ID=%s, ChannelID=%s, TimeStamp=%s, RMSVoltage=%s" % (ID, ChannelID, TimeStamp, RMSVoltage)

db.close()

我得到的输出如下:

ID=4, ChannelID=43, TimeStamp=56, RMSVoltage=78

这是我发布到web服务的python代码(sendjson.py):

^{pr2}$

这是我得到的输出:

ID=4, ChannelID=43,TimeStamp=56, RMSVoltage=78

{"Message":The request is invalid.","ModelState":{"secondlyReading.ID"["An error has occured."]", secondlyReading.ChannelID"["An error has occured."], "secondlyReading.TimeStamp"["An error has occured."], "secondlyReading.RMSVoltage"["An error has occured."]}}

谁能告诉我我做错了什么吗?我没有在我的web服务上获取任何新数据。在


Tags: 数据importanwebidmysqlerrorcursor
1条回答
网友
1楼 · 发布于 2024-05-26 17:43:16

Question: So I need to add the missing '?

,删除不添加!
你的dict应该是这样的,我删除了4'!在

query = {'ID': fetchdata.ID, 'ChannelID': fetchdata.ChannelID, 'TimeStamp': fetchdata.TimeStamp, 'RMSVoltage': fetchdata.RMSVoltage}

How to use dictionaries in Python
About dictionaries in Python Use {} curly brackets to construct the dictionary, and [] square brackets to index it.
Separate the key and value with colons : and with commas , between each pair.
Keys must be quoted

相关问题 更多 >

    热门问题