sqlEnPy
This commit is contained in:
16
goodGarden.sql
Normal file
16
goodGarden.sql
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
DROP DATABASE IF EXISTS goodgarden;
|
||||||
|
CREATE DATABASE goodgarden;
|
||||||
|
|
||||||
|
CREATE TABLE goodgarden.sensor_data (
|
||||||
|
id INT UNSIGNED NOT NULL AUTO_INCREMENT,
|
||||||
|
timestamp INT,
|
||||||
|
gateway_receive_time VARCHAR(50),
|
||||||
|
device INT,
|
||||||
|
value DECIMAL(10, 5),
|
||||||
|
PRIMARY KEY (id)
|
||||||
|
);
|
||||||
|
|
||||||
|
-- Invoegen van gegevens in de 'sensor_data'-tabel
|
||||||
|
INSERT INTO goodgarden.sensor_data (timestamp, gateway_receive_time, device, value)
|
||||||
|
VALUES (1707295162, '2024-02-07T08:39:22Z', 256, 4.107448101043701),
|
||||||
|
(1707261284, '2024-02-06T23:14:44Z', 322, 4.111111164093018);
|
||||||
37
index.py
Normal file
37
index.py
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
import mysql.connector
|
||||||
|
|
||||||
|
# Verbinding maken met de database
|
||||||
|
mydb = mysql.connector.connect(
|
||||||
|
host="localhost",
|
||||||
|
user="root",
|
||||||
|
password="",
|
||||||
|
database="goodgarden"
|
||||||
|
)
|
||||||
|
|
||||||
|
# Controleren of de verbinding succesvol is
|
||||||
|
if mydb.is_connected():
|
||||||
|
print("Connected to the database")
|
||||||
|
else:
|
||||||
|
print("Failed to connect to the database")
|
||||||
|
|
||||||
|
try:
|
||||||
|
# Maak een cursor aan
|
||||||
|
mycursor = mydb.cursor()
|
||||||
|
|
||||||
|
# Voer de query uit om gegevens op te halen
|
||||||
|
mycursor.execute("SELECT * FROM goodgarden.sensor_data")
|
||||||
|
|
||||||
|
# Haal de resultaten op
|
||||||
|
myresult = mycursor.fetchall()
|
||||||
|
|
||||||
|
# Print de resultaten
|
||||||
|
for x in myresult:
|
||||||
|
print(x)
|
||||||
|
|
||||||
|
except mysql.connector.Error as err:
|
||||||
|
print(f"Error: {err}")
|
||||||
|
|
||||||
|
finally:
|
||||||
|
# Sluit de cursor en de databaseverbinding
|
||||||
|
mycursor.close()
|
||||||
|
mydb.close()
|
||||||
Reference in New Issue
Block a user