-
Notifications
You must be signed in to change notification settings - Fork 0
/
database.py
54 lines (36 loc) · 1.34 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
# https://docs.python.org/3/library/sqlite3.html
import sqlite3
import converters
class Database:
con = sqlite3.connect(':memory:')
def __init__(self):
con = self.con
c = con.cursor()
c.execute("CREATE TABLE operacao (id INTEGER PRIMARY KEY AUTOINCREMENT, data TEXT, cv TEXT, qtd TEXT, ticker TEXT, preco TEXT, nota TEXT)")
con.commit()
def addOperacao(self, operacao):
con = self.con
c = con.cursor()
p = operacao.data, operacao.cv, operacao.qtd, operacao.ticker, operacao.preco, operacao.nota
c.execute("INSERT INTO operacao (data, cv, qtd, ticker, preco, nota) VALUES (?, ?, ?, ?, ?, ?)", p)
con.commit()
def listarOperacoes(self) -> list:
con = self.con
c = con.cursor()
c.execute("SELECT * FROM operacao")
rows = c.fetchall()
i = 0
for r in rows:
rows[i] = converters.toOperacao(r)
i+=1
return rows
def listarOperacoesPorTicker(self, ticker) -> list:
con = self.con
c = con.cursor()
c.execute("SELECT * FROM operacao WHERE ticker = ?", (ticker,))
rows = c.fetchall()
i = 0
for r in rows:
rows[i] = converters.toOperacao(r)
i+=1
return rows