User Tools

Site Tools


snippets:python:db

Snippets: Python: Datenbanken

pickledb

Beispiel

Das Beispiel von der pickledb-Webseite:
  1. from pickledb import PickleDB
  2.  
  3. # Bind to a JSON file; no I/O yet
  4. db = PickleDB("data.json")
  5. db.load()
  6.  
  7. db.set("username", "alice")
  8. db.set("theme", {
  9. "color": "blue",
  10. "font": "sans-serif"
  11. })
  12.  
  13. print(db.get("username")) # → "alice"
  14.  
  15. db.save() # atomically write to disk

SQLite

Beispiel

  1. import os
  2. import sqlite3
  3.  
  4.  
  5. DB = "~/python.sqlite3"
  6.  
  7.  
  8. DB = os.path.expanduser(
  9. os.path.expandvars(DB)
  10. )
  11. if not os.path.exists(DB):
  12. pass
  13.  
  14. con = sqlite3.connect(DB)
  15. cur = con.cursor()
  16. cur.execute("CREATE TABLE IF NOT EXISTS ...")
snippets/python/db.txt · Last modified: by malte70