-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdb.py
More file actions
250 lines (210 loc) · 5.64 KB
/
db.py
File metadata and controls
250 lines (210 loc) · 5.64 KB
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
import string
# ---------------------
# defaults
# ---------------------
DB_NAME = 'profile.db'
T_NAME = 'users'
# ---------------------
# database handling
# ---------------------
import sqlite3
# reference tutorial: https://www.youtube.com/watch?v=byHcYRpMgI4
def init(db_name = DB_NAME, table_name = T_NAME):
# connect to database
# database file is created if it doesn't exist
conn = sqlite3.connect(db_name)
# use line below instead if you'd like to use a sqlite database in temporary memory only
# conn = sqlite3.connect(':memory:')
#
# create a cursor
c = conn.cursor()
# create a table | DATYPES: null, integer, real, text, blob
c.execute(f"""CREATE TABLE IF NOT EXISTS {table_name} (
un text,
pw text,
first_name text,
last_name text,
email text,
interests text
)
""")
# commit our command
conn.commit()
# close connection (good practice)
conn.close()
print(f'Initialized database "{db_name}" and table "{table_name}".')
return
def un_exists(un, db_name = DB_NAME):
conn = sqlite3.connect(db_name)
c = conn.cursor()
c.execute(f"""SELECT un
FROM users
WHERE un = '{un}'
""")
un_result = c.fetchall()
result = True if len(un_result) else False
print(f'db.un_exists("{un}") result = "{result}"')
conn.close()
return result
def profile_new(
un,
pw,
fn,
ln,
em,
ints,
db_name = DB_NAME
):
result = False
conn = sqlite3.connect(db_name)
c = conn.cursor()
if not un_exists(un):
c.execute("""INSERT INTO users VALUES (
?,
?,
?,
?,
?,
?
)
""", (un, pw, fn, ln, em, ints,))
conn.commit()
result = True
print(f'Created new profile for "{un}".')
pass
else:
print(f'CANNOT create new profile. Profile for "{un}" already exists.')
pass
conn.close()
return result
def profile_get(*args, **kwargs):
uns = []
print_all = False
profiles = []
if len(args):
for arg in args:
if type(arg) == str:
uns.append(arg)
if type(arg) == list and len(arg) and type(arg[0]) == str:
for un in arg:
uns.append(un)
# print(f'uns = {uns}')
# handle default kwargs
if 'db_name' not in kwargs.keys():
db_name = DB_NAME
pass
else:
db_name = kwargs['db_name']
pass
if 'all' in kwargs.keys():
# print all profiles
print_all = kwargs['all']
pass
conn = sqlite3.connect(db_name)
c = conn.cursor()
if print_all:
c.execute(f"""SELECT rowid, *
FROM users
ORDER BY un
""")
profiles = c.fetchall()
pass
else:
for un in uns:
print(f'un = {un}')
c.execute(f"""SELECT rowid, *
FROM users
WHERE un = '{un}'
""")
profiles.append(c.fetchone())
print(f'db.profile_get("*{args}, **{kwargs}") profiles =')
for profile in profiles:
print(profile)
conn.close()
return profiles
def un_login(un, pw, db_name = DB_NAME):
conn = sqlite3.connect(db_name)
c = conn.cursor()
c.execute(f"""SELECT rowid, first_name
FROM users
WHERE un = '{un}'
AND pw = '{pw}'
""")
# first_name = c.fetchone()
# first_name = c.fetchmany(3)
first_name = c.fetchall()
result = True if len(first_name) else False
print(f'db.un_login("{un}", "{pw}") result = "{result}"')
conn.close()
return result
def profile_update(un, **kwargs):
# handle default kwargs
if 'db_name' not in kwargs.keys():
db_name = DB_NAME
pass
else:
db_name = kwargs['db_name']
pass
conn = sqlite3.connect(db_name)
c = conn.cursor()
for key in kwargs.keys():
c.execute(f"""UPDATE users
SET '{key}' = '{kwargs[key]}'
WHERE un = '{un}'
""")
print(f'db.profile_update({un}, {kwargs}) executed.')
conn.commit()
conn.close()
return
def profile_delete(un, **kwargs):
# handle default kwargs
if 'db_name' not in kwargs.keys():
db_name = DB_NAME
pass
else:
db_name = kwargs['db_name']
pass
conn = sqlite3.connect(db_name)
c = conn.cursor()
c.execute(f"""DELETE from users
WHERE un = '{un}'
""")
print(f'db.profile_delete({un}) profile deleted, if it existed in the first place.')
conn.commit()
conn.close()
return
# ----------------------
# Main application function
# ----------------------
def main():
# Database initialization and tests
init()
un_exists('userNameThatDoesNotExist')
profile_delete('userNameThatDoesNotExist')
profile_delete('davidDelSol')
profile_new(
'davidDelSol',
'encrypted?',
'david',
'aloka',
'test@preform.io',
'salsa,extended intelligence,marathon running in a full suit'
)
profile_new(
'Python733t',
'encrypted?',
'Doroteo ',
'Bonilla',
'doabonilla@yahoo.com',
'work,school,sleep,repeat'
)
un_login('davidDelSol', 'encrypted?')
profile_update('davidDelSol', pw = 'definitelyNotEncripted!')
un_login('davidDelSol', 'definitelyNotEncripted!')
un_exists('davidDelSol')
profile_get(all = True)
profile_get(['Python733t'])
profile_get('Python733t')
pass
if __name__ == '__main__':
main()