-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsql_connector.py
More file actions
42 lines (30 loc) · 1.16 KB
/
sql_connector.py
File metadata and controls
42 lines (30 loc) · 1.16 KB
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
from sqlalchemy import *
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker, relationship
db = create_engine('mysql://3010user:sysc3010@localhost/BLAB_db');
base = declarative_base(bind=db)
# Declarative class for mapping of SQL table to python Object class
class Item_Lookup(base):
__tablename__ = 'food_item_lookup'
tag_hashcode = Column(String(64), primary_key = True)
item_name = Column(String(40))
expiry = Column(Integer)
def __repr(self):
return "<item_lookup(tag_hashcode='%s', item_name='%s', expiry='%i')>" % (self.tag_hashcode, self.item_name, self.expiry)
# Database Handler Class Construction
# Instantiates a Session and handles SQL queries
class db_hand:
def __init__(self):
Session = sessionmaker(bind=db)
self.session = Session()
def get_item(self, hash):
for row in self.session.query(Item_Lookup).filter(Item_Lookup.tag_hashcode==hash):
return (row.item_name, row.expiry)
def put_item(self, hash, name, expiration):
item = Item_Lookup(tag_hashcode=hash, item_name=name, expiry=expiration)
self.session.add(item)
self.session.commit()
return True
#def main():
# hand = db_hand()
#main()