VNSGU BCA Sem 2: Desktop Publishing (205_02) Practical Solutions - April 2025 Set A
- Subject: Desktop Publishing (DRA) / Python SQLite
- Subject Code: 205_02
- Set: A
- Semester: 2
- Month/Year: April 2025
- Max Marks: 25
- Time Recommendation: 45 Minutes
- Paper: View Paper | Download PDF
Questions & Solutions
All questions are compulsory
Q1A: Employee Database Management
Max Marks: 10
Write a Python Script to do following:
- Create Employee (Eno, Ename, Salary, Designation) table.
- Insert minimum 5 records in the table.
1. Database Connection & Table Initialization
Establish a connection to SQLite and create the table structure.
Use the sqlite3 library. The connect() method creates the database file if it doesn't already exist.
View Solution & Output
import sqlite3
# [1] Connect and setup table
conn = sqlite3.connect('company.db')
cursor = conn.cursor()
cursor.execute('''
CREATE TABLE IF NOT EXISTS Employee (
Eno INTEGER PRIMARY KEY,
Ename TEXT NOT NULL,
Salary REAL,
Designation TEXT
)
''')
print("Employee table created successfully!")
Step-by-Step Explanation:
- Initialization: Import
sqlite3and establish a connection to 'company.db'. - Logic Flow: Use a
cursorto execute aCREATE TABLEstatement defining theEmployeeschema. - Completion: Print a confirmation message indicating the table is ready.
2. Batch Data Insertion
Insert at least 5 records and commit the changes.
View Solution & Output
# [2] Insert 5 records
employees = [
(101, 'Rahul Sharma', 75000, 'Manager'),
(102, 'Priya Patel', 65000, 'Developer'),
(103, 'Amit Kumar', 80000, 'Team Lead'),
(104, 'Sneha Gupta', 55000, 'Designer'),
(105, 'Vikram Singh', 90000, 'Director')
]
cursor.executemany('INSERT INTO Employee VALUES (?, ?, ?, ?)', employees)
conn.commit()
print(f"{len(employees)} records inserted successfully!")
conn.close()
Step-by-Step Explanation:
- Initialization: Prepare a list of tuples containing employee records.
- Logic Flow: Use
executemany()for efficient bulk insertion andconn.commit()to save changes. - Completion: Display the count of inserted records and close the database connection.
Q1B: High Salary Filters
Max Marks: 10
Write a Python Script to print employee details who is earning more than 60,000 as a salary.
Use the SELECT * FROM ... WHERE Salary > 60000 query. You can use the fetchall() method to retrieve all matching rows into a list.
View Solution & Output
import sqlite3
# [1] Connect and execute filter
conn = sqlite3.connect('company.db')
cursor = conn.cursor()
cursor.execute('SELECT * FROM Employee WHERE Salary > 60000')
results = cursor.fetchall()
# [2] Display formatted results
print("-" * 50)
print(f"{'Eno':<5} {'Name':<20} {'Salary':<10}")
print("-" * 50)
for emp in results:
print(f"{emp[0]:<5} {emp[1]:<20} {emp[2]:<10.2f}")
conn.close()
Step-by-Step Explanation:
- Initialization: Connect to 'company.db' and initialize a cursor object.
- Logic Flow: Execute a
SELECTquery with aWHEREclause forSalary > 60000and fetch all matching records. - Completion: Iterate through the results and print them in a formatted table before closing the connection.
Concept Deep Dive: Cursor Objects
The cursor is like a pointer to the SQL result set. It allows you to execute commands and step through data. Methods like fetchone() and fetchall() allow you to move that pointer to retrieve the actual Python data types.
Q2: Viva Preparation
Max Marks: 5
Potential Viva Questions
- Q: Why use
conn.commit()?- A: Since SQLite is transactional, changes aren't saved to the disk until you call commit.
- Q: What is the benefit of
executemany()over a loop ofexecute()?- A: It is significantly faster and safer for large datasets.
- Q: What happens if you don't call
conn.close()?- A: The database file might remain locked and other programs might be unable to access it.
- Q: What is the data type
REALin SQLite?- A: It is used to store floating-point numbers (decimals), similar to
FLOATorDOUBLEin other databases.
- A: It is used to store floating-point numbers (decimals), similar to
- Q: How do you retrieve only the names of all employees?
- A: Use the query
SELECT Ename FROM Employee.
- A: Use the query
- Q: What does the
connect()function return?- A: It returns a Connection object that represents the database connection.
- Closing too early: If you close the connection before the cursor finishes fetching, you will get an error.
- Case Sensitivity: Field names in SQL are case-insensitive, but data strings like
'Manager'are case-sensitive.
Quick Navigation
Related Solutions
| Set | Link |
|---|---|
| Set A | Current Page |
| Set B | Solutions |
| Set C | Solutions |
| Set D | Solutions |
| Set E | Solutions |
| Set F | Solutions |
Last Updated: April 2026