-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatabase_manager.py
59 lines (51 loc) · 2.09 KB
/
database_manager.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
import psycopg2
import os
# Create connection to the database
def connect():
try:
conn = psycopg2.connect(
host = 'localhost',
database = 'password_manager',
user = os.environ.get('DB_USER'),
password = os.environ.get('DB_PASS')
)
return conn
except (Exception, psycopg2.Error) as error:
print(error)
# Adding user details to the database
def storePassword(allsafe_username, app_name, email, username, password):
try:
connection = connect()
cursor = connection.cursor()
postgres_insert_qurey = """INSERT INTO allpasswords (allsafe_username, app_name, email, username, password) VALUES (%s, %s, %s, %s, %s)"""
record_to_insert = (allsafe_username, app_name, email, username, password)
cursor.execute(postgres_insert_qurey, record_to_insert)
connection.commit()
except (Exception, psycopg2.Error) as error:
print(error)
# Retrieving password
def getPassword(app_name):
try:
connection = connect()
cursor = connection.cursor()
postgres_insert_qurey = """ SELECT password FROM allpasswords WHERE app_name = '""" + app_name + "'" #"""SELECT password FROM allpasswords WHERE app_name=(%s)"""
record_to_insert = (app_name)
cursor.execute(postgres_insert_qurey, record_to_insert)
connection.commit()
result = cursor.fetchone()[0]
return result
except (Exception, psycopg2.Error) as error:
print(error)
# Retrieving all app detail's of a specific user
def getUserDetails(allsafe_username):
try:
connection = connect()
cursor = connection.cursor()
postgres_insert_qurey = """ SELECT * FROM allpasswords WHERE allsafe_username = '""" + allsafe_username + "'" #"""SELECT password FROM allpasswords WHERE app_name=(%s)"""
record_to_insert = (allsafe_username)
cursor.execute(postgres_insert_qurey, record_to_insert)
connection.commit()
details = cursor.fetchall()
return details
except (Exception, psycopg2.Error) as error:
print(error)