Prettify sql statement (#5)
This commit is contained in:
committed by
GitHub
parent
793c1fa130
commit
b5b308380c
38
database.py
38
database.py
@@ -10,7 +10,7 @@ def list_users():
|
|||||||
_conn = sqlite3.connect(user_db_file_location)
|
_conn = sqlite3.connect(user_db_file_location)
|
||||||
_c = _conn.cursor()
|
_c = _conn.cursor()
|
||||||
|
|
||||||
_c.execute("select id from users;")
|
_c.execute("SELECT id FROM users;")
|
||||||
result = [x[0] for x in _c.fetchall()]
|
result = [x[0] for x in _c.fetchall()]
|
||||||
|
|
||||||
_conn.close()
|
_conn.close()
|
||||||
@@ -21,7 +21,7 @@ def verify(id, pw):
|
|||||||
_conn = sqlite3.connect(user_db_file_location)
|
_conn = sqlite3.connect(user_db_file_location)
|
||||||
_c = _conn.cursor()
|
_c = _conn.cursor()
|
||||||
|
|
||||||
_c.execute("select pw from users where id = '" + id + "';")
|
_c.execute("SELECT pw FROM users WHERE id = '" + id + "';")
|
||||||
result = _c.fetchone()[0] == hashlib.sha256(pw.encode()).hexdigest()
|
result = _c.fetchone()[0] == hashlib.sha256(pw.encode()).hexdigest()
|
||||||
|
|
||||||
_conn.close()
|
_conn.close()
|
||||||
@@ -31,23 +31,23 @@ def verify(id, pw):
|
|||||||
def delete_user_from_db(id):
|
def delete_user_from_db(id):
|
||||||
_conn = sqlite3.connect(user_db_file_location)
|
_conn = sqlite3.connect(user_db_file_location)
|
||||||
_c = _conn.cursor()
|
_c = _conn.cursor()
|
||||||
_c.execute("delete from users where id = '" + id + "';")
|
_c.execute("DELETE FROM users WHERE id = '" + id + "';")
|
||||||
_conn.commit()
|
_conn.commit()
|
||||||
_conn.close()
|
_conn.close()
|
||||||
|
|
||||||
# when we delete a user from database USERS, we also need to delete all his or her notes data from database NOTES
|
# when we delete a user FROM database USERS, we also need to delete all his or her notes data FROM database NOTES
|
||||||
_conn = sqlite3.connect(note_db_file_location)
|
_conn = sqlite3.connect(note_db_file_location)
|
||||||
_c = _conn.cursor()
|
_c = _conn.cursor()
|
||||||
_c.execute("delete from notes where user = '" + id + "';")
|
_c.execute("DELETE FROM notes WHERE user = '" + id + "';")
|
||||||
_conn.commit()
|
_conn.commit()
|
||||||
_conn.close()
|
_conn.close()
|
||||||
|
|
||||||
# when we delete a user from database USERS, we also need to
|
# when we delete a user FROM database USERS, we also need to
|
||||||
# [1] delete all his or her images from image pool (done in app.py)
|
# [1] delete all his or her images FROM image pool (done in app.py)
|
||||||
# [2] delete all his or her images records from database IMAGES
|
# [2] delete all his or her images records FROM database IMAGES
|
||||||
_conn = sqlite3.connect(image_db_file_location)
|
_conn = sqlite3.connect(image_db_file_location)
|
||||||
_c = _conn.cursor()
|
_c = _conn.cursor()
|
||||||
_c.execute("delete from images where owner = '" + id + "';")
|
_c.execute("DELETE FROM images WHERE owner = '" + id + "';")
|
||||||
_conn.commit()
|
_conn.commit()
|
||||||
_conn.close()
|
_conn.close()
|
||||||
|
|
||||||
@@ -55,7 +55,7 @@ def add_user(id, pw):
|
|||||||
_conn = sqlite3.connect(user_db_file_location)
|
_conn = sqlite3.connect(user_db_file_location)
|
||||||
_c = _conn.cursor()
|
_c = _conn.cursor()
|
||||||
|
|
||||||
_c.execute("insert into users values(?, ?)", (id.upper(), hashlib.sha256(pw.encode()).hexdigest()))
|
_c.execute("INSERT INTO users values(?, ?)", (id.upper(), hashlib.sha256(pw.encode()).hexdigest()))
|
||||||
|
|
||||||
_conn.commit()
|
_conn.commit()
|
||||||
_conn.close()
|
_conn.close()
|
||||||
@@ -64,7 +64,7 @@ def read_note_from_db(id):
|
|||||||
_conn = sqlite3.connect(note_db_file_location)
|
_conn = sqlite3.connect(note_db_file_location)
|
||||||
_c = _conn.cursor()
|
_c = _conn.cursor()
|
||||||
|
|
||||||
command = "select note_id, timestamp, note from notes where user = '" + id.upper() + "';"
|
command = "SELECT note_id, timestamp, note FROM notes WHERE user = '" + id.upper() + "';"
|
||||||
_c.execute(command)
|
_c.execute(command)
|
||||||
result = _c.fetchall()
|
result = _c.fetchall()
|
||||||
|
|
||||||
@@ -78,7 +78,7 @@ def match_user_id_with_note_id(note_id):
|
|||||||
_conn = sqlite3.connect(note_db_file_location)
|
_conn = sqlite3.connect(note_db_file_location)
|
||||||
_c = _conn.cursor()
|
_c = _conn.cursor()
|
||||||
|
|
||||||
command = "select user from notes where note_id = '" + note_id + "';"
|
command = "SELECT user FROM notes WHERE note_id = '" + note_id + "';"
|
||||||
_c.execute(command)
|
_c.execute(command)
|
||||||
result = _c.fetchone()[0]
|
result = _c.fetchone()[0]
|
||||||
|
|
||||||
@@ -92,7 +92,7 @@ def write_note_into_db(id, note_to_write):
|
|||||||
_c = _conn.cursor()
|
_c = _conn.cursor()
|
||||||
|
|
||||||
current_timestamp = str(datetime.datetime.now())
|
current_timestamp = str(datetime.datetime.now())
|
||||||
_c.execute("insert into notes values(?, ?, ?, ?)", (id.upper(), current_timestamp, note_to_write, hashlib.sha1((id.upper() + current_timestamp).encode()).hexdigest()))
|
_c.execute("INSERT INTO notes values(?, ?, ?, ?)", (id.upper(), current_timestamp, note_to_write, hashlib.sha1((id.upper() + current_timestamp).encode()).hexdigest()))
|
||||||
|
|
||||||
_conn.commit()
|
_conn.commit()
|
||||||
_conn.close()
|
_conn.close()
|
||||||
@@ -101,7 +101,7 @@ def delete_note_from_db(note_id):
|
|||||||
_conn = sqlite3.connect(note_db_file_location)
|
_conn = sqlite3.connect(note_db_file_location)
|
||||||
_c = _conn.cursor()
|
_c = _conn.cursor()
|
||||||
|
|
||||||
command = "delete from notes where note_id = '" + note_id + "';"
|
command = "DELETE FROM notes WHERE note_id = '" + note_id + "';"
|
||||||
_c.execute(command)
|
_c.execute(command)
|
||||||
|
|
||||||
_conn.commit()
|
_conn.commit()
|
||||||
@@ -111,7 +111,7 @@ def image_upload_record(uid, owner, image_name, timestamp):
|
|||||||
_conn = sqlite3.connect(image_db_file_location)
|
_conn = sqlite3.connect(image_db_file_location)
|
||||||
_c = _conn.cursor()
|
_c = _conn.cursor()
|
||||||
|
|
||||||
_c.execute("insert into images values (?, ?, ?, ?)", (uid, owner, image_name, timestamp))
|
_c.execute("INSERT INTO images VALUES (?, ?, ?, ?)", (uid, owner, image_name, timestamp))
|
||||||
|
|
||||||
_conn.commit()
|
_conn.commit()
|
||||||
_conn.close()
|
_conn.close()
|
||||||
@@ -120,7 +120,7 @@ def list_images_for_user(owner):
|
|||||||
_conn = sqlite3.connect(image_db_file_location)
|
_conn = sqlite3.connect(image_db_file_location)
|
||||||
_c = _conn.cursor()
|
_c = _conn.cursor()
|
||||||
|
|
||||||
command = "select uid, timestamp, name from images where owner = '{0}'".format(owner)
|
command = "SELECT uid, timestamp, name FROM images WHERE owner = '{0}'".format(owner)
|
||||||
_c.execute(command)
|
_c.execute(command)
|
||||||
result = _c.fetchall()
|
result = _c.fetchall()
|
||||||
|
|
||||||
@@ -134,7 +134,7 @@ def match_user_id_with_image_uid(image_uid):
|
|||||||
_conn = sqlite3.connect(image_db_file_location)
|
_conn = sqlite3.connect(image_db_file_location)
|
||||||
_c = _conn.cursor()
|
_c = _conn.cursor()
|
||||||
|
|
||||||
command = "select owner from images where uid = '" + image_uid + "';"
|
command = "SELECT owner FROM images WHERE uid = '" + image_uid + "';"
|
||||||
_c.execute(command)
|
_c.execute(command)
|
||||||
result = _c.fetchone()[0]
|
result = _c.fetchone()[0]
|
||||||
|
|
||||||
@@ -147,7 +147,7 @@ def delete_image_from_db(image_uid):
|
|||||||
_conn = sqlite3.connect(image_db_file_location)
|
_conn = sqlite3.connect(image_db_file_location)
|
||||||
_c = _conn.cursor()
|
_c = _conn.cursor()
|
||||||
|
|
||||||
command = "delete from images where uid = '" + image_uid + "';"
|
command = "DELETE FROM images WHERE uid = '" + image_uid + "';"
|
||||||
_c.execute(command)
|
_c.execute(command)
|
||||||
|
|
||||||
_conn.commit()
|
_conn.commit()
|
||||||
@@ -160,4 +160,4 @@ def delete_image_from_db(image_uid):
|
|||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
print(list_users())
|
print(list_users())
|
||||||
|
|||||||
Reference in New Issue
Block a user