/
store_file_in_database.py
82 lines (63 loc) · 2.07 KB
/
store_file_in_database.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
80
81
82
"""
When start script check password is corect if true,
may attach file to database or list attached files from database
"""
# Import Modules
from hashlib import sha256 # Generate secret password
import sys # Use for exit
import sqlite3 # Database is sqlite
# sha256 password slice to 15 symbol
SECRET_PASSWORD = "10e357984b55966"
# Function's
def create_or_alter_table():
"""Create table if not exists"""
try:
con = sqlite3.connect('db_file.db')
except sqlite3.Error as e:
print(e)
else:
c = con.cursor()
sql = '''CREATE TABLE IF NOT EXISTS ideqs
(id INTEGER PRYMARY KEY AUTOINCREMENT, name text NOT NULL,
extension text NOT NULL)'''
c.execute(sql)
con.commit()
finally:
return con
def func_to_save_file_in_database():
"""Save path to database"""
con = create_or_alter_table()
c = con.cursor()
files = input("Enter path to file :")
path, extension = files.split(".")
command = """INSERT INTO ideqs (name, extension) VALUES (?, ?)"""
c.execute(command, (files, extension,))
con.commit()
def func_to_load_file_from_database():
"""Return files from database"""
con = create_or_alter_table()
c = con.cursor()
sql = """SELECT * FROM ideqs"""
c.execute(sql)
for line in c.fetchall():
print(line[1], line[2])
def ask_what_you_need():
print("-"*20)
print("""
S|s to store file in database
L|l to load file from database
Q|q to quit
""")
print("-"*20)
user = input("Enter what you whant :")
choices = {"s": func_to_save_file_in_database,
"l": func_to_load_file_from_database}
if user.lower() in choices.keys():
choices[user.lower()]()
else:
sys.eixt()
if __name__ == '__main__':
enter_password = sha256(sys.argv[1].encode("utf-8")).hexdigest()[:15]
if (enter_password[:15] != SECRET_PASSWORD):
sys.exit()
ask_what_you_need()