-
Notifications
You must be signed in to change notification settings - Fork 3
/
database_handler.py
123 lines (94 loc) · 3.87 KB
/
database_handler.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# -*- coding:utf-8 -*-
"""数据库操作类
Author: github@luochang212
Date: 2020-11-15
"""
import sqlite3
class DatabaseHandler:
"""database handler"""
def __init__(self, db_name: str, check_same_thread: bool = True):
"""init"""
self.db_name = db_name
self.conn = sqlite3.connect(
'{}.db'.format(db_name), check_same_thread=check_same_thread)
self.c = self.conn.cursor()
def execute(self, cmd: str):
"""Execute command"""
self.c.execute(cmd)
self.conn.commit()
def create_table(self, table_name: str, columns: dict):
"""Create table"""
lst = [str(k) + ' ' + str(v) for k, v in columns.items()]
columns_str = ','.join(lst)
cmd = 'CREATE TABLE {table_name}({columns_str})'
self.execute(cmd.format(
table_name=table_name,
columns_str=columns_str))
def insert_data(self, table_name: str, columns: dict, data: dict):
"""Insert a row of data"""
lst = ["'" + str(v) + "'" if columns[k] == 'TEXT' else str(v)
for k, v in data.items()]
data_str = ','.join(lst)
cmd = 'INSERT INTO {table_name} VALUES ({data_str})'
self.execute(cmd.format(
table_name=table_name,
data_str=data_str))
def update_data(self, table_name: str, columns: dict, data: dict, condition: dict):
"""Update data"""
lst1 = [str(k) + '=' + "'" + str(v) + "'" if columns[k] == 'TEXT'
else str(k) + '=' + str(v)
for k, v in data.items()]
value_str = ','.join(lst1)
lst2 = [str(k) + '=' + "'" + str(v) + "'" if columns[k] == 'TEXT'
else str(k) + '=' + str(v)
for k, v in condition.items()]
condition_str = ' AND '.join(lst2)
cmd = 'UPDATE {table_name} SET {value_str} WHERE {condition_str}'
self.execute(cmd.format(
table_name=table_name,
value_str=value_str,
condition_str=condition_str))
def delete_data(self, table_name: str, columns: dict, condition: dict):
"""Delete data"""
lst = [str(k) + '=' + "'" + str(v) + "'" if columns[k] == 'TEXT'
else str(k) + '=' + str(v)
for k, v in condition.items()]
condition_str = ' AND '.join(lst)
cmd = 'DELETE FROM {table_name} WHERE {condition_str}'
self.execute(cmd.format(
table_name=table_name,
condition_str=condition_str))
def fetch_data(self, table_name: str, columns: dict, condition: dict):
"""Fetch data"""
lst = [str(k) + '=' + "'" + str(v) + "'" if columns[k] == 'TEXT'
else str(k) + '=' + str(v)
for k, v in condition.items()]
condition_str = ' AND '.join(lst)
cmd = 'SELECT * FROM {table_name} WHERE {condition_str}'
self.execute(cmd.format(
table_name=table_name,
condition_str=condition_str))
return self.c.fetchall()
def fetch_all(self, table_name: str):
"""Fetch data"""
cmd = 'SELECT * FROM {table_name}'
self.execute(cmd.format(
table_name=table_name))
return self.c.fetchall()
def check_existence(self, table_name: str, columns: dict, condition: dict):
"""check the existence of item"""
try:
res = self.fetch_data(table_name, columns, condition)
if len(res) == 0:
return False
except Exception:
return False
return True
if __name__ == '__main__':
dbh = DatabaseHandler(db_name="CalendarDB")
print(dbh.check_existence(
'calendar',
{"sid": "TEXT", "name": "TEXT", "content": "TEXT", "category": "TEXT", "level": "INTEGER",
"status": "REAL", "creation_time": "TEXT", "start_time": "TEXT", "end_time": "TEXT"},
{"sid": "22"}
))