def krpc_store_value(self, key, value, id, _krpc_sender): t = "%0.6f" % time.time() c = self.store.cursor() try: c.execute("insert into kv values (%s, %s, %s);", (sqlite.encode(key), sqlite.encode(value), t)) except sqlite.IntegrityError, reason: # update last insert time c.execute("update kv set time = %s where key = %s and value = %s;", (t, sqlite.encode(key), sqlite.encode(value)))
def store(self, id, password): """Save or overwrite a password in the database""" # First try inserting a new row try: self.cursor.execute("INSERT INTO auth (id, password) VALUES ('%s', '%s')" % (sqlite.encode(id), sqlite.encode(password))) except sqlite.IntegrityError: # Violated the primary key constraint, update an existing item self.cursor.execute("UPDATE auth SET password = '******' WHERE id = '%s'" % (sqlite.encode(password), sqlite.encode(id))) self.connection.commit()
def _encode(self, obj): """Encode an object that may not be a plain string""" if type(obj) is unicode: obj = obj.encode('utf-8') elif type(obj) is not str: obj = str(obj) return "'%s'" % sqlite.encode(obj)
def retrieveValues(self, key): c = self.store.cursor() c.execute("select value from kv where key = %s;", sqlite.encode(key)) t = c.fetchone() l = [] while t: l.append(t['value']) t = c.fetchone() return l
def _dumpRoutingTable(self): """ save routing table nodes to the database """ c = self.store.cursor() c.execute("delete from nodes where id not NULL;") for bucket in self.table.buckets: for node in bucket.l: c.execute("insert into nodes values (%s, %s, %s);", (sqlite.encode(node.id), node.host, node.port)) self.store.commit()
def lookup(self, id, ignoreStored=False): """Lookup a password for the given ID, returning via a Deferred""" if not ignoreStored: self.cursor.execute("SELECT password FROM auth WHERE id = '%s'" % sqlite.encode(id)) row = self.cursor.fetchone() if row: return defer.succeed(sqlite.decode(row[0])) result = defer.Deferred() defer.maybeDeferred(self.promptForPassword, id).addCallback( self._storeNewPassword, id, result).addErrback(result.errback) return result
def _quote(self): return "'%s'" % sqlite.encode(self.s)
def _quote(self): return "'%s'" % sqlite.encode(self.s) # make a test database in memory, get a cursor on it, and make a table connection = sqlite.connect(':memory:')
def _saveSelfNode(self): c = self.store.cursor() c.execute('delete from self where num = 0;') c.execute("insert into self values (0, %s);", sqlite.encode(self.node.id)) self.store.commit()