====== Snippets: Python: Datenbanken ======
[[:snippets:start|← Zurück zu :snippets:start]]\\
[[:snippets:python:start|← Zurück zu :snippets:python:start]]
===== pickledb =====
* [[https://harrisonerd.com/pickledb/|pickleDB · Lightning-Fast Key-Value Store for Python]]
==== Beispiel ====
> Das Beispiel von der pickledb-Webseite:
from pickledb import PickleDB
# Bind to a JSON file; no I/O yet
db = PickleDB("data.json")
db.load()
db.set("username", "alice")
db.set("theme", {
"color": "blue",
"font": "sans-serif"
})
print(db.get("username")) # → "alice"
db.save() # atomically write to disk
===== SQLite =====
* [[https://sqlite.org/|SQLite]]
==== Beispiel ====
import os
import sqlite3
DB = "~/python.sqlite3"
DB = os.path.expanduser(
os.path.expandvars(DB)
)
if not os.path.exists(DB):
pass
con = sqlite3.connect(DB)
cur = con.cursor()
cur.execute("CREATE TABLE IF NOT EXISTS ...")