· 5 years ago · Jun 30, 2020, 01:30 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['password']:
19
20 print(f"Welcome, {login}")
21 else:
22 print("Please login or password incorrect, try again!!")
23
24
25def note(cursor, check_user):
26 while True:
27 user_choice = input("Do you want to create a note or sign out:")
28 if user_choice == "create a note":
29 note_title = input("Enter the names for the task: ")
30 note_text = input("Enter text of the task: ")
31 cursor.execute(
32 """
33 INSERT INTO notes (user_id, note_title,note_text)
34 VALUES(?, ?, ?)
35 """, (check_user['id'], note_title, note_text))
36 continue
37 else:
38 break
39
40
41def main():
42
43
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
86 if input_task == "registration":
87 registration(cursor, login, password, check_user)
88 elif input_task == "authorization":
89 authorization(login, password, check_user)
90 if check_user['login'] == login:
91 if check_user['password'] == password:
92 note(cursor, check_user)
93 conn.commit()
94 conn.close()
95
96
97if __name__ == '__main__':
98 main()