summaryrefslogtreecommitdiff
path: root/catdb.py
blob: 2b94ee6bcb2340cf9c8bf3f44073f4df593f144f (plain)
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
60
61
62
63
64
65
66
67
68
import config as cfg
from catquery import *
import sqlite3

class DBHandler():
	def __init__(self,config):
		self.DB = cfg.DB_NAME
		self.init_db()
		return

	def init_db(self):
		# self.commit(DB_SCHEMA)
		self.commit(DB_SCHEMA_INT)

	# this wrapper doesn't handle exception
	# catch them in the caller.
	# another remark: we don't expect much DB throughput
	# and it's not necessary to maintain a long-lived connection
	def commit(self, str, data=None):
		conn = sqlite3.connect(self.DB)
		cur = conn.cursor()
		if data == None:
			cur.execute(str)
		else:
			cur.execute(str,data)
		conn.commit()
		conn.close()

	# read-only access
	def query(self,str):
		conn = sqlite3.connect(self.DB)
		cur = conn.cursor()
		res = cur.execute(str).fetchall()
		conn.close()
		return res

	# the insert_event exception should be handled here
	# because this is used as a log, and should have no effect
	# on the programs even if it fails
	def insert_event(self, _type, remarks, correspond):
		try:
			self.commit(QUERY_INSERT_EVENT, (_type,remarks,correspond))
		except Exception as e:
			print("ERROR ","failed to insert event to the db, ignoring ",e)

	def count_interaction(self):
		try:
			res = self.query(QUERY_COUNT_INTERACTION)
			return dict(res)
		except:
			print("query failed")
		return None

	def get_intimacy(self):
		try:
			res = self.query(QUERY_GET_INT)
			return dict(res)
		except:
			print("query failed")
		return None

	def update_intimacy(self, name):
		try:
			self.commit(QUERY_UPDATE_INT,(name,name))
		except Exception as e:
			print("ERROR", "failed to update intimacy ",e)