Skip to content

Instantly share code, notes, and snippets.

@kkimdev
Last active June 15, 2018 21:04
Show Gist options
  • Save kkimdev/7b85d5de573098ea72c563d5db9ce70f to your computer and use it in GitHub Desktop.
Save kkimdev/7b85d5de573098ea72c563d5db9ce70f to your computer and use it in GitHub Desktop.
import sqlite3
class KeyValueFile:
def __init__(self, path):
self.conn = sqlite3.connect(path)
self.c = self.conn.cursor()
self.c.execute('CREATE TABLE IF NOT EXISTS keyvalue (key text PRIMARY KEY, value text)')
def put(self, key, value):
self.c.execute("INSERT OR REPLACE INTO keyvalue VALUES (?,?)", (key, value))
self.conn.commit()
def get(self, key):
self.c.execute("SELECT * FROM keyvalue WHERE key=?", (key, ))
result = self.c.fetchone()
if result is None:
return None
return result[1]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment