From: Stefan Huber Date: Wed, 15 Jan 2014 13:31:05 +0000 (+0100) Subject: Fix: (dvrid, num) are not unique for purposes X-Git-Url: https://git.sthu.org/?a=commitdiff_plain;h=35d1d45ef925dfebd28d27a6eb5bf962ec854b08;p=dvrdb.git Fix: (dvrid, num) are not unique for purposes --- diff --git a/dvr-db.py b/dvr-db.py index c46aec1..dbf7791 100755 --- a/dvr-db.py +++ b/dvr-db.py @@ -38,20 +38,23 @@ class Database: )") c.execute("CREATE TABLE IF NOT EXISTS registrations (\ - id INTEGER PRIMARY KEY \ + dvrid INTEGER PRIMARY KEY \ REFERENCES dvrtable(id) ON DELETE CASCADE, \ name TEXT NOT NULL, \ address TEXT NOT NULL \ )") c.execute("CREATE TABLE IF NOT EXISTS purposes (\ - id INTEGER REFERENCES dvrtable(id) ON DELETE CASCADE, \ - num INTEGER, \ + id INTEGER PRIMARY KEY AUTOINCREMENT, \ + dvrid INTEGER REFERENCES dvrtable(id) ON DELETE CASCADE, \ + num INTEGER NOT NULL, \ purpose TEXT NOT NULL, \ date TEXT NOT NULL, \ - status TEXT NOT NULL, \ - PRIMARY KEY(id, num) \ + status TEXT NOT NULL \ )") + + c.execute("CREATE INDEX IF NOT EXISTS purposes_dvrid ON purposes (dvrid)") + c.close() conn.commit() @@ -74,31 +77,32 @@ class Database: c.close() self.conn.commit() - def get_registration(self, id): - """Return the registration of given ID, if any.""" + def get_registration(self, dvrid): + """Return the registration of given DVR-ID, if any.""" c = self.conn.cursor() - c.execute("SELECT * FROM registrations WHERE id=?", (id,)) + c.execute("SELECT * FROM registrations WHERE dvrid=?", (dvrid,)) return c.fetchone() - def add_registration(self, id, name, address): - """Add a registration for the given ID.""" + def add_registration(self, dvrid, name, address): + """Add a registration for the given DVR-ID.""" c = self.conn.cursor() c.execute("INSERT INTO registrations VALUES (?, ?, ?)", - (id, name, address)) + (dvrid, name, address)) c.close() self.conn.commit() - def get_purposes(self, id): - """Return all known purposes of given ID, if any.""" + def get_purposes(self, dvrid): + """Return all known purposes of given DVR-ID, if any.""" c = self.conn.cursor() - c.execute("SELECT * FROM purposes WHERE id=?", (id,)) + c.execute("SELECT * FROM purposes WHERE dvrid=?", (dvrid,)) return c.fetchall() - def add_purpose(self, id, num, purpose, date, status): - """Add a purpose for a given ID.""" + def add_purpose(self, dvrid, num, purpose, date, status): + """Add a purpose for a given DVR-ID.""" c = self.conn.cursor() - c.execute("INSERT INTO purposes VALUES (?, ?, ?, ?, ?)", - (id, num, purpose, date, status)) + c.execute("INSERT INTO purposes (dvrid, num, purpose, date, status) \ + VALUES (?, ?, ?, ?, ?)", + (dvrid, num, purpose, date, status)) c.close() self.conn.commit() @@ -175,10 +179,10 @@ def processQuery(db, id, showLocation=False): print(" No purposes known.") else: for purp in purposes: - print(" Purpose %d:" % purp[1]) - print(" Text: ", purp[2]) - print(" Date: ", purp[3]) - print(" Status:", purp[4]) + print(" Purpose %d:" % purp[2]) + print(" Text: ", purp[3]) + print(" Date: ", purp[4]) + print(" Status:", purp[5]) return True