forked from SuperTony0/apartment-finder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
database.py
52 lines (46 loc) · 1.67 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
from sqlalchemy import create_engine
from sqlalchemy import Table, Column, Integer, String, MetaData, Boolean
from sqlalchemy.sql import select
class Database:
def __init__(self):
engine = create_engine('sqlite:///apt_results.db')#, echo=True)
metadata = MetaData()
self.apt_results = Table('apt_results', metadata,
Column('id', String, primary_key=True),
Column('name', String),
Column('url', String),
Column('datetime', String),
Column('price', String),
Column('where', String),
Column('has_image', Boolean),
Column('has_map', Boolean),
Column('geotag', String)
)
metadata.create_all(engine)
self.conn = engine.connect()
def add(self, listing):
# add to db
ins = self.apt_results.insert().values(
id=listing['id'],
name=listing['name'],
url=listing['url'],
datetime=listing['datetime'],
price=listing['price'],
where=listing['where'],
has_image=listing['has_image'],
has_map=listing['has_map'],
geotag=str(listing['geotag'])
)
self.conn.execute(ins)
def get(self, listing_id):
# get from db by id
sel = select([self.apt_results]).where(self.apt_results.c.id == listing_id)
rows = self.conn.execute(sel)
if not rows:
return None
return rows.fetchone() # there should only ever be one anyway
def contains(self, listing_id):
# return true if id exists in db
if self.get(listing_id):
return True
return False