-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathquery.py
More file actions
64 lines (51 loc) · 1.59 KB
/
query.py
File metadata and controls
64 lines (51 loc) · 1.59 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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#import psychopg2
from sqlalchemy import create_engine, select, update, insert
from sqlalchemy.ext.automap import automap_base
from dotenv import load_dotenv
import os
from base64 import b64decode, b64encode
load_dotenv()
engine = create_engine(os.getenv('DATABASE_URL'))
"""def query_one(query):
curs = engine.connect()
response = curs.execute(query).fetchone()
curs.close()
return response
def query_all(query):
curs = engine.connect()
response = curs.execute(query).fetchall()
curs.close()
return response"""
def get_image(image_id):
Base = automap_base()
Base.prepare(engine, reflect=True)
pics = Base.classes.pics
query = select([pics.pic]).where(pics.id == image_id)
curs = engine.connect()
response = curs.execute(query).fetchone()[0]
image = b64decode(response)
curs.close()
return response
def put_image(image_id, processed_image, atts):
Base = automap_base()
Base.prepare(engine, reflect=True)
pics = Base.classes.pics
attributes = Base.classes.attributes
curs = engine.connect()
updt = update(pics).where(pics.id==image_id).values(processed_pic=processed_image)
curs.execute(updt)
for key, value in atts.items():
ins = insert(attributes).values(
pic_id=image_id,
attribute=key,
count=value)
curs.execute(ins)
curs.close()
"""def produce_test_image(image_path):
with open(image_path, 'rb') as file:
text = file.read()
return text"""
"""def query(query):
conn = engine.connect()
conn.execute(query)
conn.close()"""