-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDatabaseManager.py
More file actions
174 lines (156 loc) · 4.38 KB
/
DatabaseManager.py
File metadata and controls
174 lines (156 loc) · 4.38 KB
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import pyrebase
import pickle
import csv
#Loading Data from the configration file.
file = open("Configuration.pyrebaseConfig","rb")
config = {}
config = pickle.load(file)
file.close()
#Creating database object.
firebase = pyrebase.initialize_app(config)
db = firebase.database()
a = False
#Functions.........
def insert():
name = input("Enter the Name: ")
all_users = db.child("User").get()
for user in all_users.each():
if(name == user.key()):
print("Name already exists!")
a = False
break
else:
a = True
continue
if a == True:
age = int(input("Enter the Age: "))
dob = int(input("Enter the DOB(only year): "))
path = "User/"+name+"/"
data = {path:{"Age":age,"DOB":dob}}
db.update(data)
print("Data Successfully Inserted!")
def lists():
all_users = db.child("User").get()
print("***List of Users***")
for user in all_users.each():
name = user.key()
data = user.val()
Age = data['Age']
DOB = data['DOB']
print("\nName: ",name,"\nAge: ",Age,"\nDOB: ",DOB)
print("\n\nListing Done.....")
def remove():
name = input("Enter the Name: ")
all_users = db.child("User").get()
for user in all_users.each():
if(name == user.key()):
db.child("User").child(name).remove()
print("User: ",name," removed Successfully!")
a = True
break
else:
a = False
continue
if a == False:
print("Name does not exists!")
def show():
name = input("Enter the Name: ")
all_users = db.child("User").get()
for user in all_users.each():
if(name == user.key()):
print("User found!....Showing Data")
name = user.key()
data = user.val()
Age = data['Age']
DOB = data['DOB']
print("\nName: ",name,"\nAge: ",Age,"\nDOB: ",DOB)
a = True
break
else:
a = False
continue
if a == False:
print("Name does not exists!")
def update():
name = input("Enter the Name: ")
all_users = db.child("User").get()
for user in all_users.each():
if(name == user.key()):
age = int(input("Enter the Age: "))
dob = int(input("Enter the DOB(only year): "))
path = "User/"+name+"/"
data = {path:{"Age":age,"DOB":dob}}
db.update(data)
print("Data Successfully Updated!")
a = True
break
else:
a = False
continue
if a == False:
print("Name does not exists!")
def Exportcsv():
fields = ['Name','Age','DOB']
rows = []
all_users = db.child("User").get()
for user in all_users.each():
temp = []
data = user.val()
temp = [user.key(),data['Age'],data['DOB']]
rows.append(temp)
with open("ExportedFile.csv",'w') as csvfile:
csvwriter = csv.writer(csvfile)
csvwriter.writerow(fields)
csvwriter.writerows(rows)
print("Data Successfully Exported as .csv!")
def Importcsv():
fields = []
rows = []
with open("ImportFile.csv","r") as csvfile:
csvreader = csv.reader(csvfile)
fields = next(csvreader)
for row in csvreader:
rows.append(row)
print("Data imported from .csv file and now Uploading.......")
for i in rows:
name = i[0]
age = i[1]
dob = i[2]
path = "User/"+name+"/"
data = {path:{"Age":age,"DOB":dob}}
db.update(data)
print("Data Uploaded!")
#End of functions.....
#Printing Menu
print(40*'*')
print("Pyrebase based Databse Manager")
print(40*('*'))
print("\n\n Menu")
print(40*'*')
print('''1.Inserting a Data.
2.Removing a Data.
3.List all Data
4.Search a Data.
5.Update a Data.
6.Export the Data as .csv file.
7.Import a .csv file and upload it.''')
print(40*'*')
#Getting input and running the entire pgm..
case = int(input("Enter a Operation(Its S.no): "))
if case == 1:
insert()
elif case == 2:
remove()
elif case == 3:
lists()
elif case == 4:
show()
elif case == 5:
update()
elif case == 6:
Exportcsv()
elif case == 7:
Importcsv()
else:
print("Operaion Does not Exists!\n\nERR: Not a valid input")
#End of he pgm