-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatabase.py
48 lines (41 loc) · 1.65 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
import psycopg2
class DB:
def __init__(self):
self.conn = psycopg2.connect(database="geocode", user="postgres", password="$PMadr!d1234", host="localhost", port="5432")
self.cur = self.conn.cursor()
def create(self):
try:
self.cur.execute("CREATE TABLE IF NOT EXISTS model (id serial PRIMARY KEY, area_muni VARCHAR(100), address VARCHAR(500));")
self.conn.commit()
return True
except psycopg2.Error as e:
print("Error creating table:", e)
return False
def insert(self, df):
try:
area_munis, addresses = self.select()
count = 0
for _, row in df.iterrows():
print(row)
if (row['area-muni'] not in area_munis and row['address'] not in addresses):
query = "INSERT INTO model (area_muni, address) VALUES (%s, %s) ON CONFLICT DO NOTHING"
self.cur.execute(query, (row['area-muni'], row['address']))
count += 1
self.conn.commit()
return count
except psycopg2.Error as e:
print("Error inserting data:", e)
return -1
def select(self):
try:
self.cur.execute("SELECT area_muni, address FROM model")
rows = self.cur.fetchall()
area_munis = [row[0] for row in rows]
addresses = [row[1] for row in rows]
return area_munis, addresses
except psycopg2.Error as e:
print("Error selecting data:", e)
return None, None
def close(self):
self.cur.close()
self.conn.close()