sql row factory

This commit is contained in:
Emily 2019-03-11 07:51:05 +01:00
parent fa152f4d46
commit e3eaba33ed
5 changed files with 20 additions and 5 deletions

View File

@ -31,11 +31,12 @@ class handler(asyncTornado.asyncRequestHandler):
raise Exception("Missing required arguments")
method = args_filter[0]
method_value = self.request.arguments[method]
method_value = self.get_argument(method)
cur = glob.sql.cursor()
conn = glob.new_sql()
cur = conn.cursor()
sql = SQL_STRUCT["main"] % SQL_STRUCT["method"]
sql = SQL_STRUCT["main"] % SQL_STRUCT[method]
if method == "timestamp":
sql = sql % method_value
else:
@ -45,12 +46,14 @@ class handler(asyncTornado.asyncRequestHandler):
data = cur.fetchone()
cur.close()
conn.close()
status_code = 200
except Exception as e:
status_code = 400
data["status"] = status_code
data["message"] = str(e)
print("Err: %s" % e)
finally:
self.write( json.dumps(data) )
self.set_header("Content-Type", "application/json")

0
helpers/__init__.py Normal file
View File

View File

@ -0,0 +1,5 @@
def dict_factory(cursor, row):
d = {}
for idx, col in enumerate(cursor.description):
d[col[0]] = row[idx]
return d

View File

@ -53,7 +53,7 @@ def build_database():
f = open("wayback.db", "w")
f.close()
glob.sql = sqlite3.connect("wayback.db")
glob.sql = glob.new_sql()
cur = glob.sql.cursor()
with open("database_structs.sql", "r") as f:
@ -69,7 +69,7 @@ if __name__ == "__main__":
build_database()
if glob.sql == None:
glob.sql = sqlite3.connect("wayback.db")
glob.sql = glob.new_sql()
print("Serving at %s:%s" % (glob.config["server"]["host"], glob.config["server"]["port"]))
print("To stop server press CTRL + C")

View File

@ -1,5 +1,7 @@
import json
import sqlite3
from multiprocessing.pool import ThreadPool
from helpers import databaseHelper
with open("config.json", "r") as f:
config = json.load(f)
@ -7,3 +9,8 @@ with open("config.json", "r") as f:
app = None
sql = None
pool = ThreadPool(config["threads"])
def new_sql():
db = sqlite3.connect("wayback.db")
db.row_factory = databaseHelper.dict_factory
return db