-
Notifications
You must be signed in to change notification settings - Fork 0
/
easySqlite3.py
executable file
·231 lines (180 loc) · 5.84 KB
/
easySqlite3.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
import sqlite3
from sqlite3 import Error
"""
this module is used for managing data base connections and every thing related;
this applies for all(in case True that means the query worked False if else)
for all the methods declared below, have one thing in common ::
True ==> if the methods works fine
False ==> else
need improvements, so go ahead.
"""
class Db(object):
def __init__(self, db):
self.db = db
self.conn = ""
self.query = ""
self.cur = ""
def connect(self):
"""
function used for starting the connection, to
the data base -db-.
"""
try:
self.conn = sqlite3.Connection(self.db)
self.cur = self.conn.cursor()
return True
except Error:
self.conn.close()
return False
def close(self):
"""
at the end of what ever you are doing in the DB, use this
method to close all connections.
might say the garbage collector will take care, but no
clean code is way better for both meaning and performance.
"""
try:
self.cur.close()
self.conn.close()
return True
except Error:
return False
def createTable(self, table_query):
"""
this functions is made specially for creating tables,
you can use the other function listed below, but this way i know
exactly what i am doing.
table_query == the quey to insert the table
table = \"""
CREATE TABLE IF NOT EXISTS profile(
id INTEGER PRIMARY KEY,
nom TEXT NOT NULL,
prenom TEXT NOT NULL
);
\"""
"""
try:
self.cur.execute(table_query)
self.conn.commit()
return True
except Error:
return False
def prepareQuery(self, query):
"""
At first, call the method with ::
quey = INSERT INTO tasks(name,priority,status_id,project_id,begin_date,end_date)
VALUES(?,?,?,?,?,?)
insert = prepareQuery(query)
just to prepare the query for doing what ever
like :: multi inserting elements.
then call the second method like
insertRow(list of values by the same order)
this inner method can be called as many as you need.
before using any of the methods listed under, you should use this one before.
"""
try:
self.query = query
return True
except Error:
return False
def insertRow(self, row):
"""
but before using it, use the prepareQuery(), then call this
one with a list of the parameters(Values).
as said for inserting multiple data rows.
Ex:
---
prepareQuery(query)
insertRow([data1, data2, ...........])
"""
try:
self.cur.execute(self.query, row)
self.conn.commit()
return True
except Error:
return False
def justQuery(self, data=None):
"""
but before using it, use the prepareQuery(), then call this
one with a list of the parameters(Values).
for another usage query like delete, update any other type for queries
that does not have anything as an output on.
Ex:
---
query = ALTER TABLE [YOUR TABLE]
SET [COLMUN1]= ? -VALUE1-
WHERE [COLOMUN2]=? -VALUE2-
prepareQuery(query)
insertRow([data1, data2])
"""
try:
if data is None:
self.cur.execute(self.query)
else:
self.cur.execute(self.query, data)
self.conn.commit()
return True
except:
return False
def extractData(self, data=None):
"""
but before using it, use the prepareQuery(), then call this
one with a list of the parameters(Values).
for data extraction; but it does not give the rows,
so you should know them.
in case it returns None it means that there is something wrong with the query
or it is just that the query output is empty.
Ex1:
---
query = SELECT * FROM TABLE [YOUR TABLE]
WHERE [COLOMUN2]=? -VALUE1-
prepareQuery(query)
insertRow([data1])
Ex2:
---
query = SELECT * FROM TABLE [YOUR TABLE]
prepareQuery(query)
insertRow()
"""
try:
if data is not None:
self.cur.execute(self.query, data)
else:
self.cur.execute(self.query)
for row in self.cur.fetchall():
yield row
except Error:
yield None
def notTableOutPutQuey(self, data=None):
"""
but before using it, use the prepareQuery(), then call this
one with a list of the parameters(Values).
this is made for -- none table -- output queries
like:
select coun(*)
select max()....
with out Goup By
in case it returns None it means that there is some thing wrong with the query.
Ex1:
----
query = SELECT count(*) FROM [YOUR TABLE]
prepareQuery(query)
notTableOutPutQuery()
Ex2:
----
query = SELECT count(*) FROM [YOUR TABLE]
HAVING BY [COLOMN]=? -VALUE-
prepareQuery(query)
notTableOutPutQuery([data])
"""
try:
if data is not None:
self.cur.execute(self.query, data)
else:
self.cur.execute(self.query)
d = self.cur.fetchall()[0]
return d[0]
except Error:
return None
if __name__ == "__main__":
pass