Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
M
mongo
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
mohammed.shibili
mongo
Commits
756e82db
Commit
756e82db
authored
Jan 20, 2023
by
mohammed.shibili
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
included postgres
parent
e66200b6
Hide whitespace changes
Inline
Side-by-side
Showing
9 changed files
with
143 additions
and
15 deletions
+143
-15
app.py
+15
-4
conf/dbconf.conf
+10
-2
script/configuration/databaseconfig.py
+8
-1
script/core/engine/connecton.py
+21
-2
script/core/handlers/deleting.py
+10
-0
script/core/handlers/finding.py
+14
-0
script/core/handlers/inserting.py
+19
-0
script/core/handlers/update.py
+12
-0
script/servieces/accept_data.py
+34
-6
No files found.
app.py
View file @
756e82db
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
\n
2.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"
)
conf/dbconf.conf
View file @
756e82db
[
data_base
]
# for mongodb
server
=
mongodb
://
localhost
:
27017
database
=
employee_db
collection
=
employees
\ No newline at end of file
collection
=
employees
# for postgres
postgrehost
=
localhost
postgredatabase
=
emp_db
postgreuser
=
postgres
postgrepassword
=
admin
portnumber
=
5432
\ No newline at end of file
script/configuration/databaseconfig.py
View file @
756e82db
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'
)
script/core/engine/connecton.py
View file @
756e82db
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
script/core/handlers/deleting.py
View file @
756e82db
# 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
()
script/core/handlers/finding.py
View file @
756e82db
# 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
()
script/core/handlers/inserting.py
View file @
756e82db
# 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
()
script/core/handlers/update.py
View file @
756e82db
# 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
()
script/servieces/accept_data.py
View file @
756e82db
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
\n
2.update
\n
3.delete
\n
4.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
\n
2.update
\n
3.delete
\n
4.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
)
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment