def __getitem__(self, key):
# ====== multiple keys select ====== #
if isinstance(key, (tuple, list, np.ndarray)):
query = """SELECT value FROM {tb}
WHERE key IN {keyval};"""
keyval = '(' + ', '.join(['"%s"' % str(k) for k in key]) + ')'
self.cursor.execute(
query.format(tb=self._current_table, keyval=keyval))
results = self.cursor.fetchall()
# check if any not found keys
if len(results) != len(key):
raise KeyError("Cannot find all `key`='%s' in the dictionary." % keyval)
# load binary data
results = [marshal.loads(r[0]) for r in results]
# ====== single key select ====== #
else:
key = str(key)
if key in self.current_cache:
return self.current_cache[key]
query = """SELECT value FROM {tb} WHERE key="{keyval}" LIMIT 1;"""
results = self.connection.execute(
query.format(tb=self._current_table, keyval=key)).fetchone()
# results = self.cursor.fetchone()
if results is None:
raise KeyError("Cannot find `key`='%s' in the dictionary." % key)
results = marshal.loads(results[0])
return results
评论列表
文章目录