-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb_handling.py
66 lines (56 loc) · 2.13 KB
/
db_handling.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
import os
import sqlite3
from dotenv import load_dotenv
load_dotenv()
DB_NAME = os.environ.get('DB_NAME')
TABLE_NAME = os.environ.get('TABLE_NAME')
def create_db_if_not_exits():
connection = sqlite3.connect(DB_NAME)
cur = connection.cursor()
return connection, cur
def create_table_if_not_exits(cur):
connection, cur = create_db_if_not_exits()
cur.execute("CREATE TABLE IF NOT EXISTS creds (Username TEXT NOT NULL, Password TEXT NOT NULL UNIQUE)")
connection.commit()
cur.close()
connection.close()
def insert_data(username, password):
connection, cur = create_db_if_not_exits()
try:
cur.execute("INSERT INTO creds (Username, Password) VALUES (?, ?)", (username, password))
except Exception as e:
return False
connection.commit()
cur.close()
connection.close()
return True
def delete_data(username, password):
connection, cur = create_db_if_not_exits()
cur.execute("DELETE FROM creds WHERE Username = (?) and Password = (?)", (username, password))
connection.commit()
cur.close()
connection.close()
def update_password(username, password):
connection, cur = create_db_if_not_exits()
cur.execute("UPDATE creds SET Password = (?) WHERE Username = (?)", (password, username))
connection.commit()
cur.close()
connection.close()
def select_data(username=None, password=None):
connection, cur = create_db_if_not_exits()
if username is None and password is None:
curr = cur.execute("SELECT * FROM creds ")
connection.commit()
return connection, cur, curr
if username is None:
curr = cur.execute("SELECT * FROM creds WHERE Password = (?)",(password))
connection.commit()
return connection, cur, curr
if password is None:
curr = cur.execute("SELECT * FROM creds WHERE Username = (?)",(username))
connection.commit()
return connection, cur, curr
if password is not None and username is not None:
curr = cur.execute("SELECT * FROM creds WHERE Username = (?) and Password = (?)",(username, password))
connection.commit()
return connection, cur, curr