Files
J1B3-Sensor-boxes/server/web-data-connection/generalDatabaseFile.md
2024-04-02 21:13:27 +02:00

38 lines
1.2 KiB
Markdown

## General node file.
The general node file includes the database connecting and a node connection class.
This is done so the files don't go back and forth with the db connection, this otherwise would have caused problems.
```py
#Importing a database library to connect to the database.
import mysql.connector
def dbLogin():
mydb = mysql.connector.connect(
host="localhost",
user="root",
password="**********",
database="NodeData"
)
return mydb
```
The "__init__"function is called.
(In javascript this is known as the "constructor".
For further information visit https://www.w3schools.com/python/gloss_python_class_init.asp)
```py
# A general class which acts as a database connector and a node identifyer
class Node():
def __init__(self, macAdress):
self.macAdress = macAdress
self.id = None
def getNodeId(self):
id = (self.macAdress,)
mydb = dbLogin()
cursor = mydb.cursor()
cursor.execute("""SELECT nodeID FROM Node WHERE MAC = %s""", id)
data = cursor.fetchall()
#Again go along all tuple items. and asign them to the ID
for tuples in data:
for item in tuples:
self.id = item
```