diff options
| author | Tianhao Wang <wth@riseup.net> | 2024-01-11 13:50:41 +0000 |
|---|---|---|
| committer | Tianhao Wang <wth@riseup.net> | 2024-01-11 13:50:41 +0000 |
| commit | 7b3ef7a4726ba1e76f99b8569767aea5be99a1f5 (patch) | |
| tree | 5261d467e164e6542921ad35a29738d26f572123 /catdb.py | |
| parent | ebc18cf2aa3d486b744bceba1ae1479672b8bddd (diff) | |
interactions based on intimacy
Diffstat (limited to 'catdb.py')
| -rw-r--r-- | catdb.py | 105 |
1 files changed, 59 insertions, 46 deletions
@@ -3,53 +3,66 @@ 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) - - # 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 __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) |
