Commit fbbc728f by arjun.b

updated

parent bb6987ad
...@@ -16,3 +16,4 @@ while ch: ...@@ -16,3 +16,4 @@ while ch:
delete_table(conn) delete_table(conn)
elif choice == 4: elif choice == 4:
select_table(conn) select_table(conn)
conn.close()
import psycopg2 import psycopg2
from psycopg2 import Error from psycopg2 import Error
from scripts.constants.create_database import create_database
def db_connect(): def db_connect():
# connecting the database # connecting the database
try: try:
connection = psycopg2.connect(user='postgres', connection = psycopg2.connect(user='postgres',
password='123', password='123',
host='localhost', host='localhost',
port=5432, port=5432,
database='student') database="student_details")
connection.autocommit = True
# creating the cursor to perform database operations # creating the cursor to perform database operations
cursor = connection.cursor() cursor = connection.cursor()
cursor.execute(f"SELECT * FROM information_schema.tables WHERE table_name = 'student'") cursor.execute("SELECT 1 FROM pg_database WHERE datname='{}'".format("student_details"))
# create table 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(): if cursor.fetchone():
pass pass
else: 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): def delete_table(connection):
cursor = connection.cursor() try:
rn = input("enter the id") cursor = connection.cursor()
delete_query = f'delete from student where id={rn}' rn = input("enter the id")
cursor.execute(delete_query) delete_query = f'delete from student where id={rn}'
connection.commit() cursor.execute(delete_query)
connection.commit()
print(f'data with id {rn} deleted')
except Exception as e:
print(str(e))
def insert_table(connection): def insert_table(connection):
cursor = connection.cursor() try:
data_count = int(input("enter the number of data")) cursor = connection.cursor()
for i in range(0, data_count): data_count = int(input("enter the number of data"))
rn = input("enter the id") for i in range(0, data_count):
f_name = input("enter the first name") rn = input("enter the id")
l_name = input("enter the last name") f_name = input("enter the first name")
age = input("enter your age") l_name = input("enter the last name")
dept = input("enter the department") age = input("enter your age")
insert_data = "INSERT INTO student(id,f_name,l_name,age,department) values(%s,%s,%s,%s,%s)" dept = input("enter the department")
row_value = (rn, f_name, l_name, age, dept) insert_data = "INSERT INTO student(id,f_name,l_name,age,department) values(%s,%s,%s,%s,%s)"
cursor.execute(insert_data, row_value) row_value = (rn, f_name, l_name, age, dept)
print("data inserted") cursor.execute(insert_data, row_value)
connection.commit() print("data inserted")
connection.commit()
except Exception as e:
print(str(e))
def select_table(connection): def select_table(connection):
cursor = connection.cursor() try:
select_query = "select * from student" cursor = connection.cursor()
cursor.execute(select_query) select_query = "select * from student"
row = cursor.fetchall() cursor.execute(select_query)
for data in row: row = cursor.fetchall()
# print(f'id {data[0]},f_name{data[1]},l_name{data[2]},Age{data[3]}, Department{data[4]}') for data in row:
print(data[0], data[1], data[2], data[3], data[4]) # print(f'id {data[0]},f_name{data[1]},l_name{data[2]},Age{data[3]}, Department{data[4]}')
connection.commit() print(data[0], data[1], data[2], data[3], data[4])
connection.commit()
except Exception as e:
print(str(e))
def update_table(connection): def update_table(connection):
cursor = connection.cursor() try:
rn = input("enter the id") cursor = connection.cursor()
field_to_update = input("enter the field you want to update") rn = input("enter the id")
update_data = input("enter the data") no_of_field = int(input("How many fields you want to update?"))
update_query = f'update student SET {field_to_update}={update_data} where id={rn}' for i in range(0, no_of_field):
cursor.execute(update_query) field = input("enter the field name")
connection.commit() 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