update database.py and main.py
This commit is contained in:
@@ -0,0 +1,77 @@
|
||||
import sqlite3
|
||||
from pathlib import Path
|
||||
|
||||
DB_PATH = Path(__file__).parent.parent / "database" / "calendar.db"
|
||||
|
||||
|
||||
def get_connection():
|
||||
"""
|
||||
Maakt een verbinding met de SQLite database.
|
||||
"""
|
||||
conn = sqlite3.connect(DB_PATH)
|
||||
|
||||
# Zorgt dat foreign keys werken in SQLite
|
||||
conn.execute("PRAGMA foreign_keys = ON")
|
||||
|
||||
# Resultaten kunnen als dictionary worden gebruikt
|
||||
conn.row_factory = sqlite3.Row
|
||||
|
||||
return conn
|
||||
|
||||
|
||||
def execute(query, params=()):
|
||||
"""
|
||||
Voor INSERT, UPDATE, DELETE.
|
||||
"""
|
||||
with get_connection() as conn:
|
||||
cursor = conn.cursor()
|
||||
cursor.execute(query, params)
|
||||
conn.commit()
|
||||
|
||||
return cursor.lastrowid
|
||||
|
||||
|
||||
def fetch_one(query, params=()):
|
||||
"""
|
||||
Haalt één resultaat op.
|
||||
"""
|
||||
with get_connection() as conn:
|
||||
cursor = conn.cursor()
|
||||
cursor.execute(query, params)
|
||||
|
||||
return cursor.fetchone()
|
||||
|
||||
|
||||
def fetch_all(query, params=()):
|
||||
"""
|
||||
Haalt meerdere resultaten op.
|
||||
"""
|
||||
with get_connection() as conn:
|
||||
cursor = conn.cursor()
|
||||
cursor.execute(query, params)
|
||||
|
||||
return cursor.fetchall()
|
||||
|
||||
|
||||
def test_connection():
|
||||
"""
|
||||
Test of de database bereikbaar is.
|
||||
"""
|
||||
with get_connection() as conn:
|
||||
cursor = conn.cursor()
|
||||
cursor.execute("SELECT name FROM sqlite_master WHERE type='table';")
|
||||
|
||||
return cursor.fetchall()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
print("Database:", DB_PATH)
|
||||
|
||||
tables = test_connection()
|
||||
|
||||
if tables:
|
||||
print("Gevonden tabellen:")
|
||||
for table in tables:
|
||||
print("-", table["name"])
|
||||
else:
|
||||
print("Geen tabellen gevonden.")
|
||||
+28
-2
@@ -2,5 +2,31 @@
|
||||
# We had the problem of never agreeing when or where
|
||||
# we should meet up, and I hope this fixes the problem.
|
||||
|
||||
import fastapi
|
||||
import time
|
||||
from fastapi import FastAPI
|
||||
from database import fetch_all
|
||||
|
||||
app = FastAPI(
|
||||
title="Calendar API",
|
||||
description="Room based agenda system",
|
||||
version="0.1"
|
||||
)
|
||||
|
||||
|
||||
@app.get("/")
|
||||
def home():
|
||||
return {
|
||||
"status": "online",
|
||||
"service": "Calendar API"
|
||||
}
|
||||
|
||||
|
||||
@app.get("/database")
|
||||
def database_status():
|
||||
tables = fetch_all(
|
||||
"SELECT name FROM sqlite_master WHERE type='table';"
|
||||
)
|
||||
|
||||
return {
|
||||
"tables": [table["name"] for table in tables]
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user