-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdatabase_conn.py
196 lines (160 loc) · 6.6 KB
/
database_conn.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
from chooseCategory import Category
from re import sub
import pymysql
import json
class DataBase:
def __init__(self):
#Load config.json
jsonFile = open('config.json')
config = json.load(jsonFile)
#Get database info
self.database = config['db']
self.host = self.database['host']
self.user = self.database['user']
self.password = self.database['password']
self.db = self.database['db_name']
#Connect to database
self.connection = pymysql.connect(host=self.host, user=self.user, password=self.password, db=self.db)
self.cursor = self.connection.cursor()
print("Connected succedfully to database")
# OpenAI
self.category = Category()
def check_user(self, username):
query = "SELECT * FROM users WHERE username = %s"
self.cursor.execute(query, (username))
result = self.cursor.fetchone()
if result:
return True
else:
return False
def create_user(self, username, currency):
query = "CREATE TABLE `%s` (id INT AUTO_INCREMENT PRIMARY KEY, income DECIMAL, expense DECIMAL, date datetime DEFAULT CURRENT_TIMESTAMP, description VARCHAR(255), category VARCHAR(10))"
self.cursor.execute(query, (username,))
# Create a column in balance_users table
sql = "INSERT INTO `balance_users` (`user`, `balance`, `total_income`, `total_expense`) VALUES (%s, '0', '0', '0')"
self.cursor.execute(sql, (username,))
# Create a column in currency table
sql = "INSERT INTO `currency` (`id`, `currency`) VALUES (%s, %s)"
self.cursor.execute(sql, (username, currency))
def add_column(self, username, income, expense, description):
if expense != 0:
category = self.category.chooseCategory(description)
sql = "INSERT INTO `%s` (`income`, `expense`, `description`, `category`) VALUES ('0', '%s', '%s', '%s')"
else:
sql = "INSERT INTO `%s` (`income`, `expense`, `description`) VALUES ('%s', '0', '%s')"
self.cursor.execute(sql, (username, income, expense, description, category))
def get_balance(self, username):
sql = "SELECT `balance` FROM `balance_users` WHERE `user` = %s"
self.cursor.execute(sql, (username))
result = self.cursor.fetchall()
result = result[0][0]
return result
def update_balance(self, username, income, expense):
sql = "SELECT `balance`, `total_income`, `total_expense` FROM `balance_users` WHERE `user` = %s"
self.cursor.execute(sql, (username,))
result = self.cursor.fetchall()
balance = float(result[0][0])
total_income = float(result[0][1])
total_expense = float(result[0][2])
print("Updating balance")
result = float(balance) + income - expense
total_income += income
total_expense += expense
print("Balance: %s" % result, "Income: %s" % income, "Expense: %s" % expense)
sql = "UPDATE `balance_users` SET `balance`=%s,`total_income`=%s,`total_expense`=%s WHERE user = %s"
self.cursor.execute(sql, (result, total_income, total_expense, username))
return result
def get_mensual_balance(self, username):
sql = "SELECT `income`, `expense`, date FROM `%s`"
self.cursor.execute(sql, (username,))
result = self.cursor.fetchall()
incomeAp = []
date = []
subT = 0
lastDate = ""
for long in result:
dateSplit = str(long[2]).split(" ")
if lastDate == "":
lastDate = dateSplit[0]
subT += float(long[0]) - float(long[1])
elif lastDate == dateSplit[0]:
subT += float(long[0]) - float(long[1])
elif lastDate != dateSplit[0]:
incomeAp.append(subT)
date.append(lastDate)
lastDate = dateSplit[0]
subT += float(long[0]) - float(long[1])
incomeAp.append(subT)
date.append(lastDate)
balance = [incomeAp, date]
#Get the total income
sql = "SELECT `income`, date FROM `%s` WHERE expense = '0'"
self.cursor.execute(sql, (username,))
result = self.cursor.fetchall()
incomeT = []
dateIncome = []
subT = 0
lastDate = ""
for long in result:
dateSplit = str(long[1]).split(" ")
if lastDate == "":
lastDate = dateSplit[0]
subT += float(long[0])
elif lastDate == dateSplit[0]:
subT += float(long[0])
elif lastDate != dateSplit[0]:
incomeT.append(subT)
dateIncome.append(lastDate)
subT = 0
lastDate = dateSplit[0]
subT += float(long[0])
incomeT.append(subT)
dateIncome.append(lastDate)
income = [incomeT, dateIncome]
#Get the total expense
sql = "SELECT `expense`, date FROM `%s` WHERE income = '0'"
self.cursor.execute(sql, (username,))
result = self.cursor.fetchall()
expenseT = []
dateExpense = []
subT = 0
lastDate = ""
for long in result:
dateSplit = str(long[1]).split(" ")
if lastDate == "":
lastDate = dateSplit[0]
subT += float(long[0])
elif lastDate == dateSplit[0]:
subT += float(long[0])
elif lastDate != dateSplit[0]:
expenseT.append(subT)
dateExpense.append(lastDate)
subT = 0
lastDate = dateSplit[0]
subT += float(long[0])
expenseT.append(subT)
dateExpense.append(lastDate)
expense = [expenseT, dateExpense]
return balance, income, expense
def deleteUser(self, username):
sql = "DROP TABLE `%s`"
self.cursor.execute(sql, (username,))
sql = "DELETE FROM `balance_users` WHERE `user` = '%s'"
self.cursor.execute(sql, (username,))
sql = "DELETE FROM `currency` WHERE `id` = '%s'"
self.cursor.execute(sql, (username,))
print("User deleted")
def checkCurrency(self, username):
sql = "SELECT `currency` FROM `currency` WHERE `id` = '%s'"
self.cursor.execute(sql, (username,))
result = self.cursor.fetchall()
return result[0][0]
def commit(self):
self.connection.commit()
return
def close(self):
self.connection.close()
return
def rollback(self):
self.connection.rollback()
return