-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdb.py
More file actions
70 lines (50 loc) · 1.59 KB
/
db.py
File metadata and controls
70 lines (50 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
65
66
67
68
69
70
import sqlite3
import pandas
import click
from flask import current_app, g
from flask.cli import with_appcontext
def get_db():
"""
Returns current database connection. If connection not present,
initiates connection to configured database.
"""
if 'db' not in g:
try:
g.db = sqlite3.connect(
current_app.config['DATABASE'],
detect_types=sqlite3.PARSE_DECLTYPES,
)
except:
g.db = sqlite3.connect(
current_app.config['LOCALDATABASE'],
detect_types=sqlite3.PARSE_DECLTYPES,
)
g.db.row_factory = sqlite3.Row
return g.db
def close_db(e=None):
db = g.pop('db', None)
if db is not None:
db.close()
def init_app(app):
app.teardown_appcontext(close_db)
def query_database(lst):
import os
conn = get_db()
c = conn.cursor()
desc = []
for i in lst:
rows = c.execute("""SELECT * FROM Strain_info as S
JOIN Cannabinoids as C ON C.StrainID=S.StrainID
JOIN Terpenes as T ON T.StrainID=S.StrainID
WHERE S.StrainID=""" + str(i)).fetchall()
desc.append(rows)
names = [description[0] for description in c.description]
format_desc = {}
for item_number, row in enumerate(desc):
temp_dic = {}
for count,name in enumerate(names):
temp_dic[name] = row[0][count]
#print(temp_dic[name])
format_desc[item_number] = temp_dic
#print(names)
return format_desc