Commit 8cd28115 by ajil.k

added postgresql files

parent a35c373a
# importing library
from scripts.services.crud_operations import start_crud_operation
from scripts.services.crud_operations import start_crud_operation_mongodb, start_crud_operations_postgresql
# calling function to perform crud operations
start_crud_operation()
print("-----CRUD Operations-----\n"
"1.MongoDB\n"
"2.PostgreSQL\n")
# choose which method to start first
choice = int(input("Enter your choice: "))
# check which option is entered
if choice == 1:
# calling function to perform crud operations in mongoDB
start_crud_operation_mongodb()
elif choice == 2:
# calling function to perform crud operations in postgresqlDB
start_crud_operations_postgresql()
def delete_row(connection):
try:
cursor = connection.cursor()
print("------DELETE OPERATION------\n")
license_no = input("Enter the license no")
delete_data = f'DELETE FROM tbl_company WHERE license_no={license_no}'
cursor.execute(delete_data)
connection.commit()
print("Row deleted successfully!")
except Exception as e:
print("Error ", e)
def data_from_user():
company_license_no = input("Enter the company license no: ")
company_name = input("Enter the company name: ")
market_cap = input("Enter the market capital: ")
revenue_change = input("Enter the revenue change: ")
pat_change = input("Enter the pat change: ")
company_document = (company_license_no, company_name, market_cap, revenue_change, pat_change)
return company_document
from scripts.core.handlers.get_data import data_from_user
from scripts.core.handlers.get_data_mongodb import data_from_user
def inserting_one(company_collection):
......
from scripts.core.handlers.get_data_postgresql import data_from_user
def insert(connection):
cursor = connection.cursor()
print("------INSERT OPERATION------\n")
insert_table = "INSERT INTO tbl_company(license_no, company_name, market_cap,revenue_change, pat_change) VALUES(" \
"%s,%s,%s,%s,%s)"
row_value = data_from_user()
cursor.execute(insert_table, row_value)
connection.commit()
count = cursor.rowcount
print(count, "Record inserted successfully into tbl_company table")
def read_data(connection):
try:
cursor = connection.cursor()
print("------SELECT OPERATION------\n")
view_data = "SELECT * FROM tbl_company"
cursor.execute(view_data)
company_records = cursor.fetchall()
print("Each row and it's columns values")
for row in company_records:
print("license_no = ", row[0])
print("company_name = ", row[1])
print("market_cap = ", row[2])
print("revenue_change = ", row[3])
print("pat_change = ", row[4], "\n")
except Exception as e:
print("Error ", e)
def update_row(connection):
try:
cursor = connection.cursor()
print("------UPDATE OPERATION------\n")
license_no = input("Enter the license no")
print("Choose the field you want to update\n"
"1.company_name\n"
"2.market_cap\n"
"3.revenue_change\n"
"4.pat_change\n")
field_choice = int(input("Enter your choice: "))
if field_choice == 1:
update_data = input("Enter the data: ")
update_query = f'update tbl_company SET company_name ={update_data} where license_no={license_no}'
else:
if field_choice == 2:
field_to_update = "market_cap"
elif field_choice == 3:
field_to_update = "revenue_change"
elif field_choice == 4:
field_to_update = "pat_change"
update_data = input("Enter the data: ")
update_query = f'UPDATE tbl_company SET {field_to_update}={update_data} WHERE license_no={license_no}'
cursor.execute(update_query)
connection.commit()
print("Row updated successfully!")
except Exception as e:
print("Error ", e)
import psycopg2
def connect_db():
try:
connection = psycopg2.connect(user="postgres",
password="test",
host="127.0.0.1",
port="5432",
database="company_db")
return connection
except (Exception, psycopg2.Error) as error:
print("Failed to Connect to DB", error)
from scripts.core.handlers.mongodb_connect import db_connect
from scripts.core.handlers.insert import inserting_one, inserting_many
from scripts.core.handlers.read_documents import finding_one, finding_many
from scripts.core.handlers.delete import delete_one_document, delete_many_document
from scripts.core.handlers.update import update_one_document, update_many_document
from scripts.db_connection.mongodb_connect import db_connect
from scripts.db_connection.postgreSQL_connect import connect_db
from scripts.core.handlers.insert_mongodb import inserting_one, inserting_many
from scripts.core.handlers.read_documents_mongodb import finding_one, finding_many
from scripts.core.handlers.delete_mongodb import delete_one_document, delete_many_document
from scripts.core.handlers.update_mongodb import update_one_document, update_many_document
from scripts.core.handlers.insert_postgresql import insert
from scripts.core.handlers.read_data_postgresql import read_data
from scripts.core.handlers.delete_postgresql import delete_row
from scripts.core.handlers.update_postgresql import update_row
def start_crud_operation():
def start_crud_operation_mongodb():
try:
# create a db connection
db = db_connect()
......@@ -12,32 +18,58 @@ def start_crud_operation():
# create a collection
company_collection = db['company']
# inserting one document
inserting_one(company_collection)
# finding more than one document
inserting_many(company_collection)
# finding one document
finding_one(company_collection)
# inserting more than one document
finding_many(company_collection)
# updating one document
update_one_document(company_collection)
# updating many documents
update_many_document(company_collection)
# deleting one document
delete_one_document(company_collection)
# deleting many documents
delete_many_document(company_collection)
except Exception as e:
print("Error-", e)
\ No newline at end of file
print("Error-", e)
def start_crud_operations_postgresql():
try:
postgresql_connection = connect_db()
cursor = postgresql_connection.cursor()
# checking if the table exists
cursor.execute(f"SELECT * FROM information_schema.tables WHERE table_name = 'tbl_company'")
if cursor.fetchone():
print("Table Already Exists!")
else:
postgres_create_query = """CREATE TABLE tbl_company (license_no INT,
company_name CHAR(50) NOT NULL,
market_cap INT NOT NULL,
revenue_change INT NOT NULL,
pat_change INT NOT NULL,
PRIMARY KEY( license_no ))"""
cursor.execute(postgres_create_query)
postgresql_connection.commit()
print("Table Created!")
# inserting records in the table
insert(postgresql_connection)
# read or selecting the data in the table
read_data(postgresql_connection)
# updating data in the table based on the license number user entered
update_row(postgresql_connection)
# deleting data in the table based on the license number user entered
delete_row(postgresql_connection)
except Exception as e:
print("Error-", 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