-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDatabaseInstance.py
46 lines (36 loc) · 1.4 KB
/
DatabaseInstance.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
import sqlite3
from flask import Flask, request, jsonify, g, Response
app = Flask(__name__)
USER_DATABASE = './DataBase/users.db'
COMMENTS_DATABASE = './DataBase/comments.db'
TAGS_DATABASE = './DataBase/tags.db'
ARTICLE_DATABASE = './DataBase/article.db'
def get_userdb():
db = getattr(g, '_database', None)
if db is None:
db = g._database = sqlite3.connect(USER_DATABASE) #create a database instance and use it for later execution
print("database instance is created")
return db
def get_commentsdb():
db = getattr(g, '_database', None)
if db is None:
db = g._database = sqlite3.connect(COMMENTS_DATABASE) #create a database instance and use it for later execution
print("database instance is created")
return db
def get_tagsdb():
db = getattr(g, '_database', None)
if db is None:
db = g._database = sqlite3.connect(TAGS_DATABASE) #create a database instance and use it for later execution
print("database instance is created")
return db
def get_articledb():
db = getattr(g, '_database', None)
if db is None:
db = g._database = sqlite3.connect(ARTICLE_DATABASE) #create a database instance and use it for later execution
print("database instance is created")
return db
@app.teardown_appcontext
def close_connection(exception):
db = getattr(g, '_database', None)
if db is not None:
db.close()