-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaccounts.py
79 lines (70 loc) · 2.12 KB
/
accounts.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
import cockroachdb
def create_account():
# Get user's information
username = input("Enter a new username: ")
password = input("Enter a new password: ")
# Connect to the CockroachDB database
try:
conn = cockroachdb.connect(
host='localhost',
port=2424,
user='username',
password='password',
database='users'
)
cursor = conn.cursor()
except cockroachdb.Error as e:
print(f'Error connecting to CockroachDB: {e}')
return
# Insert the new account into the database
query = f"INSERT INTO users (username, password) VALUES('{username}', '{password}')"
try:
cursor.execute(query)
conn.commit()
print("Account created successfully!")
except cockroachdb.Error as e:
print(f'Error creating account: {e}')
finally:
cursor.close()
conn.close()
def login():
# Get user's login information
username = input("Enter your username: ")
password = input("Enter your password: ")
# Connect to the CockroachDB database
try:
conn = cockroachdb.connect(
host='localhost',
port=2424,
user='username',
password='password',
database='users'
)
cursor = conn.cursor()
except cockroachdb.Error as e:
print(f'Error connecting to CockroachDB: {e}')
return
# Check if the login information is correct
query = f"SELECT * FROM users WHERE username='{username}' AND password='{password}'"
cursor.execute(query)
result = cursor.fetchone()
if result:
print("Welcome back!")
else:
print("Invalid login information.")
cursor.close()
conn.close()
if __name__ == "__main__":
while True:
print("1. Login")
print("2. Create an account")
print("3. Exit")
choice = input("Enter your choice: ")
if choice == "1":
login()
elif choice == "2":
create_account()
elif choice == "3":
break
else:
print("Invalid choice. Please try again.")