-
Notifications
You must be signed in to change notification settings - Fork 1
/
sql.py
51 lines (39 loc) · 1.17 KB
/
sql.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import sqlite3
def execute(query):
connection = sqlite3.connect("spotifynow.db")
cursor = connection.cursor()
cursor.execute(query)
r = cursor.fetchall()
connection.commit()
connection.close()
return(r)
ADD_USER = """INSERT OR IGNORE INTO userdb (userid, username, authtoken)
VALUES ("{}", "{}", "none");"""
ADD_TOKEN = """UPDATE userdb
SET authtoken = "{}"
WHERE userid = "{}";"""
DEL_USER = """DELETE
FROM userdb
WHERE userid = "{}";"""
LIST_USERS = """SELECT userid
FROM userdb;"""
GET_USER = """SELECT *
FROM userdb
WHERE userid = "{}";"""
CREATE_TABLE = """CREATE TABLE IF NOT EXISTS userdb (
userid NVARCHAR(15) PRIMARY KEY,
username NVARCHAR(30),
authtoken NVARCHAR(300)
);"""
def add_user(userid, username):
return(execute(ADD_USER.format(userid, username)))
def add_token(authtoken, userid):
return(execute(ADD_TOKEN.format(authtoken, userid)))
def del_user(userid):
return(execute(DEL_USER.format(userid)))
def list_users():
return(execute(LIST_USERS))
def get_user(userid):
return(execute(GET_USER.format(userid)))
def create_table():
return(execute(CREATE_TABLE))