From cc17637c993d2d28d272cb5f7a5d69db72fc3db3 Mon Sep 17 00:00:00 2001 From: Corder Guy Date: Tue, 17 Apr 2018 14:38:59 -0500 Subject: [PATCH] Close cursor each time --- mocha_server.py | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/mocha_server.py b/mocha_server.py index 21a2c3a..e4bbcd1 100755 --- a/mocha_server.py +++ b/mocha_server.py @@ -6,17 +6,10 @@ import json import sqlite3 from os import path -# NOTE: closing a database makes the next query in a script not work - DATABASE = '/usr/local/www/mocha-server/mocha.db' if not path.exists(DATABASE): DATABASE = 'mocha.db' -conn = sqlite3.connect(DATABASE) -conn.row_factory = sqlite3.Row -cursor = conn.cursor() - - # TODO: Add fetching of list of users # TODO: Add fetching of top N users by score # TODO: Add ability to store and retrieve avatars (as image files?) @@ -26,6 +19,8 @@ def get_users(username_list): Gets a list of users searching by name. This can also easily be done by user_id. """ + conn = sqlite3.connect(DATABASE) + cursor = conn.cursor() output = [] for usr in username_list: cursor.execute("select * from users where username=(?)", (usr,)) @@ -33,7 +28,7 @@ def get_users(username_list): output = json.dumps([dict(row) for row in output]) conn.commit() - #conn.close() + conn.close() if output == '[]': output = None @@ -48,6 +43,8 @@ def get_top_N(search_parameter, desc, N): Store an orderd id list and put that in the json? """ + conn = sqlite3.connect(DATABASE) + cursor = conn.cursor() if desc == True: cursor.execute("select * from users order by ? desc limit ?", (search_parameter, N)) else: @@ -58,7 +55,7 @@ def get_top_N(search_parameter, desc, N): output = json.dumps([dict(row) for row in output]) conn.commit() - #conn.close() + conn.close() if output == '[]': output = None @@ -77,9 +74,11 @@ def insert_row(user_id, username): This may work better with AUTOINCREMENT to avoid arbitrary ids and duplicates """ + conn = sqlite3.connect(DATABASE) + cursor = conn.cursor() cursor.execute("insert into users values (?,?)", (user_id, username)) conn.commit() - #conn.close() + conn.close() def fetch_user(user_id): @@ -87,7 +86,8 @@ def fetch_user(user_id): Returns a JSON object containing the requested user Also can return a list of all users if user_id == "*" """ - + conn = sqlite3.connect(DATABASE) + cursor = conn.cursor() if user_id != '*': # must use (?), (item,) format cursor.execute("SELECT * FROM users WHERE user_id=(?)", (user_id,)) else: @@ -96,7 +96,7 @@ def fetch_user(user_id): output = cursor.fetchall() output = json.dumps([dict(row) for row in output]) conn.commit() - #conn.close() + conn.close() if output == '[]': output = None