49 lines
1.8 KiB
Markdown
49 lines
1.8 KiB
Markdown
## Questionaire class
|
|
This file prmairly is dedicated to the class which gets its data form the questionare and pushes it to the database.
|
|
This is done this way to make the code more visable and more clear.
|
|
```py
|
|
#Importing different librarys.
|
|
import mysql.connector
|
|
import json
|
|
#importing different classes.
|
|
from classes_data import Node
|
|
from classes_data import dbLogin
|
|
```
|
|
Here a class is made to again provide more clear viewing and more reusability.
|
|
The
|
|
```py
|
|
#Node is between brackets to show that this class is a child class from the parent class "Node"
|
|
class EnqueteNode(Node):
|
|
#A private query to use later in a function.
|
|
__query = "INSERT INTO `Reply` (Result, Node_NodeID, Question_QuestionID) VALUES (%s, %s, %s)"
|
|
#use a super to get info from the parent class.
|
|
def __init__(self, macAdress, response, questionID):
|
|
super().__init__(macAdress)
|
|
self.response = response
|
|
self.questionID = questionID
|
|
#making a database connection to then load in the processed data.
|
|
async def processEnqueteNodeData(data, nodeID):
|
|
try:
|
|
mydb = dbLogin()
|
|
cursor = mydb.cursor()
|
|
|
|
processedData = json.loads(data)
|
|
|
|
EnqueteNode.questionID = (processedData['QuestionID'])
|
|
EnqueteNode.response = (processedData['Response'])
|
|
|
|
#an array with the data to push.
|
|
pushingDataArray = [(EnqueteNode.questionID, nodeID, EnqueteNode.response)]
|
|
|
|
#push the data according to the query to the database.
|
|
for i in pushingDataArray:
|
|
print(EnqueteNode.__query, i)
|
|
cursor.execute(EnqueteNode.__query, i)
|
|
mydb.commit()
|
|
#print an error.
|
|
except mysql.connector.Error as err:
|
|
print("MySQL Error:", err)
|
|
finally:
|
|
cursor.close()
|
|
mydb.close()
|
|
``` |