def findLargestId(cursor): """ Finds the largest id so we can add stations after that """ cursor.execute("SELECT MAX(id) FROM iradiobookmarks") row = cursor.fetchone() if row: return row[0] else: return -1 def buildBlob(self, string): """ Builds the needed blob for value column """ return sqlite3.Binary(chr(1) + chr(0) * 3 + '@' + chr(0) * 3 + string + chr(0) ) # Check if station in already listed and return it's id. # Used to do updates def findStationId(self, cursor, station): """ finds the id of an existing station """ cursor.execute('SELECT id FROM iradiobookmarks WHERE key="title" and value=?', (self.buildBlob(station),)) row = cursor.fetchone() if row: return row[0] else: return -1 def insertIntoDB(cursor, title, uri): """ inserts station in db""" nextID = self.findLargestId(cursor)+1 stationId = self.findStationId(cursor, title) if stationId == -1: # station is not listed. Add it to the list with the next ID stationId = nextID nextID += 1 cursor.execute('INSERT INTO iradiobookmarks VALUES (?, "title", ?)', (stationId, self.buildBlob(title),)) cursor.execute('INSERT INTO iradiobookmarks VALUES (?, "mime-type", ?)', (stationId, self.buildBlob("audio"),)) cursor.execute('INSERT INTO iradiobookmarks VALUES (?, "uri", ?)', (stationId, self.buildBlob(uri),)) else: # station is listed. Update the uri entry cursor.execute('UPDATE iradiobookmarks SET value = ? WHERE id = ? AND key = "uri" ', (self.buildBlob(uri), stationId)) conn = sqlite3.connect("/home/user/.mafw.db") cursor = conn.cursor()