-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb.py
186 lines (147 loc) · 5.28 KB
/
db.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
import sqlite3
class Archivo:
def __init__(self, name):
"""Connects to the database.
The name of the database is passed as a parameter.
"""
self.conn = sqlite3.connect(name)
def __del__(self):
self.conn.close()
def create_table(self, table):
"""Creates a new table in the database.
The name of the table is passed as a parameter.
Returns true if the table is created correctly, false otherwise.
"""
exp = """
CREATE TABLE %s
(num INT PRIMARY KEY,
princCompareciente TEXT SECONDARY KEY,
otrosComparecientes TEXT,
nombreEscritura TEXT)
""" % (table,)
try:
curs = self.conn.cursor()
curs.execute(exp)
return True
except Exception:
return False
def delete_table(self, table):
"""Deletes a table from the database.
The name of the table is passed as a parameter.
Returns true if the table is deleted correctly, false otherwise.
"""
exp = """
DROP TABLE %s
""" % (table,)
try:
curs = self.conn.cursor()
curs.execute(exp)
return True
except Exception:
return False
def view_table(self, table):
"""Returns the content of a table in the database.
The name of the table is passed as a parameter.
Returns a list where every element corresponds to a row of the table.
If an error occurs returns None.
"""
exp = """SELECT * FROM %s""" % (table,)
try:
cur = self.conn.cursor()
cur.execute(exp)
return cur.fetchall()
except Exception:
return None
def insert_data(self, table, num, p_name, o_name, n_escr):
"""Inserts a new row in a table.
The name of the table and all the necessary data are passed
as parameters.
Returns true if the data is inserted correctly, false otherwise.
"""
exp = """INSERT INTO %s
(num, princCompareciente, otrosComparecientes, nombreEscritura)
VALUES(?, ?, ?, ?)""" % (table,)
try:
cur = self.conn.cursor()
cur.execute(exp, (num, p_name, o_name, n_escr))
self.conn.commit()
return True
except Exception:
return False
def doc_by_number(self, table, number):
"""Gets a row of table filtered by the number of the document.
The name of the table and the number of the document are passed
as parameters
Returns the row of the table that match with the number of the
document. If an error occurs return None.
"""
exp = """SELECT * FROM %s WHERE num = ?""" % (table,)
try:
cur = self.conn.cursor()
cur.execute(exp, (number,))
return cur.fetchall()
except Exception:
return None
def max_number(self, table):
"""Return the maximum number amongst the documents stored in a table.
The name of the table is passed as a parameter.
Returns the maximum number amongst the documents stored in a table.
If an error occurs returns None.
"""
exp = """SELECT MAX(num) FROM %s""" % table
# try:
cur = self.conn.cursor()
cur.execute(exp)
return cur.fetchone()
# except Exception:
# return None
def view_tables(self):
"""Returns the names of all the tables in the database.
Returns a list of names of the database's tables.
If an error occurs returns None.
"""
exp = """SELECT name FROM sqlite_master WHERE type='table'"""
try:
cur = self.conn.cursor()
cur.execute(exp)
return cur.fetchall()
except Exception:
return None
def get_table_by_letter(self, protocol, letter, number_ini, number_fin):
exp = """SELECT *
FROM %s
WHERE princCompareciente
LIKE '%s%%'
AND %d <= num
AND num <= %d""" \
% (protocol, letter, number_ini, number_fin)
try:
cur = self.conn.cursor()
cur.execute(exp)
return cur.fetchall()
except Exception:
return None
def delete_data(self, table, number):
exp = """DELETE FROM %s WHERE num = ?""" % (table,)
try:
cur = self.conn.cursor()
cur.execute(exp, (number,))
self.conn.commit()
return True
except Exception:
return None
def search(self, table, field, string):
sel = """SELECT *
FROM %s
WHERE %s
LIKE '%%%s%%'""" % (table, field, string)
cur = self.conn.cursor()
cur.execute(sel)
return cur.fetchall()
if __name__ == "__main__":
miArchivo = Archivo('Protocolo.db')
# miArchivo.insert_data("Protocolo2017", 1, "El Yo", "", "TEST")
# print(miArchivo.get_table_by_letter("Protocolo2017", "E", 0, 1000))
# print(miArchivo.delete_data("Protocolo2017", 1))
# print(miArchivo.get_table_by_letter("Protocolo2017", "E", 0, 1000))
print(miArchivo.search("Ba"))