Skip to content

Instantly share code, notes, and snippets.

@ldx
Created November 28, 2014 16:03
Show Gist options
  • Save ldx/7d8426697ef1606fcfb3 to your computer and use it in GitHub Desktop.
Save ldx/7d8426697ef1606fcfb3 to your computer and use it in GitHub Desktop.
Create wallpaper sqlite DB on Mac OS X
import sqlite3
conn = sqlite3.connect('sqlite.db')
cur = conn.cursor()
cur.execute("PRAGMA foreign_keys=OFF;")
cur.execute("BEGIN TRANSACTION;")
cur.execute("CREATE TABLE pictures (space_id INTEGER, display_id INTEGER);")
cur.execute("INSERT INTO pictures VALUES(NULL,NULL);")
cur.execute("INSERT INTO pictures VALUES(NULL,1);")
cur.execute("INSERT INTO pictures VALUES(1,1);")
cur.execute("INSERT INTO pictures VALUES(1,NULL);")
cur.execute("CREATE TABLE preferences (key INTEGER, data_id INTEGER, "
"picture_id INTEGER);")
cur.execute("CREATE TABLE spaces (space_uuid VARCHAR);")
cur.execute("INSERT INTO spaces VALUES('');")
cur.execute("CREATE TABLE displays (display_uuid);")
cur.execute("INSERT INTO displays "
"VALUES('FFFFFFFF-FFFF-FFFF-FFFF-FFFFFFFFFFFF');")
cur.execute("CREATE TABLE data (value);")
cur.execute("CREATE TABLE prefs (key INTEGER, data);")
cur.execute("CREATE INDEX pictures_index ON pictures (space_id, display_id);")
cur.execute("CREATE INDEX preferences_index ON "
"preferences (picture_id, data_id);")
cur.execute("CREATE INDEX spaces_index ON spaces (space_uuid);")
cur.execute("CREATE INDEX displays_index ON displays (display_uuid);")
cur.execute("CREATE INDEX data_index ON data (value);")
cur.execute("CREATE INDEX prefs_index ON prefs (key);")
cur.execute("CREATE TRIGGER space_deleted AFTER DELETE ON spaces "
"BEGIN DELETE FROM pictures WHERE space_id=OLD.ROWID;END;")
cur.execute("CREATE TRIGGER display_deleted AFTER DELETE ON displays "
"BEGIN DELETE FROM pictures WHERE display_id=OLD.ROWID;END;")
cur.execute("CREATE TRIGGER picture_deleted AFTER DELETE ON pictures "
"BEGIN DELETE FROM preferences WHERE picture_id=OLD.ROWID; "
"DELETE FROM displays WHERE ROWID=OLD.display_id AND NOT EXISTS "
"(SELECT NULL FROM pictures WHERE display_id=OLD.display_id);"
"DELETE FROM spaces WHERE ROWID=OLD.space_id AND NOT EXISTS "
"(SELECT NULL FROM pictures WHERE space_id=OLD.space_id);END;")
cur.execute("CREATE TRIGGER preferences_deleted AFTER DELETE ON "
"preferences BEGIN DELETE FROM data WHERE ROWID=OLD.data_id AND "
"NOT EXISTS (SELECT NULL FROM preferences WHERE "
"data_id=OLD.data_id);END;")
conn.commit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment