From 53a0ca7fcf3c389f6e6aedc414aa2c4e74ec7e8f Mon Sep 17 00:00:00 2001 From: asleal2 Date: Tue, 17 Apr 2018 10:17:17 -0500 Subject: [PATCH] adding database functionality --- __pycache__/mocha_server.cpython-36.pyc | Bin 0 -> 2126 bytes mocha.db | Bin 8192 -> 8192 bytes mocha_server.py | 31 +++++++++++++++++++----- test.py | 5 ++++ 4 files changed, 30 insertions(+), 6 deletions(-) create mode 100644 __pycache__/mocha_server.cpython-36.pyc create mode 100644 test.py diff --git a/__pycache__/mocha_server.cpython-36.pyc b/__pycache__/mocha_server.cpython-36.pyc new file mode 100644 index 0000000000000000000000000000000000000000..1091d27bd2dc73960be4c144908a84fe04441576 GIT binary patch literal 2126 zcmZuy-EQ1O6!whmUB}sk(3YPfsH{kpvJuHfv=RuVDQUV@TgnzTDM&`eay_$&H(rlB zGj52rd&9y5@EAM{w_Ny^tG+_6IA=CVLt0C7X3qScpYMFWwXon%7XJwy&$9ltE_@Wm z-#}BpfM8Z=F~Vp@LSlLvQf7xXr$dL^Qxdu`YH&Ada(C!Wt9l7o1iy-3hV{IDm`cAzOAgtn$I!7R9(^xC9$3z_D+P3vZQIZKV;)?CjjyGq!_dCF%oOt`QHULnV599tT9rIb!aCGPZ_*nJMrLv;Uyi0U3~e z=eot*L#mcF`He7pLSYWp3@WxXYR&6_8JI^V@v6T~u4Zfk39#v$|7C z$#b0@2dXRzA+`G2%p8CT!nZic#nc|jFCNeAggnBms9u3E*} zH{ThA%~fxq&{%XrIp$vM3%56J2R)&K2cpbbZB_2G zy$5SwowRm<72u9FeO&O9rMQ52B{4>-gIKQ?&(@#td|w{`)ISBM;p?R(q1-LQ##4{T_v zH?;fWD4YlChz4<@g*={BJ^qmlgBze2FU{ diff --git a/mocha_server.py b/mocha_server.py index 37363f6..33af5cd 100755 --- a/mocha_server.py +++ b/mocha_server.py @@ -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") diff --git a/test.py b/test.py new file mode 100644 index 0000000..2950d04 --- /dev/null +++ b/test.py @@ -0,0 +1,5 @@ +import mocha_server + +#mocha_server.insert_row(4, 'andrew') + +print(mocha_server.fetch_user(3))