-
Notifications
You must be signed in to change notification settings - Fork 0
/
sqlite3_requests.py
167 lines (148 loc) · 4.51 KB
/
sqlite3_requests.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
import sqlite3
import os
from tables import tables_arr, default_cats_arr, default_currs_arr
def check_init(db_filename):
''' Connect to db, create if it doesn't exist, return conn obj '''
try:
conn = sqlite3.connect(db_filename)
except Exception as e:
return 'Error: ' + str(e)
finally:
if conn:
conn.close()
if os.stat(db_filename).st_size == 0:
result = init_db(db_filename)
return result
return True
def init_db(db_filename):
''' Init db, create tables, input default values '''
print('Init db, filename:\'' + db_filename + '\'')
try:
conn = sqlite3.connect(db_filename)
c = conn.cursor()
# create tables
for table in tables_arr:
c.execute(table)
# insert default values
c.executemany('INSERT INTO categories(type, name) VALUES (?,?)', default_cats_arr)
for curr in default_currs_arr:
c.execute('INSERT INTO currencies(name) VALUES (?)', (curr,))
# make sure changes are permanent
conn.commit()
return True
except sqlite3.Error as e:
return 'Error: ' + str(e)
finally:
if conn:
conn.close()
def select(db_filename, table, fields='*', filters=None):
''' Make SELECT request to db '''
try:
conn = sqlite3.connect(db_filename)
c = conn.cursor()
request = 'SELECT ' + fields + ' FROM ' + table
if not filters == None:
request += ' WHERE ' + filters
c.execute(request)
result = c.fetchall()
except sqlite3.Error as e:
return 'Error: ' + str(e)
finally:
if conn:
conn.close()
return result
def add_many_records_to_db(db_filename, table, fields_arr):
''' fields is an array of arrys: [field_name, value] is one key=value pair in record
'fields_arr' is a array of 'fields' arrs '''
try:
request_template = 'INSERT INTO {tbl_name} ({flds}) VALUES ({qstn_marks})'
for fields in fields_arr:
field_names = ''
question_marks = ''
values = ''
conn = sqlite3.connect(db_filename)
c = conn.cursor()
for counter, field in enumerate(fields):
field_names += field[0]
question_marks += '?'
values += str(field[1])
if counter == len(fields) - 1:
break
field_names += ', '
question_marks += ','
values +='`'
values = tuple(values.split('`'))
request = request_template.format(tbl_name=table, flds=field_names, qstn_marks=question_marks)
c.execute(request, (values))
result = conn.commit()
if result == None:
result = fields
except sqlite3.Error as e:
return 'Error: ' + str(e)
finally:
if conn:
conn.close()
return result
def add_record_to_db(db_filename, table, fields):
''' fields is an array of arrys: [field_name, value] is one key=value pair in record '''
try:
request_template = 'INSERT INTO {tbl_name} ({flds}) VALUES ({qstn_marks})'
field_names = ''
question_marks = ''
values = ''
conn = sqlite3.connect(db_filename)
c = conn.cursor()
for counter, field in enumerate(fields):
field_names += field[0]
question_marks += '?'
values += str(field[1])
if counter == len(fields) - 1:
break
field_names += ', '
question_marks += ','
values +='`'
values = tuple(values.split('`'))
request = request_template.format(tbl_name=table, flds=field_names, qstn_marks=question_marks)
c.execute(request, (values))
result = conn.commit()
if result == None:
result = fields
except sqlite3.Error as e:
return 'Error: ' + str(e)
finally:
if conn:
conn.close()
return result
def del_records_from_db(db_filename, table, filters):
''' Delete records from any table in db by filters '''
try:
conn = sqlite3.connect(db_filename)
c = conn.cursor()
request = 'DELETE FROM ' + table + ' WHERE '
for filt in filters:
request += filt + ' '
c.execute(request)
result = conn.commit()
if result == None:
result = filters
except sqlite3.Error as e:
return 'Error: ' + str(e)
finally:
if conn:
conn.close()
return result
def update_records_in_db(db_filename, table, new_data, filters):
''' Update records in db by filters '''
try:
request_template = 'UPDATE {tbl} SET {data} WHERE {fltrs}'
conn = sqlite3.connect(db_filename)
c = conn.cursor()
request = request_template.format(tbl=table, data=new_data, fltrs=filters)
c.execute(request)
result = conn.commit()
except sqlite3.Error as e:
return 'Error: ' + str(e)
finally:
if conn:
conn.close()
return result