mysql - Retrieving data from my sql and sending it to a web service using python -
i trying retrieve data mysql in raspberry pi 2 , posting data web service using python . able retrieve data mysql within raspberry pi using python script. however, having trouble posting data web service using separate python script when try try pass data values it. tell me doing wrong? or there way can this?
python code retrieve data mysql in raspberry pi(fetchdata.py):
import mysqldb import sys import subprocess db = mysqldb.connect("localhost", "root", "1234", "tempdata") cursor = db.cursor() sql = "select * webdata" cursor.execute(sql) results = cursor.fetchall() 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 following:
id=4, channelid=43, timestamp=56, rmsvoltage=78
here python code posting web service(sendjson.py):
import sys import json import pprint import requests import subprocess import fetchdata #name of python script retrieves data 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 output getting:
id=4, channelid=43,timestamp=56, rmsvoltage=78
{"message":the request 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."]}}
could tell me did wrong? not getting new data on web service.
question: need add missing
'
?
no, removing not adding!
dict
should this, removed 4 '
!
query = {'id': fetchdata.id, 'channelid': fetchdata.channelid, 'timestamp': fetchdata.timestamp, 'rmsvoltage': fetchdata.rmsvoltage}
how use dictionaries in python
dictionaries in python use {} curly brackets construct dictionary, , [] square brackets index it.
separate key , value colons : , commas , between each pair.
keys must quoted
Comments
Post a Comment