-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdb.py
137 lines (116 loc) · 3.68 KB
/
db.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import sqlite3
from sqlite3 import Error
class ServerDB():
def __init__(self):
self.conn = None
sql_create_users_table = """ CREATE TABLE IF NOT EXISTS users (
name text PRIMARY KEY,
password text NOT NULL,
bmi real
); """
self.create_connection(r'serverDB.db')
self.create_table(sql_create_users_table)
# self.create_user(('doe','999',45.2))
# self.update_user((20.3,'adel'))
# self.select_user_by_name(('adel'))
# self.select_all_users()
def create_connection(self,db_file):
""" create a database connection to the SQLite database
specified by db_file
:param db_file: database file
:return: Connection object or None
"""
try:
self.conn = sqlite3.connect(db_file)
print(sqlite3.version)
except Error as e:
print(e)
# finally:
# if self.conn:
# self.conn.close()
def create_table(self,create_table_sql):
""" create a table from the create_table_sql statement
:param create_table_sql: a CREATE TABLE statement
:return:
"""
try:
c = self.conn.cursor()
c.execute(create_table_sql)
except Error as e:
print(e)
def create_user(self,user):
"""
Create a new project into the projects table
:param user: (name:str,password:str,bmi:float)
:return: user id
"""
sql = ''' INSERT INTO users(name,password,bmi)
VALUES(?,?,?) '''
cur = self.conn.cursor()
try:
cur.execute(sql, user)
self.conn.commit()
# print(f'name: {cur.lastrowid}')
return cur.lastrowid
except sqlite3.IntegrityError as e:
print(e)
def update_user(self, user):
"""
update bmi of a user
:param user: (bmi:float,name:str)
:return:
"""
sql = ''' UPDATE users
SET bmi = ?
WHERE name = ?'''
cur = self.conn.cursor()
cur.execute(sql, user)
self.conn.commit()
def select_user_by_name(self, name):
"""
Query users by name
:param name: str
:return: the row as a triple (name,pasword,bmi)
"""
cur = self.conn.cursor()
cur.execute("SELECT * FROM users WHERE name=?", (name,))
rows = cur.fetchall() # this should be a single row since iam fetching using a unique identifier
try:
print(rows[0])
return rows[0]
except IndexError as e:
return 'notFound'
def select_all_users(self):
"""
Query all rows in the users table
:return:
"""
cur = self.conn.cursor()
cur.execute("SELECT * FROM users")
rows = cur.fetchall()
for row in rows:
print(row)
def delete_user(self,name):
"""
Delete a user by user name
:param name: name, of the user
:return:
"""
sql = 'DELETE FROM users WHERE name=?'
cur = self.conn.cursor()
try:
cur.execute(sql, (name,))
self.conn.commit()
except:
print('user does NOT exits')
def delete_all_users(self):
"""
Delete all rows in the users table
:return:
"""
sql = 'DELETE FROM users'
cur = self.conn.cursor()
cur.execute(sql)
self.conn.commit()
if __name__ == '__main__':
db = ServerDB()