-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdb.py
60 lines (45 loc) · 1.38 KB
/
db.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import atexit
import sqlite3
get_all_mappings_query = '''
SELECT * FROM mappings
'''
get_all_mapping_names_query = '''
SELECT DISTINCT mname FROM mappings
'''
get_mapping_from_name_query = '''
SELECT key, note FROM mappings
WHERE mname = "{0}"
'''
delete_mapping_from_name_query = '''
DELETE FROM mappings
WHERE mname = "{0}"
'''
insert_mapping_query = '''
INSERT OR IGNORE INTO mappings VALUES(?, ?, ?)
'''
class db(object):
def __init__(self):
super(db, self).__init__()
self.conn = sqlite3.connect('mappings.db')
atexit.register(self.close_connection)
self.c = self.conn.cursor()
def close_connection(self):
self.conn.close()
print "db connection closed"
def get_all_mappings(self):
self.c.execute(get_all_mappings_query)
return self.c.fetchall()
def get_all_mapping_names(self):
self.c.execute(get_all_mapping_names_query)
return self.c.fetchall()
def get_mapping_from_name(self, mname):
self.c.execute(get_mapping_from_name_query.format(mname))
return self.c.fetchall()
def delete_mapping_from_name(self, mname):
self.c.execute(delete_mapping_from_name_query.format(mname))
self.conn.commit()
def insert_mapping(self, mapping):
if not mapping:
return
self.c.executemany(insert_mapping_query, mapping)
self.conn.commit()