0% found this document useful (0 votes)
26 views6 pages

Question 1 Practical Paper Set 1 Option 1: File Operations

Uploaded by

coloufull.in
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views6 pages

Question 1 Practical Paper Set 1 Option 1: File Operations

Uploaded by

coloufull.in
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Question 1 practical paper set 1

Option 1: File Operations

with open("student_records.txt", "w") as file:


file.write("Alice | 101 | 85\nBob | 102 | 90\n")

with open("student_records.txt", "r") as file:


print(file.read())

Option 2: MySQL Database Operations

import mysql.connector

conn = mysql.connector.connect(host="localhost", user="root",


password="password", database="company")
cur = conn.cursor()

cur.execute("CREATE TABLE IF NOT EXISTS employees (emp_id INT PRIMARY


KEY, emp_name VARCHAR(50), emp_salary FLOAT, emp_department
VARCHAR(50))")
cur.executemany("INSERT INTO employees VALUES (%s, %s, %s, %s)", [
(1, 'Alice', 50000, 'HR'),
(2, 'Bob', 60000, 'Finance'),
(3, 'Charlie', 70000, 'IT')
])
cur.execute("SELECT * FROM employees")
for row in cur.fetchall():
print(row)

conn.commit()
conn.close()
Question 2 practical paper set 1

• Students in Grade A

sql
Copy code
SELECT * FROM students WHERE Grade = 'A';

• Count Students by Grade

sql
Copy code
SELECT Grade, COUNT(*) FROM students GROUP BY Grade;

• Courses and Instructors

sql
Copy code
SELECT course_name, Instructor FROM courses;

• Add marks Column

sql
Copy code
ALTER TABLE students ADD marks INT;

• Update Grade for Student 104

sql
Copy code
UPDATE students SET Grade = 'B' WHERE student_id = 104;
Question 1 practical paper set 2

Option 1: Binary File Operations

import pickle
with open("employee_data.dat", "wb") as f: pickle.dump([(101, "John
Doe", 45000.00), (102, "Jane Smith", 50000.00)], f)
with open("employee_data.dat", "rb") as f: print(pickle.load(f))

Option 2: MySQL Database Operations

import mysql.connector

conn = mysql.connector.connect(host="localhost", user="root", password="password",


database="library")

cur = conn.cursor()

cur.execute("CREATE TABLE IF NOT EXISTS books (id INT, title VARCHAR(100), author
VARCHAR(100), year INT, price FLOAT)")

cur.executemany("INSERT INTO books VALUES (%s, %s, %s, %s, %s)", [(1, "1984",
"Orwell", 1949, 350), (2, "Mockingbird", "Lee", 1960, 300), (3, "Gatsby", "Fitzgerald", 1925,
400)])

cur.execute("SELECT * FROM books")

print(cur.fetchall())

conn.commit()

conn.close()
Question 2 practical paper set 2

-- 1. Employees with salary > 50000

SELECT emp_name FROM employees WHERE emp_salary > 50000;

-- 2. Average salary

SELECT AVG(emp_salary) FROM employees;

-- 3. Count employees in Sales

SELECT COUNT(*) FROM employees e JOIN departments d ON e.emp_id = d.dept_id


WHERE dept_name = 'Sales';

-- 4. Insert new employee

INSERT INTO employees VALUES (505, 'Kate', 30, 55000);

-- 5. Delete employee with emp_id = 504

DELETE FROM employees WHERE emp_id = 504;


Question 1 practical paper set 3

Option 1: CSV File Operations

import csv
with open("products.csv", "w", newline="") as file:
writer = csv.writer(file)
writer.writerow(["Product ID", "Product Name", "Quantity",
"Price"])
writer.writerow([101, "Laptop", 10, 50000])
writer.writerow([102, "Smartphone", 15, 20000])

with open("products.csv", "r") as file:


print(file.read())

Option 2: MySQL Database Operations

import mysql.connector

conn = mysql.connector.connect(host="localhost", user="root",


password="password", database="school")
cur = conn.cursor()
cur.execute("CREATE TABLE IF NOT EXISTS students (student_id INT,
student_name VARCHAR(50), age INT)")
cur.execute("INSERT INTO students VALUES (1, 'Alice', 16)")
cur.execute("INSERT INTO students VALUES (2, 'Bob', 17)")
cur.execute("INSERT INTO students VALUES (3, 'Charlie', 18)")
cur.execute("SELECT * FROM students")
print(cur.fetchall())

conn.commit()
conn.close()
Question 2 practical paper set 3

-- 1. Orders with amount > 1000

SELECT * FROM orders WHERE order_amount > 1000;

-- 2. Customer names and phone numbers

SELECT customer_name, phone_number FROM customers;

-- 3. Orders on '2025-01-02'

SELECT * FROM orders WHERE order_date = '2025-01-02';

-- 4. Update order_amount for order_id = 1001

UPDATE orders SET order_amount = 600 WHERE order_id = 1001;

-- 5. Delete customer with customer_id = 203

DELETE FROM customers WHERE customer_id = 203;

You might also like