Lab 1
Lab 1
Query:
Create database dbbpa6;
Lab 2: CRUD Operation
Create Operation:
# Create table student with necessary columns and constraints
Query:
create table student (
id int primary key,
name varchar(50),
faculty varchar(20),
age int,
address varchar(50)
);
Insert Operation:
# Insert data in to student table
Query:
insert into student (id, name, faculty, age, address) values
(1, 'Hari Thapa', 'BBS', 21, 'Dang'),
(2, 'Ankit Neupane', 'BBS', 21, 'Dang'),
(3, 'Ram Thapa', 'BPA', 21, 'Dang'),
(4, 'Sita KC', 'BPA', 20, 'NPJ'),
(5, 'Krishna', 'BPA', 19, 'Rolpa'),
(6, 'Suresh', 'BBA', 21, 'Butwal');
Read Operation:
# Display all the data of the table student
Query:
select * from student;
Result:
# Display name, faculty and address of student who is from out of NPJ
Query:
select name, faculty, address from student where address != 'NPJ';
Result:
# Display name, age and faculty of student whose age is greater than 20 and
studies in BPA
Query:
select name, age, faculty from student where age>20 and faculty = 'BPA';
Result:
# Display name, faculty and address of student who is studding in BPA or lives
in NPJ
Query:
select name, faculty, address from student where faculty = 'BPA' or address =
'NPJ';
Result:
Delete Operation:
# Display information of student table
Query:
select* from student;
Result:
# Delete the data from student whose id is 3
Query:
delete from student where id = 3;
# Display information of student table
Query:
select* from student;
Result:
Update Operation:
# Change the faculty of student as BBA whose id is 2
Query:
update student set faculty = 'BBA' where id = 2;
Result:
# Change the age as 25 and address as Npj whose id is 6
Query:
update student set age = 25, address = 'Npj' where id = 6;
Result:
#Display information of student whose name start with any character followed
by uresh
Query:
select* from student where name like '_uresh';
Result:
#Display name, age and number of students having more than 2 students in an
age group
Query:
select name, age, count(id) as Tot_Std from student group by age having
count(id)>=2;
Result:
#Display the information between name krishna and suresh and sort in
ascending order
Query:
select* from student where name between 'krishna' and 'Suresh' order by
name;
Result:
Inner Join:
#Display name and department of employee who is work in HR department
Query:
select empName, depName from employees
inner join departments
on employees.depID = departments.depID
where departments.depName='hr';
Result:
#Display the information of all employees with their corresponding
department name
Query:
select* from employees
inner join departments
on employees.depID = departments.depID;
Result:
Left Join:
# display the information of all employees with their corresponding
department name
Query:
select* from employees
left join departments
on employees.depID = departments.depID;
Result:
Right Join:
# display the information of all employees with their corresponding
department name
Query:
select* from employees
right join departments
on employees.depID = departments.depID;
Result:
# Display information
Query:
select * from bpastudents;
Result:
Create Database:
File Name: create_db.py
import mysql.connector
con = mysql.connector.connect(host="localhost", user="root", password="")
mycursor = con.cursor()
try:
mycursor.execute("create database db_MMC")
print("Database Created Successfully..")
con.close()
except:
con.rollback()
con.close()
Create Table:
File Name: create_table.py
import mysql.connector
con = mysql.connector.connect(host="localhost", user="root", password="",
database="db_MMC")
mycursor = con.cursor()
try:
mycursor.execute("create table student(Sid int primary key, Sname
varchar(50), Sgender varchar(6), Sage int, Sfee double)")
print("Table created successfully..")
con.close()
except:
con.rollback()
con.close()
Display Data:
File Name: display_data.py
import mysql.connector
con = mysql.connector.connect(host="localhost", user="root", password="",
database="db_MMC")
mycursor = con.cursor()
mycursor.execute("select * from student")
showResult = mycursor.fetchall()
print("ID Name Gender Age Fee")
for i in showResult:
id = i[0]
name = i[1]
gender = i[2]
age = i[3]
fee = i[4]
print(id,name,gender,age,fee)
con.close()
Output:
ID Name Gender Age Fee
2 Mina KC Female 25 50001.0
3 Sunita Rana Female 23 500010.59
4 Kumar Sanu Male 25 10000.0
5 Saraswati Bista Female 25 10000.0
6 Basanti Rimal Female 20 10000.0
7 Rahul KC Male 20 500.0
Update Data:
File Name: update_data.py
import mysql.connector
con = mysql.connector.connect(host="localhost", user="root", password="",
database="db_MMC")
mycursor = con.cursor()
try:
mycursor.execute("update student set Sfee=500010.59 where Sid=3")
con.commit()
print("Data updated successfully")
con.close()
except:
con.rollback()
con.close()
Result:
Name: John
City: New York
Age: 30
Lab 14: Parsing Python dictionary to JSON data
Parsejson.py
import json
pyobj = {
"name": "John",
"age": 30,
"city": "New York"
}
jsondata = json.dumps(pyobj)
print(jsondata)
Result:
{"name": "John", "age": 30, "city": "New York"}
Result:
Average Marks: 5.69
Result:
Name: Ram, Age: 30, Location: Nepalgunj
Name: Sita, Age: 25, Location: Dang
Name: Gopal, Age: 28, Location: Rolpa
THE END