-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbd_req.py
186 lines (138 loc) · 4.93 KB
/
bd_req.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 psycopg2
def connectDB() :
try:
conn = psycopg2.connect(
host="localhost",
database="pea_pescarte_cotacoespeixes",
user="postgres",
password="21452123888")
return conn
except (Exception, psycopg2.DatabaseError) as error:
print(error)
# # finally:
# if conn is not None:
# conn.close()
# print('Database connection closed.')
def isNumeric(txt) :
for i in range(len(txt)) :
if not (txt[i].isdigit() or txt[i] == ',') :
return False
return True
def insereDataBoletimNoBD (conn, data_boletim, link) :
try :
# create a cursor
# cur = conn.cursor()
# execute a statement
print('PostgreSQL database - insert into cotacoes:' )
sql = "SET datestyle to DMY, SQL; "
sql = sql + "INSERT INTO cotacoes VALUES ('" + data_boletim + "', '" + link + "') ON CONFLICT (data) DO NOTHING;"
f = open("/Users/sahudymontenegro/Downloads/cotacoes22102021.csv", "a")
f.write(data_boletim + ", " + link + "\n")
f.close()
# cur.execute(sql)
# conn.commit()
# close the communication with the PostgreSQL
# cur.close()
except (Exception ,psycopg2.DatabaseError) as error:
print(error)
pass
# finally:
# if conn is not None:
# conn.close()
# print('Database connection closed.')
def formatNumber(n) :
return n.replace(',','.') # cast: int()
# def formatCotacoes(precos) :
# campos = ''
# valores = ''
# for i, p in list(enumerate(precos)) :
# if i == 0 and isNumeric(p) :
# campos += ',minimo'
# valores += ', ' + formatNumber(p)
# if i == 1 and isNumeric(p) :
# campos += ',mais_comum'
# valores += ', ' + formatNumber(p)
# if i == 2 and isNumeric(p) :
# campos += ',maximo'
# valores += ', ' + formatNumber(p)
# print(campos)
# return campos, valores
def formatCotacoes(precos) :
valores = ''
for i, p in list(enumerate(precos)) :
if i == 0 and isNumeric(p) :
valores += ',' + formatNumber(p)
elif i== 0 :
valores += ','
if i == 1 and isNumeric(p) :
valores += ',' + formatNumber(p)
elif i == 1 :
valores += ','
if i == 2 and isNumeric(p) :
valores += ',' + formatNumber(p)
elif i == 2 :
valores += ','
for i in range(3 - valores.count(',')) :
valores += ','
return valores
def insereCotacoesNoBD(conn,f, cod_pescado,data_boletim,precos) :
try:
valores = formatCotacoes(precos)
# create a cursor
# cur = conn.cursor()
# execute a statement
print('PostgreSQL database - insert into cotacoes_pescados:' )
# sql = "SET datestyle to DMY, SQL; "
sql = "INSERT INTO cotacoes_pescados (cod_pescado, data "
# sql += campos
sql += ") VALUES ('" + cod_pescado + "', '"
sql += data_boletim + "'"
sql += valores + ") ON CONFLICT (cod_pescado,data) DO NOTHING;"
print(sql)
# cur.execute(sql)
# conn.commit()
# close the communication with the PostgreSQL
# cur.close()
f.write(cod_pescado + ", " + data_boletim + valores + "\n")
# print("eh aqui? end")
# return f
except (Exception, psycopg2.DatabaseError) as error:
print(error)
return -1
def inserePescadoNoBD(conn, cod_pescado,descricao) :
try :
# create a cursor
# cur = conn.cursor()
# execute a statement
print('PostgreSQL database - insert into pescados:' )
sql = "INSERT INTO pescados VALUES ('" + cod_pescado + "', '" + descricao + "')"
print(sql)
# cur.execute(sql)
# conn.commit()
# close the communication with the PostgreSQL
# cur.close()
f = open("/Users/sahudymontenegro/Downloads/pescados22102021.csv", "a")
f.write(cod_pescado + ", " + descricao + "\n")
f.close()
except (Exception, psycopg2.DatabaseError) as error:
print(error)
# pass
def buscaPescadoNoCampoBD(conn, cod, desc) :
try :
# create a cursor
cur = conn.cursor()
# execute a statement
print('PostgreSQL database - tabela pescados:' ) # + 'SELECT ' + campo + ' FROM pescados')
sql = "SELECT cod_pescado FROM pescados WHERE cod_pescado = '"
sql += cod + "' OR descricao = '"
sql += desc + "'"
print(sql)
cur.execute(sql) #+ campo +
# display the PostgreSQL database server version
pescado = cur.fetchone()[0]
print(pescado)
# close the communication with the PostgreSQL
cur.close()
return pescado
except (Exception, psycopg2.DatabaseError) as error:
print(error)