# SQLite Datenbank-Befehle

## Verbindung zur Datenbank

Die Datenbank liegt in: `./backend/spaces.db`

### Verbindung herstellen:
```bash
cd /home/nick/Development/certigo-addon/backend
sqlite3 spaces.db
```

## Nützliche SQLite-Befehle

### Tabellen auflisten:
```sql
.tables
```

### Schema einer Tabelle anzeigen:
```sql
.schema audit_logs
```

### Alle Audit-Logs anzeigen:
```sql
SELECT * FROM audit_logs ORDER BY timestamp DESC LIMIT 10;
```

### Anzahl der Audit-Logs:
```sql
SELECT COUNT(*) FROM audit_logs;
```

### Letzte 5 Audit-Logs mit Details:
```sql
SELECT 
    timestamp,
    username,
    action,
    resource_type,
    details
FROM audit_logs 
ORDER BY timestamp DESC 
LIMIT 5;
```

### Einen spezifischen Audit-Log anzeigen (nach ID):
```sql
SELECT * FROM audit_logs WHERE id = 'LOG_ID_HIER';
```

### Einen spezifischen Audit-Log anzeigen (nach Timestamp):
```sql
SELECT * FROM audit_logs WHERE timestamp = '2025-11-20 16:20:00';
```

### Audit-Logs nach Aktion filtern:
```sql
SELECT * FROM audit_logs WHERE action = 'CREATE' ORDER BY timestamp DESC;
```

### Audit-Logs nach Ressourcentyp filtern:
```sql
SELECT * FROM audit_logs WHERE resource_type = 'user' ORDER BY timestamp DESC;
```

### Audit-Logs nach Benutzer filtern:
```sql
SELECT * FROM audit_logs WHERE username = 'admin' ORDER BY timestamp DESC;
```

### Vollständige Details eines Logs (formatiert):
```sql
SELECT 
    id,
    timestamp,
    user_id,
    username,
    action,
    resource_type,
    resource_id,
    details,
    ip_address,
    user_agent
FROM audit_logs 
WHERE id = 'LOG_ID_HIER';
```

### Neueste Logs mit formatierten Details:
```sql
SELECT 
    datetime(timestamp) as zeit,
    username,
    action,
    resource_type,
    json_extract(details, '$.message') as nachricht,
    ip_address
FROM audit_logs 
ORDER BY datetime(timestamp) DESC 
LIMIT 10;
```

### Logs der letzten Stunde:
```sql
SELECT * FROM audit_logs 
WHERE datetime(timestamp) >= datetime('now', '-1 hour')
ORDER BY timestamp DESC;
```

### Logs von heute:
```sql
SELECT * FROM audit_logs 
WHERE date(timestamp) = date('now')
ORDER BY timestamp DESC;
```

### Alle Tabellen auflisten:
```sql
SELECT name FROM sqlite_master WHERE type='table';
```

### Prüfen ob audit_logs Tabelle existiert:
```sql
SELECT name FROM sqlite_master WHERE type='table' AND name='audit_logs';
```

### Alle Spalten der audit_logs Tabelle:
```sql
PRAGMA table_info(audit_logs);
```

### SQLite verlassen:
```sql
.quit
```
oder
```sql
.exit
```

## Direkte Befehle (ohne interaktive Shell)

### Anzahl der Logs:
```bash
sqlite3 backend/spaces.db "SELECT COUNT(*) FROM audit_logs;"
```

### Letzte 5 Logs:
```bash
sqlite3 backend/spaces.db "SELECT timestamp, username, action, resource_type, details FROM audit_logs ORDER BY timestamp DESC LIMIT 5;"
```

### Prüfen ob Tabelle existiert:
```bash
sqlite3 backend/spaces.db "SELECT name FROM sqlite_master WHERE type='table' AND name='audit_logs';"
```

### Alle Logs als CSV exportieren:
```bash
sqlite3 -header -csv backend/spaces.db "SELECT * FROM audit_logs ORDER BY timestamp DESC;" > audit_logs.csv
```

### Einen spezifischen Log anzeigen (Beispiel):
```bash
sqlite3 backend/spaces.db "SELECT * FROM audit_logs WHERE id = '5d424293-14c1-48ef-a34c-5555d843d289';"
```

### Neueste 10 Logs mit Details:
```bash
sqlite3 -header -column backend/spaces.db "SELECT timestamp, username, action, resource_type, details FROM audit_logs ORDER BY timestamp DESC LIMIT 10;"
```

### Logs nach Aktion filtern:
```bash
sqlite3 -header -column backend/spaces.db "SELECT * FROM audit_logs WHERE action = 'CREATE' ORDER BY timestamp DESC LIMIT 10;"
```

### Logs nach Benutzer filtern:
```bash
sqlite3 -header -column backend/spaces.db "SELECT * FROM audit_logs WHERE username = 'admin' ORDER BY timestamp DESC LIMIT 10;"
```

### Logs von heute:
```bash
sqlite3 -header -column backend/spaces.db "SELECT * FROM audit_logs WHERE date(timestamp) = date('now') ORDER BY timestamp DESC;"
```

### JSON-Details eines Logs formatiert anzeigen:
```bash
sqlite3 backend/spaces.db "SELECT json_pretty(details) FROM audit_logs WHERE id = 'LOG_ID_HIER';"
```

**Hinweis**: Falls `json_pretty` nicht verfügbar ist, verwende stattdessen:
```bash
sqlite3 backend/spaces.db "SELECT details FROM audit_logs WHERE id = 'LOG_ID_HIER';" | python3 -m json.tool
```

