-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdatabase.py
56 lines (47 loc) · 1.72 KB
/
database.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
52
53
54
55
56
import sqlite3
class Database:
def __init__(self,db):
self.con = sqlite3.connect(db)
self.cur = self.con.cursor()
sql = """
CREATE TABLE IF NOT EXISTS users(
id Integer Primary Key,
firstname text,
lastname text,
email text,
password text
)
"""
self.cur.execute(sql)
self.con.commit()
def insert(self,firstname,lastname,email,password):
self.cur.execute("insert into users values(NULL,?,?,?,?)",
(firstname,lastname,email,password))
self.con.commit()
def get_users(self):
self.cur.execute("SELECT * FROM users")
rows = self.cur.fetchall()
return rows
def get_user(self, email):
self.cur.execute("SELECT * FROM users WHERE email = ?", (email,))
row = self.cur.fetchone()
if row:
return [row]
else:
return None
def get_user_by_id(self, id):
self.cur.execute("SELECT * FROM users WHERE id = ?", (id,))
row = self.cur.fetchone()
if row:
return [row]
else:
return None
def update_user(self, firstname, lastname, email, id):
self.cur.execute('UPDATE users SET firstname = ?, lastname = ?, email = ? WHERE id = ?',(firstname,lastname, email, id))
self.con.commit()
def update_pass(self, password, id):
self.cur.execute('UPDATE users SET password = ? WHERE id = ?',(password, id))
self.con.commit()
def remove_user(self, email):
self.cur.execute("DELETE FROM users WHERE email = ?",(email))
self.con.commit()