-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatabase.py
123 lines (105 loc) · 4.02 KB
/
database.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
import sqlite3
class Database:
def __init__(self, name=None):
self.__conn = None
self.__cursor = None
if name:
self.open_connection(name)
def open_connection(self, name):
try:
self.__conn = sqlite3.connect(name)
self.__cursor = self.__conn.cursor()
except sqlite3.Error as e:
print('Error connecting to database: ', e)
def close_connection(self):
if self.__conn:
self.__conn.commit()
self.__cursor.close()
self.__conn.close()
def get(self, table, headings, limit=None):
'''
Function that returns records from specified table
:param table:
:param headings:
:param limit:
:return:
'''
try:
data_result = []
query = "SELECT " + ', '.join(headings) + ' FROM {};'.format(table)
for row in self.__cursor.execute(query):
data_result.append([row[0], row[1], row[2], row[3], row[4]])
return data_result
except sqlite3.Error as e:
print("Error retrieving data: ", e)
def get_specified(self, table, headings, condition):
'''
Function that returns records filtered by the condition
:param table:
:param headings:
:param condition:
:return: iterator
'''
try:
query = "SELECT " + ', '.join(headings) + ' FROM {} '.format(table) + condition
return self.__cursor.execute(query)
except sqlite3.Error as e:
print("Error retrieving data: ", e)
def write(self, table, data):
'''
Function to write data from the interface into the database.
:param table:
:param data:
:return: None
'''
try:
if data['-MOVIMENTO_ENTRATA-']:
self.__cursor.execute("INSERT INTO {} (TIPO_DI_MOVIMENTO,CATEGORIA,DATA,IMPORTO) VALUES (?,?,?,?);".format(table), ('Entrata', data['-TIPO_ENTRATA-'], data['-DATA_MOVIMENTO-'], data['-ENTRATA-']))
elif data['-MOVIMENTO_USCITA-']:
self.__cursor.execute("INSERT INTO {} (TIPO_DI_MOVIMENTO,CATEGORIA,DATA,IMPORTO) VALUES (?,?,?,?);".format(table), ('Uscita', data['-TIPO_USCITA-'], data['-DATA_MOVIMENTO-'], data['-USCITA-']))
except sqlite3.Error as e:
print('Error writing data: ', e)
def create_table(self, name):
try:
query = ('''CREATE TABLE {}
(ID INTEGER PRIMARY KEY,
TIPO_DI_MOVIMENTO TEXT NOT NULL,
CATEGORIA TEXT NOT NULL,
DATA TEXT NOT NULL,
IMPORTO REAL NOT NULL);'''.format(name))
self.__cursor.execute(query)
except sqlite3.Error as e:
print('Error creating database: ', e)
def list_tables(self):
'''
Function that shows all tables in the database
:return:
'''
try:
tables = []
query = "SELECT name FROM sqlite_master WHERE type='table';"
self.__cursor.execute(query)
for row in self.__cursor.fetchall():
tables.append(row[0])
return tables
except sqlite3.Error as e:
print('Error retrieving tables: ', e)
def delete_table(self, name):
'''
Function that delete the specified table from the database
:param name:
:return:
'''
self.__conn.execute("DROP TABLE {}".format(name))
def delete_row(self, name, row_id):
'''
Function that delete the specified row from the specified table
:param name:
:param row_id:
:return:
'''
try:
query = ('DELETE FROM {0} WHERE ID = {1};'.format(name, row_id))
self.__cursor.execute(query)
except sqlite3.Error as e:
print('Error deleting row: ', e)