dump_db.py 883 B

12345678910111213141516171819202122232425262728
  1. import sqlite3
  2. import sys
  3. db = r"C:\Users\huang\.workbuddy\workbuddy.db"
  4. con = sqlite3.connect("file:%s?mode=ro" % db.replace("\\", "/"), uri=True)
  5. cur = con.cursor()
  6. cur.execute("SELECT name FROM sqlite_master WHERE type='table' ORDER BY name")
  7. tables = [r[0] for r in cur.fetchall()]
  8. print("tables:", len(tables))
  9. kw = sys.argv[1].lower() if len(sys.argv) > 1 else "workspace"
  10. hit = [t for t in tables if kw in t.lower()]
  11. print("\nmatching tables for %r: %s" % (kw, hit))
  12. for t in hit:
  13. print("\n=== %s ===" % t)
  14. cur.execute("PRAGMA table_info(%s)" % t)
  15. cols = [c[1] for c in cur.fetchall()]
  16. print(" columns:", cols)
  17. try:
  18. cur.execute("SELECT * FROM %s LIMIT 12" % t)
  19. rows = cur.fetchall()
  20. for r in rows:
  21. s = str(r)
  22. print(" ", s[:400])
  23. except Exception as e:
  24. print(" read error:", e)
  25. con.close()