· 5 years ago · Jun 28, 2020, 07:04 PM
1import sqlite3
2from hashlib import sha256
3
4
5def registration(cursor, login, password, check_user):
6 if check_user is None:
7 cursor.execute(
8 """
9 INSERT INTO user (login, password)
10 VALUES (?, ?)""", (login, password)
11 )
12 else:
13 print("Thy another user!")
14
15
16def authorization(login, password, check_user):
17 if check_user is not None:
18 if password == check_user[2]:
19 print(f"Welcome, {login}")
20 else:
21 print("Please login or password incorrect, try again!!")
22
23
24def note(cursor, check_user):
25 while True:
26 user_choice = input("Do you want to create a note or sign out:")
27 if user_choice == "create a note":
28 note_title = input("Enter the names for the task: ")
29 note_text = input("Enter text of the task: ")
30 cursor.execute(
31 """
32 INSERT INTO notes (user_id, note_title,note_text)
33 VALUES(?, ?, ?)
34 """, (check_user[0], note_title, note_text))
35 continue
36 else:
37 break
38
39
40def main():
41 login = input("Input login: ")
42 password = sha256(input("Input password: ").encode()).hexdigest()
43 input_task = input("You want registration or authorization?: ")
44
45 conn = sqlite3.connect("mydatabase.db")
46 cursor = conn.cursor()
47
48 cursor.execute(
49 """
50 CREATE TABLE
51 IF NOT EXISTS user
52 (
53 id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
54 login,
55 password
56 )
57 """
58 )
59
60 cursor.execute(
61 """
62 CREATE TABLE
63 IF NOT EXISTS notes
64 (
65 user_Id Integer not null,
66 note_title,
67 note_text,
68 FOREIGN KEY (user_id) REFERENCES information (id)
69 )
70 """
71 )
72
73 check_user = cursor.execute(
74 """
75 SELECT id, login, password
76 FROM user
77 WHERE login = ?""", (login,)
78 ).fetchone()
79 print(check_user)
80
81 if input_task == "registration":
82 registration(cursor, login, password, check_user)
83 elif input_task == "authorization":
84 authorization(login, password, check_user)
85 note(cursor, check_user)
86
87 conn.commit()
88 conn.close()
89
90
91if __name__ == '__main__':
92 main()