Commit fbbc728f by arjun.b

updated

parent bb6987ad
......@@ -16,3 +16,4 @@ while ch:
delete_table(conn)
elif choice == 4:
select_table(conn)
conn.close()
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:
......
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")
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))
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))
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))
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))
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