Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
P
PostgreSQL
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
arjun.b
PostgreSQL
Commits
fbbc728f
Commit
fbbc728f
authored
Feb 06, 2023
by
arjun.b
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
updated
parent
bb6987ad
Hide whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
79 additions
and
38 deletions
+79
-38
.idea/sonarlint/issuestore/index.pb
+8
-0
app.py
+1
-0
scripts/config/connect.py
+12
-3
scripts/constants/create_database.py
+10
-0
scripts/core/handlers/delete_table.py
+9
-5
scripts/core/handlers/insert_data.py
+16
-14
scripts/core/handlers/select_data.py
+11
-8
scripts/core/handlers/update_data.py
+12
-8
No files found.
.idea/sonarlint/issuestore/index.pb
View file @
fbbc728f
No preview for this file type
app.py
View file @
fbbc728f
...
...
@@ -16,3 +16,4 @@ while ch:
delete_table
(
conn
)
elif
choice
==
4
:
select_table
(
conn
)
conn
.
close
()
scripts/config/connect.py
View file @
fbbc728f
import
psycopg2
from
psycopg2
import
Error
from
scripts.constants.create_database
import
create_database
def
db_connect
():
# connecting the database
try
:
connection
=
psycopg2
.
connect
(
user
=
'postgres'
,
password
=
'123'
,
host
=
'localhost'
,
port
=
5432
,
database
=
'student'
)
database
=
"student_details"
)
connection
.
autocommit
=
True
# creating the cursor to perform database operations
cursor
=
connection
.
cursor
()
cursor
.
execute
(
f
"SELECT * FROM information_schema.tables WHERE table_name = 'student'"
)
# create table
cursor
.
execute
(
"SELECT 1 FROM pg_database WHERE datname='{}'"
.
format
(
"student_details"
))
if
cursor
.
fetchone
()
is
not
None
:
print
(
"database exist"
)
else
:
cursor
.
execute
(
"create database student_details"
)
print
(
"database created"
)
cursor
.
execute
(
"SELECT * FROM information_schema.tables WHERE table_name = 'student'"
)
if
cursor
.
fetchone
():
pass
else
:
...
...
scripts/constants/create_database.py
0 → 100644
View file @
fbbc728f
import
psycopg2
from
psycopg2
import
errors
def
create_database
(
cur
):
try
:
cur
.
execute
(
"create database student_details"
)
print
(
"database student_details created"
)
except
psycopg2
.
errors
.
DuplicateDatabase
:
print
(
"database student_details already exist"
)
scripts/core/handlers/delete_table.py
View file @
fbbc728f
def
delete_table
(
connection
):
cursor
=
connection
.
cursor
()
rn
=
input
(
"enter the id"
)
delete_query
=
f
'delete from student where id={rn}'
cursor
.
execute
(
delete_query
)
connection
.
commit
()
try
:
cursor
=
connection
.
cursor
()
rn
=
input
(
"enter the id"
)
delete_query
=
f
'delete from student where id={rn}'
cursor
.
execute
(
delete_query
)
connection
.
commit
()
print
(
f
'data with id {rn} deleted'
)
except
Exception
as
e
:
print
(
str
(
e
))
scripts/core/handlers/insert_data.py
View file @
fbbc728f
def
insert_table
(
connection
):
cursor
=
connection
.
cursor
()
data_count
=
int
(
input
(
"enter the number of data"
))
for
i
in
range
(
0
,
data_count
):
rn
=
input
(
"enter the id"
)
f_name
=
input
(
"enter the first name"
)
l_name
=
input
(
"enter the last name"
)
age
=
input
(
"enter your age"
)
dept
=
input
(
"enter the department"
)
insert_data
=
"INSERT INTO student(id,f_name,l_name,age,department) values(
%
s,
%
s,
%
s,
%
s,
%
s)"
row_value
=
(
rn
,
f_name
,
l_name
,
age
,
dept
)
cursor
.
execute
(
insert_data
,
row_value
)
print
(
"data inserted"
)
connection
.
commit
()
try
:
cursor
=
connection
.
cursor
()
data_count
=
int
(
input
(
"enter the number of data"
))
for
i
in
range
(
0
,
data_count
):
rn
=
input
(
"enter the id"
)
f_name
=
input
(
"enter the first name"
)
l_name
=
input
(
"enter the last name"
)
age
=
input
(
"enter your age"
)
dept
=
input
(
"enter the department"
)
insert_data
=
"INSERT INTO student(id,f_name,l_name,age,department) values(
%
s,
%
s,
%
s,
%
s,
%
s)"
row_value
=
(
rn
,
f_name
,
l_name
,
age
,
dept
)
cursor
.
execute
(
insert_data
,
row_value
)
print
(
"data inserted"
)
connection
.
commit
()
except
Exception
as
e
:
print
(
str
(
e
))
scripts/core/handlers/select_data.py
View file @
fbbc728f
def
select_table
(
connection
):
cursor
=
connection
.
cursor
()
select_query
=
"select * from student"
cursor
.
execute
(
select_query
)
row
=
cursor
.
fetchall
()
for
data
in
row
:
# print(f'id {data[0]},f_name{data[1]},l_name{data[2]},Age{data[3]}, Department{data[4]}')
print
(
data
[
0
],
data
[
1
],
data
[
2
],
data
[
3
],
data
[
4
])
connection
.
commit
()
try
:
cursor
=
connection
.
cursor
()
select_query
=
"select * from student"
cursor
.
execute
(
select_query
)
row
=
cursor
.
fetchall
()
for
data
in
row
:
# print(f'id {data[0]},f_name{data[1]},l_name{data[2]},Age{data[3]}, Department{data[4]}')
print
(
data
[
0
],
data
[
1
],
data
[
2
],
data
[
3
],
data
[
4
])
connection
.
commit
()
except
Exception
as
e
:
print
(
str
(
e
))
scripts/core/handlers/update_data.py
View file @
fbbc728f
def
update_table
(
connection
):
cursor
=
connection
.
cursor
()
rn
=
input
(
"enter the id"
)
field_to_update
=
input
(
"enter the field you want to update"
)
update_data
=
input
(
"enter the data"
)
update_query
=
f
'update student SET {field_to_update}={update_data} where id={rn}'
cursor
.
execute
(
update_query
)
connection
.
commit
()
try
:
cursor
=
connection
.
cursor
()
rn
=
input
(
"enter the id"
)
no_of_field
=
int
(
input
(
"How many fields you want to update?"
))
for
i
in
range
(
0
,
no_of_field
):
field
=
input
(
"enter the field name"
)
field_data
=
input
(
"enter the data"
)
update_query
=
f
"update student set {field}='{field_data}' where id={rn}"
cursor
.
execute
(
update_query
)
connection
.
commit
()
except
Exception
as
e
:
print
(
str
(
e
))
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