adding database functionality
This commit is contained in:
parent
b173b1e007
commit
53a0ca7fcf
4 changed files with 30 additions and 6 deletions
BIN
__pycache__/mocha_server.cpython-36.pyc
Normal file
BIN
__pycache__/mocha_server.cpython-36.pyc
Normal file
Binary file not shown.
BIN
mocha.db
BIN
mocha.db
Binary file not shown.
|
@ -6,24 +6,43 @@ import json
|
|||
import sqlite3
|
||||
|
||||
|
||||
DATABASE = '/usr/local/www/mocha-server/mocha.db'
|
||||
# DATABASE = '/usr/local/www/mocha-server/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 top N users by score
|
||||
# TODO: Add ability to store and retrieve avatars (as image files?)
|
||||
|
||||
# add new parameters as needed
|
||||
def update_row(user_id, updated_username):
|
||||
print()
|
||||
|
||||
def insert_row(user_id, username):
|
||||
"""
|
||||
Inserts a row for a NEW user with given parameters
|
||||
This may work better with AUTOINCREMENT to avoid arbitrary ids and duplicates
|
||||
"""
|
||||
|
||||
item = (user_id, username)
|
||||
cursor.execute('insert into users values (?,?)', item)
|
||||
conn.commit()
|
||||
conn.close()
|
||||
|
||||
|
||||
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)
|
||||
conn.row_factory = sqlite3.Row
|
||||
cursor = conn.cursor()
|
||||
|
||||
if user_id != '*':
|
||||
cursor.execute("SELECT * FROM users WHERE user_id=?", user_id)
|
||||
if user_id != '*': # must use (?), (item,) format
|
||||
cursor.execute("SELECT * FROM users WHERE user_id=(?)", (user_id,))
|
||||
else:
|
||||
cursor.execute("SELECT * FROM users")
|
||||
|
||||
|
|
5
test.py
Normal file
5
test.py
Normal file
|
@ -0,0 +1,5 @@
|
|||
import mocha_server
|
||||
|
||||
#mocha_server.insert_row(4, 'andrew')
|
||||
|
||||
print(mocha_server.fetch_user(3))
|
Reference in a new issue