This repository was archived by the owner on Jun 10, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathusers.py
More file actions
74 lines (59 loc) · 1.89 KB
/
users.py
File metadata and controls
74 lines (59 loc) · 1.89 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
import sqlite3
import os
import time # For Unix timestamp handling
import random
import string
from hashlib import sha256
try:
from prettytable import PrettyTable
except ImportError:
os.system("pip install prettytable")
from prettytable import PrettyTable
# Ten CSDL
db_name = "data"
# Ket noi vao CSDL SQLite
db = sqlite3.connect(f"{db_name}.db")
# Tao mot vat the de chay lenh SQL
cursor = db.cursor()
# Tao mot TABLE neu TABLE ids ko ton tai
cursor.execute('''
CREATE TABLE IF NOT EXISTS users (
username TEXT NOT NULL UNIQUE,
password TEXT NOT NULL,
name TEXT,
date_created INTEGER,
phone TEXT
)
''')
# Retrieve and display data using PrettyTable
cursor.execute('SELECT * FROM users')
rows = cursor.fetchall()
# Print the data in a pretty table
table = PrettyTable()
table.field_names = ["Username", "Password", "Name", "Date created", "Phone number"]
# Add rows to the PrettyTable
for row in rows:
table.add_row(row)
# Display the table
print(table)
nusername=str(input("Enter new username (BLANK IF IGNORE): "))
if (nusername != ""):
npasswd = sha256(str(input("Enter password: ")).encode("utf-8")).hexdigest()
nname = str(input("Enter new name (BLANK IF NULL): "))
if nname == "": nname = None
nphone = str(input("Enter phone number (BLANK IF NULL): "))
if nphone == "": nphone = None
data = (nusername, npasswd, nname, int(time.time()), nphone)
# Insert data into the table
cursor.execute('''
INSERT OR IGNORE INTO users (username, password, name, date_created, phone)
VALUES (?, ?, ?, ?, ?)
''', data)
db.commit()
remove = str(input("Enter username of user you want to remove (BLANK IF IGNORE): "))
if remove != "":
cursor.execute("DELETE FROM users WHERE username=?", (remove,))
# Commit the transaction and close the connection
db.commit()
# Close the database connection
db.close()