-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsqlite_user.py
More file actions
43 lines (35 loc) · 1.29 KB
/
Copy pathsqlite_user.py
File metadata and controls
43 lines (35 loc) · 1.29 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
import sqlite3
import hashlib
# Connect to the SQLite database
conn = sqlite3.connect('users.db')
cursor = conn.cursor()
# Create a users table if it doesn't exist
cursor.execute('''CREATE TABLE IF NOT EXISTS users (
id INTEGER PRIMARY KEY,
username TEXT UNIQUE,
password TEXT
)''')
# Function to hash passwords
def hash_password(password):
return hashlib.sha256(password.encode()).hexdigest()
# Function to register a new user
def register_user(username, password):
hashed_password = hash_password(password)
cursor.execute('INSERT INTO users (username, password) VALUES (?, ?)', (username, hashed_password))
conn.commit()
print('User registered successfully!')
# Function to login a user
def login_user(username, password):
hashed_password = hash_password(password)
cursor.execute('SELECT * FROM users WHERE username=? AND password=?', (username, hashed_password))
user = cursor.fetchone()
if user:
print('Login successful!')
else:
print('Invalid username or password.')
# Register a new user (uncomment to test)
# register_user('testuser', 'testpassword')
# Login a user (uncomment to test)
# login_user('testuser', 'testpassword')
# Close the database connection
conn.close()