简体   繁体   English

从我的SQL检索数据并将其发送到使用python的Web服务

[英]Retrieving data from my sql and sending it to a web service using python

I am trying to retrieve data from MySQL in Raspberry Pi 2 and posting that data to a web service using python . 我试图在Raspberry Pi 2中从MySQL检索数据,然后使用python将数据发布到Web服务。 I am able to retrieve data from MySQL within the raspberry pi using a python script. 我可以使用python脚本从树莓派中的MySQL检索数据。 However, I am having trouble posting the data to a web service using a separate python script when I try to I try to pass the data values to it. 但是,当我尝试将数据值传递给它时,我无法使用单独的python脚本将数据发布到Web服务。 Would anyone tell me what I am doing wrong? 谁能告诉我我在做什么错? Or is there another way that I can do this? 还是有其他方法可以做到这一点?

Python code to retrieve data from MySQL in Raspberry Pi(fetchdata.py): 在Raspberry Pi(fetchdata.py)中从MySQL检索数据的Python代码:

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()

The output that I get is the following: 我得到的输出如下:

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

Here is my python code for posting to the web service(sendjson.py): 这是我发布到Web服务(sendjson.py)的python代码:

import sys
import json
import pprint
import requests
import subprocess
import fetchdata #name of python script that retrieves data from MySQL in RPi

url = 'http://192.168.1.101/TestWebsite/api/SecondlyReadingDatas'
query = {'ID': 'fetchdata.ID, 'ChannelID': fetchdata.ChannelID, 'TimeStamp': fetchdata.TimeStamp', 'RMSVoltage': 'fetchdata.RMSVoltage'}

headers = {'Content-Type': 'application/json'}
r = requests.post(url, headers=headers, data=json.dumps(query))
print(r.text)

This is the output I am getting: 这是我得到的输出:

ID=4, ChannelID=43,TimeStamp=56, RMSVoltage=78 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."]}} {“消息”:请求无效。“,” ModelState“:{” secondlyReading.ID“ [”发生错误。“]”,secondReading.ChannelID“ [”发生错误。“],” secondaryReading。 TimeStamp“ [”发生错误。“],” secondlyReading.RMSVoltage“ [”发生错误。“]}}

Could anyone tell me what I did wrong? 谁能告诉我我做错了什么? I am not getting any new data on my web service. 我的网络服务上没有任何新数据。

Question : So I need to add the missing ' ? 问题 :所以我需要添加缺少'

No , removing not adding! ,删除不添加!
Your dict should look like this, I removed 4 ' ! 您的dict应该像这样,我删除了4 '

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

How to use dictionaries in Python 如何在Python中使用字典
About dictionaries in Python Use {} curly brackets to construct the dictionary, and [] square brackets to index it. 关于Python中的字典使用{}大括号来构造字典,使用[]方括号来对其进行索引。
Separate the key and value with colons : and with commas , between each pair. 每对之间用冒号:和逗号分隔键和值。
Keys must be quoted 键必须加引号

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM