· 5 years ago · Jul 07, 2020, 08:02 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
18
19 if check_user is not None:
20 if password == check_user['password']:
21
22 print(f"Welcome, {login}")
23 else:
24 print("Please login or password incorrect, try again!!")
25
26
27def note(cursor, check_user):
28 while True:
29 user_choice = input("Do you want to create a note or sign out:")
30 if user_choice == "create a note":
31 note_title = input("Enter the names for the task: ")
32 note_text = input("Enter text of the task: ")
33 cursor.execute(
34 """
35 INSERT INTO notes (user_id, note_title,note_text)
36 VALUES(?, ?, ?)
37 """, (check_user['id'], note_title, note_text))
38 continue
39 else:
40 break
41
42
43def main():
44 login = input("Input login: ")
45 password = sha256(input("Input password: ").encode()).hexdigest()
46 input_task = input("You want registration or authorization?: ")
47
48 conn = sqlite3.connect("mydatabase.db")
49 conn.row_factory = sqlite3.Row
50 cursor = conn.cursor()
51
52 cursor.execute(
53 """
54 CREATE TABLE
55 IF NOT EXISTS user
56 (
57 id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
58 login,
59 password
60 )
61 """
62 )
63
64 cursor.execute(
65 """
66 CREATE TABLE
67 IF NOT EXISTS notes
68 (
69 user_Id Integer not null,
70 note_title,
71 note_text,
72 FOREIGN KEY (user_id) REFERENCES information (id)
73 )
74 """
75 )
76
77 check_user = cursor.execute(
78 """
79 SELECT id, login, password
80 FROM user
81 WHERE login = ?""", (login,)
82 ).fetchone()
83 print(check_user)
84
85 if input_task == "registration":
86 registration(cursor, login, password, check_user)
87 elif input_task == "authorization":
88 authorization(login, password, check_user)
89 try:
90 if check_user['login'] == login:
91 if check_user['password'] == password:
92 note(cursor, check_user)
93 except TypeError:
94 print('Please, input correct login and password')
95 conn.commit()
96 conn.close()
97
98
99if __name__ == '__main__':
100 main()