Close cursor each time
This commit is contained in:
parent
1df6899472
commit
cc17637c99
1 changed files with 12 additions and 12 deletions
|
@ -6,17 +6,10 @@ import json
|
||||||
import sqlite3
|
import sqlite3
|
||||||
from os import path
|
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'
|
DATABASE = '/usr/local/www/mocha-server/mocha.db'
|
||||||
if not path.exists(DATABASE):
|
if not path.exists(DATABASE):
|
||||||
DATABASE = 'mocha.db'
|
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 list of users
|
||||||
# TODO: Add fetching of top N users by score
|
# TODO: Add fetching of top N users by score
|
||||||
# TODO: Add ability to store and retrieve avatars (as image files?)
|
# 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.
|
Gets a list of users searching by name.
|
||||||
This can also easily be done by user_id.
|
This can also easily be done by user_id.
|
||||||
"""
|
"""
|
||||||
|
conn = sqlite3.connect(DATABASE)
|
||||||
|
cursor = conn.cursor()
|
||||||
output = []
|
output = []
|
||||||
for usr in username_list:
|
for usr in username_list:
|
||||||
cursor.execute("select * from users where username=(?)", (usr,))
|
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])
|
output = json.dumps([dict(row) for row in output])
|
||||||
conn.commit()
|
conn.commit()
|
||||||
#conn.close()
|
conn.close()
|
||||||
|
|
||||||
if output == '[]':
|
if output == '[]':
|
||||||
output = None
|
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?
|
Store an orderd id list and put that in the json?
|
||||||
"""
|
"""
|
||||||
|
conn = sqlite3.connect(DATABASE)
|
||||||
|
cursor = conn.cursor()
|
||||||
if desc == True:
|
if desc == True:
|
||||||
cursor.execute("select * from users order by ? desc limit ?", (search_parameter, N))
|
cursor.execute("select * from users order by ? desc limit ?", (search_parameter, N))
|
||||||
else:
|
else:
|
||||||
|
@ -58,7 +55,7 @@ def get_top_N(search_parameter, desc, N):
|
||||||
|
|
||||||
output = json.dumps([dict(row) for row in output])
|
output = json.dumps([dict(row) for row in output])
|
||||||
conn.commit()
|
conn.commit()
|
||||||
#conn.close()
|
conn.close()
|
||||||
|
|
||||||
if output == '[]':
|
if output == '[]':
|
||||||
output = None
|
output = None
|
||||||
|
@ -77,9 +74,11 @@ def insert_row(user_id, username):
|
||||||
This may work better with AUTOINCREMENT to avoid arbitrary ids and duplicates
|
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))
|
cursor.execute("insert into users values (?,?)", (user_id, username))
|
||||||
conn.commit()
|
conn.commit()
|
||||||
#conn.close()
|
conn.close()
|
||||||
|
|
||||||
|
|
||||||
def fetch_user(user_id):
|
def fetch_user(user_id):
|
||||||
|
@ -87,7 +86,8 @@ def fetch_user(user_id):
|
||||||
Returns a JSON object containing the requested user
|
Returns a JSON object containing the requested user
|
||||||
Also can return a list of all users if user_id == "*"
|
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
|
if user_id != '*': # must use (?), (item,) format
|
||||||
cursor.execute("SELECT * FROM users WHERE user_id=(?)", (user_id,))
|
cursor.execute("SELECT * FROM users WHERE user_id=(?)", (user_id,))
|
||||||
else:
|
else:
|
||||||
|
@ -96,7 +96,7 @@ def fetch_user(user_id):
|
||||||
output = cursor.fetchall()
|
output = cursor.fetchall()
|
||||||
output = json.dumps([dict(row) for row in output])
|
output = json.dumps([dict(row) for row in output])
|
||||||
conn.commit()
|
conn.commit()
|
||||||
#conn.close()
|
conn.close()
|
||||||
|
|
||||||
if output == '[]':
|
if output == '[]':
|
||||||
output = None
|
output = None
|
||||||
|
|
Reference in a new issue