Commit bb6987ad by arjun.b

PostgreSQL task

parents
# Default ignored files
/shelf/
/workspace.xml
<?xml version="1.0" encoding="UTF-8"?>
<module type="PYTHON_MODULE" version="4">
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$" />
<orderEntry type="jdk" jdkName="Python 3.9" jdkType="Python SDK" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
\ No newline at end of file
<component name="InspectionProjectProfileManager">
<profile version="1.0">
<option name="myName" value="Project Default" />
<inspection_tool class="PyPep8NamingInspection" enabled="true" level="WEAK WARNING" enabled_by_default="true">
<option name="ignoredErrors">
<list>
<option value="N801" />
</list>
</option>
</inspection_tool>
</profile>
</component>
\ No newline at end of file
<component name="InspectionProjectProfileManager">
<settings>
<option name="USE_PROJECT_PROFILE" value="false" />
<version value="1.0" />
</settings>
</component>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.9" project-jdk-type="Python SDK" />
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/Postgress.iml" filepath="$PROJECT_DIR$/.idea/Postgress.iml" />
</modules>
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>
\ No newline at end of file
from scripts.config.connect import db_connect
from scripts.core.handlers.delete_table import delete_table
from scripts.core.handlers.insert_data import insert_table
from scripts.core.handlers.select_data import select_table
from scripts.core.handlers.update_data import update_table
conn = db_connect()
ch = True
while ch:
choice = int(input("enter your choice\n1.insert\n2.update\n3.delete\n4.read\n"))
if choice == 1:
insert_table(conn)
elif choice == 2:
update_table(conn)
elif choice == 3:
delete_table(conn)
elif choice == 4:
select_table(conn)
import psycopg2
from psycopg2 import Error
def db_connect():
# connecting the database
try:
connection = psycopg2.connect(user='postgres',
password='123',
host='localhost',
port=5432,
database='student')
# creating the cursor to perform database operations
cursor = connection.cursor()
cursor.execute(f"SELECT * FROM information_schema.tables WHERE table_name = 'student'")
# create table
if cursor.fetchone():
pass
else:
create_table = '''CREATE TABLE student(id int PRIMARY KEY,
f_name char(10) NOT NULL,l_name char(10) NOT NULL,Age int NOT NULL, department char(20) NOT NULL)'''
# execute the query
cursor.execute(create_table)
print("table created successfully")
return connection
except (Exception, Error) as error:
print(error)
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()
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()
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()
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()
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