Commit 756e82db by mohammed.shibili

included postgres

parent e66200b6
from script.core.engine.connecton import app_call
from script.servieces.accept_data import user_data
from script.core.engine.connecton import mongo_connection, postgres_connection
from script.servieces.accept_data import user_data, user_postgres
coll_name = app_call()
user_data(coll_name)
# accepting user selection of database
print("1.mongodb\n2.postgresql")
choice = int(input("enter your choice"))
if choice == 1:
coll_name = mongo_connection()
user_data(coll_name)
elif choice == 2:
connection, cursor_name = postgres_connection()
user_postgres(connection, cursor_name)
else:
print("invalid choice")
[data_base]
# for mongodb
server=mongodb://localhost:27017
database =employee_db
collection =employees
# for postgres
postgrehost=localhost
postgredatabase=emp_db
postgreuser=postgres
postgrepassword=admin
portnumber=5432
\ No newline at end of file
import configparser
# for mongodb
config = configparser.ConfigParser()
config.read("conf/dbconf.conf")
server_name = config.get("data_base", "server")
db_name = config.get("data_base", "database")
collection_name = config.get("data_base", "collection")
# for postgres
hostname = config.get("data_base", 'postgrehost')
database_sql = config.get("data_base", 'postgredatabase')
password = config.get("data_base", 'postgrepassword')
username = config.get("data_base", 'postgreuser')
port_name = config.get("data_base", 'portnumber')
import psycopg2
from pymongo import MongoClient
from script.configuration.databaseconfig import db_name, server_name, collection_name
from script.configuration.databaseconfig import db_name, server_name, collection_name, hostname, username, password, \
database_sql
def app_call():
# connecting to mongodb
def mongo_connection():
client = MongoClient(server_name)
database = client[db_name]
collection = database[collection_name]
return collection
# connecting to postgresql
def postgres_connection():
try:
connection = psycopg2.connect(
host=hostname,
database=database_sql,
user=username,
password=password,
)
cursor = connection.cursor()
print("connection successful")
except Exception as e:
print("connection error", e)
connection.set_session(autocommit=True)
return connection, cursor
# delete for mongodb
def deleting(id_to_delete, coll_name):
try:
coll_name.delete_many({'id': id_to_delete})
except Exception as e:
print(e, "not deleted")
# delete for postgres
def post_delete(connection, cursor_name):
field_to_search = input("enter the field of row to delete")
value_of_field = input("enter the value of the field")
query = f'DELETE FROM emp_table11 WHERE {field_to_search}={value_of_field}'
cursor_name.execute(query)
connection.commit()
# view for mongodb
def finding(id_to_find, coll_name):
try:
element_found = coll_name.find_one({"emp_id": id_to_find}, {"_id": 0})
print(element_found)
except Exception as e:
print(e, "not found")
# view for postgres
def postgres_search(connection, cursor_name):
search_field = input("enter the field")
value_of_field = input("enter the value")
query = f'SELECT * FROM emp_table11 WHERE {search_field} = {value_of_field}'
cursor_name.execute(query)
rows = cursor_name.fetchall()
for row in rows:
print(row)
connection.commit()
# insertion for mongodb
def inserting(datas_list, coll_name):
employee = datas_list
try:
coll_name.insert_many(employee)
except Exception as e:
print(e, "data not inserted")
# insertion for postgres
def post_insert(connection, cursor_name):
head_tuple = ("id", "name", "age", "salary")
print(f'insert values corresponding to:{head_tuple}')
data_list = []
value_num = int(input("enter the number of values to be inserted"))
while value_num > 0:
value_list = []
for values in range(0, 4):
value = input(f"enter value of heading {values + 1}")
value_list.append(value)
value_tuple = tuple(value_list)
data_list.append(value_tuple)
value_num = value_num - 1
cursor_name.executemany('INSERT INTO emp_table11 ("id", "name", "age", "salary") VALUES (%s,%s,%s,%s)', data_list)
connection.commit()
# update for mongodb
def updating(id_to_search, field_to_update, element_tobe_updated, coll_name):
update = {"$set": {field_to_update: element_tobe_updated}}
try:
coll_name.update_one({"emp_id": id_to_search}, update)
except Exception as e:
print(e, "updation failed")
# update for postgres
def post_update(connection, cursor_name):
id_to_update = input("enter the id to search")
field_to_update = input("enter the field to update")
element_tobe_updated = input("enter the value to be updated")
query = f'UPDATE emp_table11 SET {field_to_update} = {element_tobe_updated} WHERE id = {id_to_update}'
cursor_name.execute(query)
connection.commit()
from script.core.handlers.deleting import deleting
from script.core.handlers.finding import finding
from script.core.handlers.inserting import inserting
from script.core.handlers.update import updating
from script.core.handlers.deleting import deleting, post_delete
from script.core.handlers.finding import finding, postgres_search
from script.core.handlers.inserting import inserting, post_insert
from script.core.handlers.update import updating, post_update
# function for operation of mongodb
def user_data(coll_name):
print("1.insert \n2.update\n3.delete\n4.read")
operation = int(input("enter the operation to perform"))
......@@ -25,10 +26,37 @@ def user_data(coll_name):
field_to_update = input("enter the field to update")
element_tobe_updated = input("enter the value to be updated")
updating(id_to_search, field_to_update, element_tobe_updated, coll_name)
# delete operation
elif operation == 3:
id_to_delete = input("enter the id to deleted")
deleting(id_to_delete, coll_name)
# view operation
elif operation == 4:
id_to_find =int(input("enter the id to find"))
id_to_find = int(input("enter the id to find"))
finding(id_to_find, coll_name)
# function for operation of mongodb
def user_postgres(connection, cursor_name):
print("1.insert \n2.update\n3.delete\n4.read")
operation = int(input("enter the operation to perform"))
# creating table on postgresql
query = """
CREATE TABLE emp_table11 (
id INTEGER PRIMARY KEY,
name VARCHAR(255) NOT NULL,
age INTEGER NOT NULL,
salary INTEGER NOT NULL
);
"""
# accepting operation to be performed
cursor_name.execute(query)
connection.commit()
if operation == 1:
post_insert(connection, cursor_name)
elif operation == 2:
post_update(connection, cursor_name)
elif operation == 3:
post_delete(connection, cursor_name)
elif operation == 4:
postgres_search(connection, cursor_name)
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