Update sqlite3/README.md
This commit is contained in:
@@ -13,8 +13,7 @@ class DB(sqlite3.Connection):
|
||||
''')
|
||||
|
||||
def __setitem__(self, key, value):
|
||||
if not isinstance(value, bytes):
|
||||
value = json.dumps(value)
|
||||
if not isinstance(value, bytes): value = json.dumps(value)
|
||||
with self:
|
||||
cur = self.execute('''
|
||||
INSERT OR REPLACE INTO kv_store
|
||||
@@ -31,28 +30,29 @@ class DB(sqlite3.Connection):
|
||||
if result:
|
||||
if isinstance(result[0], str):
|
||||
try: return json.loads(result[0])
|
||||
except: pass
|
||||
except json.JSONDecodeError: pass
|
||||
return result[0]
|
||||
|
||||
def delete(self, key):
|
||||
with self:
|
||||
cur = self.execute('''
|
||||
DELETE FROM kv_store WHERE key = ?
|
||||
DELETE FROM kv_store
|
||||
WHERE key = ?
|
||||
''', (key,))
|
||||
return {"deleted_count": cur.rowcount}
|
||||
|
||||
|
||||
def keys(self, pattern='*'):
|
||||
pattern = pattern.translate(str.maketrans({
|
||||
'\\': '\\\\', '%': '\\%', '_': '\\_', '*': '%', '?': '_'
|
||||
}))
|
||||
|
||||
with self:
|
||||
cur = self.execute('''
|
||||
result = self.execute('''
|
||||
SELECT key FROM kv_store
|
||||
WHERE key LIKE ? ESCAPE '\\'
|
||||
''', (pattern,))
|
||||
return [row[0] for row in cur.fetchall()]
|
||||
''', (pattern,)).fetchall()
|
||||
return [row[0] for row in result]
|
||||
|
||||
def __repr__(self): return repr(self.keys())
|
||||
```
|
||||
# Usage
|
||||
```python
|
||||
|
||||
Reference in New Issue
Block a user