-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmanage_user.py
65 lines (49 loc) · 1.63 KB
/
manage_user.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
import sqlite3
from datetime import date
DB = "users.db"
TODAY = date.today()
# connect to our database and insert a new user
def insert_user(user, db=DB, date=TODAY):
"""Create a new user into the users table
:param user:
:param db:
:param today:
:return: user, date
"""
db_connection = None
try:
db_connection = sqlite3.connect(db)
cursor = db_connection.cursor()
print("[*] DB Connection Successful!")
sql = '''INSERT INTO users(entry_date, user)
VALUES(?,?)'''
cursor.execute(sql, (date, user))
db_connection.commit()
print("[*] User Inserted!")
db_connection.close()
return date, user
except Exception as e:
print(f"[!] DB connection aborted! Error:{e}")
return f"[!] DB connection aborted! Error:{e}"
def retrieve_users(db=DB):
"""Retrieve all users from the users table
:param db:
:return: users
"""
db_connection = None
try:
db_connection = sqlite3.connect(db)
cursor = db_connection.cursor()
print("[*] DB Connection Successful!")
sql = '''SELECT rowid, entry_date, user FROM users'''
cursor.execute(sql)
users = cursor.fetchall()
db_connection.commit()
print("[*] Users Retrieved!")
db_connection.close()
return users
except Exception as e:
print(f"[!] DB connection aborted! Error:{e}")
return f"[!] DB connection aborted! Error:{e}"
if __name__ == '__main__':
insert_user("dummy_user")