def runQuery(database, query): global pt, rt, sc db = determineDB(database) if (db is None): return curs = db.cursor() result = curs.set(query.encode("utf-8")) listofIDs = list() if result != None: print("\nList of all reviews found using input '" + query + "'") while result != None: if (str(result[0].decode("utf-8")) != query): break # Only keep track of new IDs! (Not sure if we should do this) ReviewID = str(result[1].decode("utf-8")) ReviewID = ReviewID.replace('\r', '') if ReviewID not in listofIDs: listofIDs.append(ReviewID) result = curs.next() curs.close() return listofIDs # Returns a list of the ID's that matched (Exact)
def getDateGreater(date, eq, db): cur = db.cursor() res = None res = cur.set_range(date.encode()) if res == None: return [] # If we include the original key, we can set the output to be the dups from price. If not we set output to empty if eq: output = getAllDupsFromDate(date, db) else: output = [] res = cur.next() date = datetime.datetime.strptime(date, "%Y/%m/%d") if eq: while res != None: # If the price is >= to the current (should be), make the output extend and add item sfrom another list. if (datetime.datetime.strptime(res[0].decode(), "%Y/%m/%d") >= date): output.extend(getAllDupsFromDate(res[0].decode(), db)) res = cur.next() else: while res != None: # If the price is >= to the current (should be), make the output extend and add item sfrom another list. if (datetime.datetime.strptime(res[0].decode(), "%Y/%m/%d") > date): output.extend(getAllDupsFromDate(res[0].decode(), db)) res = cur.next() cur.close() return output
def getDateLess(date, eq, db): cur = db.cursor() res = None res = cur.set_range(date.encode()) if res == None: return [] # If we include the original key, we can set the output to be the dups from price. If not we set output to empty if eq: output = getAllDupsFromDate(date, db) else: output = [] res = cur.prev() date = datetime.datetime.strptime(date, "%Y/%m/%d") if eq: while res != None: # If the date is <= the current date, we add it. if (datetime.datetime.strptime(res[0].decode(), "%Y/%m/%d") <= date): output.extend(getAllDupsFromDate(res[0].decode(), db)) res = cur.prev() else: while res != None: # If the date is < to the current date, we add it if (datetime.datetime.strptime(res[0].decode(), "%Y/%m/%d") < date): output.extend(getAllDupsFromDate(res[0].decode(), db)) res = cur.prev() cur.close() return output
def getPriceLess(price, eq, db): cur = db.cursor() res = None res = cur.set_range(price.encode()) if res == None: return [] # If we include the original key, we can set the output to be the dups from price. If not we set output to empty if eq: output = getAllDupsFromPrice(price, db) else: output = [] res = cur.prev() if eq: while res != None: # If the ads price is <= to the searching price, we add it. if (int(res[0]) <= int(price)): output.extend(getAllDupsFromPrice(res[0].decode(), db)) res = cur.prev() else: while res != None: # If the ads price is < the search price, we add it. if (int(res[0]) < int(price)): output.extend(getAllDupsFromPrice(res[0].decode(), db)) res = cur.prev() cur.close() return output
def getPriceGreater(price, eq, db): cur = db.cursor() res = None res = cur.set_range(price.encode()) if res == None: return [] # If we include the original key, we can set the output to be the dups from price. If not we set output to empty if eq: output = getAllDupsFromPrice(price, db) else: output = [] res = cur.next() if eq: while res != None: # If the price is >= to the current (should be), make the output extend and add item sfrom another list. if (int(res[0]) >= int(price)): output.extend(getAllDupsFromPrice(res[0].decode(), db)) res = cur.next() else: while res != None: # If the price is >= to the current (should be), make the output extend and add item sfrom another list. if (int(res[0]) > int(price)): output.extend(getAllDupsFromPrice(res[0].decode(), db)) res = cur.next() cur.close() return output
def getAllDups(key, db): cur = db.cursor() output = [] res = cur.set(key.encode()) if res == None: return [] output.append(res) dup = cur.next_dup() while (dup != None): output.append(dup) dup = cur.next_dup() cur.close() return output
def dumpDB(db): curs = db.cursor() iter = curs.first() while (iter): print(iter) #iterating through duplicates dup = curs.next_dup() while (dup != None): print(dup) dup = curs.next_dup() iter = curs.next()
def search_db(db, expr, keep_index=None): # Search key from the given database. # expr: key # keep_index: specify the return should contain key:0 or value:1, if # None then key-value pair is returned. cursor = db.cursor() item = cursor.set(expr) result = [] while item != None: if keep_index is None: result.append(item) else: result.append(item[keep_index]) item = cursor.next_dup() return result
def getAllDupsFromPrice(key, db): cur = db.cursor() output = [] res = cur.set(key.encode()) if res == None: return [] # Append the result since its not null output.append(res[1].decode().split(',')[0].encode()) dup = cur.next_dup() # While there is still a duplicate, run through it and get its ad id. while (dup != None): output.append(dup[1].decode().split(',')[0].encode()) dup = cur.next_dup() cur.close() return output
def iter_db_file(self, db_path): zoom = self.get_zoom(db_path) # also checks data in range if not zoom: return db = self.sqlite3.connect(db_path) dbc = db.cursor() dbc.execute('SELECT x, y, MAX(v), b FROM t GROUP BY x,y;') for x, y, version, data in dbc: if data: coord = [zoom, x, y] #~ log('db tile', coord, tile[:20], path) #~ log('tile', coord, data) yield PixBufTile(coord, data, key=(db_path, coord)) db.close()
def getLocationQuery(key, db): output = [] curs = db.cursor() iter = curs.first() while (iter): cat = iter[1].decode().split(',')[2] # If the category is the category we want to search for, add its aid to the output if cat.lower() == key: output.append(iter[1].decode().split(',')[0].encode()) dup = curs.next_dup() while (dup != None): cat = dup[1].decode().split(',')[2] if cat.lower() == key: output.append(iter[1].decode().split(',')[0].encode()) dup = curs.next_dup() iter = curs.next() curs.close() return output
def getTermQuery(keyword, db, wildcard=False): cur = db.cursor() res = None # If we are using a wildcard, we need to remove the % and search on the set range. if wildcard == True: keyword = keyword[:len(keyword) - 1] res = cur.set_range(keyword.encode()) # if res is None, return nothing. if res == None: return [] # If we are not using a wildcard and it doesn't match exactally: if res[0].decode() != keyword and not wildcard: return [] output = [] if wildcard: iter = res while keyword in iter[0].decode(): output.append(iter[1]) dup = cur.next_dup() while (dup != None): output.append(dup[1]) dup = cur.next_dup() iter = cur.next() else: output.append(res[1]) dup = cur.next_dup() while (dup != None): output.append(dup[1]) dup = cur.next_dup() cur.close() return output