mirror of
https://github.com/TriliumNext/Notes.git
synced 2025-07-27 18:12:29 +08:00
44 lines
1.1 KiB
Python
44 lines
1.1 KiB
Python
![]() |
import sqlite3
|
||
|
import base64
|
||
|
|
||
|
def dict_factory(cursor, row):
|
||
|
d = {}
|
||
|
for idx, col in enumerate(cursor.description):
|
||
|
if isinstance(row[idx], buffer):
|
||
|
d[col[0]] = base64.b64encode(row[idx])
|
||
|
else:
|
||
|
d[col[0]] = row[idx]
|
||
|
|
||
|
return d
|
||
|
|
||
|
conn = sqlite3.connect('demo.ncdb')
|
||
|
conn.row_factory = dict_factory
|
||
|
|
||
|
def insert(tablename, rec):
|
||
|
keys = ','.join(rec.keys())
|
||
|
question_marks = ','.join(list('?'*len(rec)))
|
||
|
values = tuple(rec.values())
|
||
|
cursor = execute('INSERT INTO '+tablename+' ('+keys+') VALUES ('+question_marks+')', values)
|
||
|
return cursor.lastrowid
|
||
|
|
||
|
def delete(tablename, note_id):
|
||
|
execute("DELETE FROM " + tablename + " WHERE note_id = ?", [note_id])
|
||
|
|
||
|
def execute(sql, params=[]):
|
||
|
cursor = conn.cursor()
|
||
|
cursor.execute(sql, params)
|
||
|
return cursor
|
||
|
|
||
|
def getResults(sql, params=[]):
|
||
|
cursor = conn.cursor()
|
||
|
query = cursor.execute(sql, params)
|
||
|
return query.fetchall()
|
||
|
|
||
|
def getSingleResult(sql, params=()):
|
||
|
cursor = conn.cursor()
|
||
|
query = cursor.execute(sql, params)
|
||
|
return query.fetchone()
|
||
|
|
||
|
def commit():
|
||
|
conn.commit()
|