0% found this document useful (0 votes)
18 views

Practicals SQL Notes

Uploaded by

wipido9160
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

Practicals SQL Notes

Uploaded by

wipido9160
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

1-Insert one record in table

2-Insert many records in table


3-Terminate
Enter Choice:1
Enter Employee No.:1
Enter Employee Name:Adhitya
Enter Employee Department:IT
Enter Date of Joining:2021/08/20
Enter Employee Salary:200000
(1, 'Adhitya', 'IT', datetime.date(2021, 8, 20), 200000)
1-Insert one record in table
2-Insert many records in table
3-Terminate
Enter Choice:2
Enter no. of records to Enter:2
Enter Employee No.:2
Enter Employee Name:Adhi
Enter Employee Department:HR
Enter Date of Joining:2022/02/14
Enter Employee Salary:140000
Enter Employee No.:3
Enter Employee Name:Arun
Enter Employee Department:IT
Enter Date of Joining:2020/09/08
Enter Employee Salary:240000
(1, 'Adhitya', 'IT', datetime.date(2021, 8, 20), 200000)
(2, 'Adhi', 'HR', datetime.date(2022, 2, 14), 140000)
(3, 'Arun', 'IT', datetime.date(2020, 9, 8), 240000)
1-Insert one record in table
2-Insert many records in table
3-Terminate
Enter Choice:3
Terminated
Code-18
import pymysql
def one_rec():
# Create a new connection and cursor
con = pymysql.connect(host="localhost", user="root", password="a!b@c#d$e%f67890")
mycursor = con.cursor()
# Database and table creation
mycursor.execute("create database if not exists students;")
mycursor.execute("use students")
mycursor.execute("CREATE TABLE IF NOT EXISTS EMP(Eno int, Ename varchar(30), Dept char(25), DOJ date,
Salary int)")
# User inputs
no = input("Enter Employee No.:")
name = input("Enter Employee Name:")
de = input("Enter Employee Department:")
doj = input("Enter Date of Joining:")
sal = input("Enter Employee Salary:")
# Insert record
query = "insert into emp values (%s, %s, %s, %s, %s)"
mycursor.execute(query, (no, name, de, doj, sal))
# Fetch and display records
mycursor.execute("select * from emp;")
c = mycursor.fetchall()
for i in c:
print(i)
# Commit the changes and close connection
con.commit()
con.close()
def many_rec():
# Create a new connection and cursor
con = pymysql.connect(host="localhost", user="root", password="a!b@c#d$e%f67890")
mycursor = con.cursor()
# Database and table creation
mycursor.execute("create database if not exists students;")
mycursor.execute("use students")
mycursor.execute("CREATE TABLE IF NOT EXISTS EMP(Eno int, Ename varchar(30), Dept char(25), DOJ date,
Salary int)")
# Insert multiple records
n = int(input("Enter no. of records to Enter:"))
for i in range(n):
no = input("Enter Employee No.:")
name = input("Enter Employee Name:")
de = input("Enter Employee Department:")
doj = input("Enter Date of Joining:")
sal = input("Enter Employee Salary:")
query = "insert into emp values (%s, %s, %s, %s, %s)"
mycursor.execute(query, (no, name, de, doj, sal))
# Fetch and display records
mycursor.execute("select * from emp;")
c = mycursor.fetchall()
for i in c:
print(i)
# Commit the changes and close connection
con.commit()
con.close()
# Main loop
while True:
print("1-Insert one record in table")
print("2-Insert many records in table")
print("3-Terminate")
o = int(input("Enter Choice:"))
if o == 1:
one_rec()
elif o == 2:
many_rec()
else:
print("Terminated")
break
1-Insert record in table
2-Search record using Employee Number
3-Search record using Department and Salary greater than 50000
4-Terminate
Enter Choice:1
Enter Employee No.:1
Enter Employee Name:Adhitya
Enter Employee Department:IT
Enter Date of Joining:2018/09/13
Enter Employee Salary:150000
(1, 'Adhitya', 'IT', datetime.date(2018, 9, 13), 150000)
1-Insert record in table
2-Search record using Employee Number
3-Search record using Department and Salary greater than 50000
4-Terminate
Enter Choice:2
Enter Employee ID:1
(1, 'Adhitya', 'IT', datetime.date(2018, 9, 13), 150000)
1-Insert record in table
2-Search record using Employee Number
3-Search record using Department and Salary greater than 50000
4-Terminate
Enter Choice:3
Enter Department:IT
(1, 'Adhitya', 'IT', datetime.date(2018, 9, 13), 150000)
1-Insert record in table
2-Search record using Employee Number
3-Search record using Department and Salary greater than 50000
4-Terminate
Enter Choice:4
Terminated
Codeeee-19

import pymysql
# Create connection and cursor
con = pymysql.connect(host="localhost", user="root", password="a!b@c#d$e%f67890")
mycursor = con.cursor()
def ins_rec():
mycursor.execute("create database if not exists students;")
mycursor.execute("use students")
mycursor.execute("CREATE TABLE IF NOT EXISTS EMP(Eno int, Ename varchar(30), Dept char(25), DOJ date,
Salary int)")
# Take user inputs
no = input("Enter Employee No.:")
name = input("Enter Employee Name:")
de = input("Enter Employee Department:")
doj = input("Enter Date of Joining:")
sal = input("Enter Employee Salary:")
# Insert record
query = "insert into emp values (%s, %s, %s, %s, %s)"
mycursor.execute(query, (no, name, de, doj, sal))
# Fetch and display all records
mycursor.execute("select * from emp;")
c = mycursor.fetchall()
for i in c:
print(i)
con.commit()
def search_rec():
x = int(input("Enter Employee ID:"))
mycursor.execute("use students")
# Fetch the record based on Employee No.
mycursor.execute("select * from emp where eno=%s", (x,))
cd = mycursor.fetchone()
print(cd)
con.commit()
def more_cons():
mycursor.execute("use students")
# Input department and check for salary > 50,000
d = input("Enter Department:")
mycursor.execute("select * from emp where salary > 50000 and dept=%s", (d,))
cd = mycursor.fetchall()
for i in cd:
print(i)
con.commit()
# Main loop
while True:
print("1-Insert record in table")
print("2-Search record using Employee Number")
print("3-Search record using Department and Salary greater than 50000")
print("4-Terminate")
o = int(input("Enter Choice:"))
if o == 1:
ins_rec()
elif o == 2:
search_rec()
elif o == 3:
more_cons()
else:
print("Terminated")
break
# Close connection after exiting the loop
con.close()

You might also like