Commit ffb3c936 by arjun.b

updated after review.

parent a073a02b
# database connection
from scripts.config.connect import db_connect
from scripts.core.handlers.crud_ops import insert, select, delete_one, update_one, update_many, delete_many
from scripts.core.handlers.crud_ops import insert, select, delete_one, update_one, update_field, edit_field
db = db_connect()
# insert(db)
# delete_one(db)
# delete_many(db)
select(db)
update_one(db)
# update_many(db)
ch = True
while ch:
choice = int(input("enter your choice\n1.insert\n2.read\n3.delete"
"\n4.update\n5.update with new field score\n6.update score field"))
if choice == 1:
insert(db)
print("data inserted")
elif choice == 2:
select(db)
elif choice == 3:
delete_one(db)
print("data deleted")
elif choice == 4:
update_one(db)
print("data updated")
elif choice == 5:
update_field(db)
elif choice == 6:
edit_field(db)
print("field value updated")
[mongo]
uri_path=mongodb://localhost:27017
\ No newline at end of file
import configparser
try:
config = configparser.ConfigParser()
config.read("conf/application.conf")
# mongo uri
uri = config.get("mongo", "uri_path")
except configparser.NoOptionError:
print("could not find conf file")
import pymongo
from scripts.config.application_config import uri
def db_connect():
try:
# create mongo client
client = pymongo.MongoClient("mongodb://localhost:27017")
client = pymongo.MongoClient(uri)
# create database
database = client['Students']
database = client["Students"]
# create student collection
student = database['Student']
student = database["student"]
return student
except Exception as e:
print("Error:", str(e))
# db_name =students
# collection_name = student
......@@ -6,7 +6,6 @@ def insert(student):
age = int(input("enter your age"))
dept = input("enter your department")
student.insert_many([{"RollNo": rn, "f_name": f_name, "l_name": l_name, "Age": age, "Department": dept}])
print("Inserted")
except Exception as e:
print(str(e))
......@@ -15,7 +14,6 @@ def delete_one(student):
try:
data = int(input("enter the id of the document"))
student.delete_one({"RollNo": data})
print("data deleted")
except Exception as e:
print(str(e))
......@@ -37,14 +35,43 @@ def select(student):
def update_one(student):
try:
update = {"$set": {"l_name": "C"}}
student.update_one({"f_name": "Sanal"}, update)
user_id = int(input("enter user roll no."))
db_field = input("enter the field you want to update")
field_data = input("enter the data")
update_doc = {"$set": {db_field: field_data}}
student.update_many({"RollNo": user_id}, update_doc)
except Exception as e:
print(str(e))
def update_many(student):
# update collection with new field score
def update_field(student):
try:
student.update_many({"f_name": "Gopika"}, {"$set": {"Department": "MCA"}})
rn = int(input("enter the roll number"))
cricket = int(input("enter the score of cricket"))
football = int(input("enter the score of football"))
volleyball = int(input("enter the score of volleyball"))
student.update({"RollNo": rn},
{"$set":
{"sports":
{"cricket": cricket, "football": football, "volleyball": volleyball
}
}
}
)
print("new filed score added to student")
except Exception as e:
print(str(e))
def edit_field(student):
try:
rn = int(input("enter the roll number"))
doc = student.find_one({"RollNo": rn})
field = input("enter the field you want to update")
# if field == cricket:
value = int(input("enter the score"))
doc['sports'][field] = value
student.save(doc)
except Exception as e:
print(str(e))
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment