从我的 sql 检索数据并使用 python 将其发送到 Web 服务
Retrieving data from my sql and sending it to a web service using python
我正在尝试从 Raspberry Pi 2 中的 MySQL 检索数据并使用 python 将该数据发布到 Web 服务。我可以使用 python 脚本从 raspberry pi 中的 MySQL 检索数据。但是,当我尝试将数据值传递给它时,我无法使用单独的 python 脚本将数据发布到 Web 服务。谁能告诉我我做错了什么?还是有其他方法可以做到这一点?
Python 从 MySQL 中检索数据的代码 Raspberry Pi(fetchdata.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
这是我的 python 用于发布到网络服务的代码 (sendjson.py):
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)
这是我得到的输出:
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."]}}
谁能告诉我我做错了什么?我没有在我的网络服务上获得任何新数据。
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
我正在尝试从 Raspberry Pi 2 中的 MySQL 检索数据并使用 python 将该数据发布到 Web 服务。我可以使用 python 脚本从 raspberry pi 中的 MySQL 检索数据。但是,当我尝试将数据值传递给它时,我无法使用单独的 python 脚本将数据发布到 Web 服务。谁能告诉我我做错了什么?还是有其他方法可以做到这一点?
Python 从 MySQL 中检索数据的代码 Raspberry Pi(fetchdata.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
这是我的 python 用于发布到网络服务的代码 (sendjson.py):
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)
这是我得到的输出:
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."]}}
谁能告诉我我做错了什么?我没有在我的网络服务上获得任何新数据。
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