VNSGU BCA Sem 2: Desktop Publishing (205_02) Practical Solutions - April 2025 Set D
- Subject: Desktop Publishing (DRA) / Python SQLite
- Subject Code: 205_02
- Set: D
- Semester: 2
- Month/Year: April 2025
- Max Marks: 25
- Time Recommendation: 45 Minutes
- Paper: View Paper | Download PDF
Questions & Solutions
All questions are compulsory
Q1: Library Data Migration
Max Marks: 20
- Create CSV file
Library.csvcontaining the following columns:Book_No,Book_Title,Subject,Author,Price. The file contains data of minimum 10 Books. - Create a table named
Bookswith appropriate data types. - Write a Python script that Imports data from the CSV file into the
Bookstable.
1. CSV Data Generation
Create the source file with at least 10 book records.
You can create this file manually in Excel/Notepad or using a simple Python script as shown below.
View Solution & Output
Library.csv (Content):
Book_No,Book_Title,Subject,Author,Price
101,C Programming,Programming,Dennis Ritchie,450.0
102,Python Basics,Programming,Guido van Rossum,550.0
103,Modern RDBMS,Database,C.J. Date,750.0
104,Operating Systems,Systems,Silberschatz,800.0
105,Networking,Networking,Tanenbaum,600.0
106,Data Structures,Algorithms,Sartaj Sahni,500.0
107,AI Fundamentals,AI,Stuart Russell,950.0
108,Web Design,Web,Jon Duckett,400.0
109,Java complete ref,Programming,Herbert Schildt,700.0
110,Software Engg,Management,Pressman,650.0
Step-by-Step Explanation:
- Initialization: Identify the required column headers for library data.
- Logic Flow: List at least 10 book records with their details in CSV format.
- Completion: Save the content as 'Library.csv' for future database import.
2. Database Table Creation
Define the Books table structure in SQLite.
View Solution & Output
import sqlite3
conn = sqlite3.connect('library.db')
cursor = conn.cursor()
cursor.execute('''
CREATE TABLE IF NOT EXISTS Books (
Book_No INTEGER PRIMARY KEY,
Book_Title TEXT NOT NULL,
Subject TEXT,
Author TEXT,
Price REAL
)
''')
print("Books table created.")
conn.close()
Step-by-Step Explanation:
- Initialization: Import
sqlite3and connect to 'library.db'. - Logic Flow: Execute a
CREATE TABLEquery for theBookstable with a primary key. - Completion: Print a success message and close the database connection.
3. Data Import Script
Write the logic to read the CSV and insert it into the database.
Use csv.DictReader to map column headers to dictionary keys, making the insertion logic cleaner.
View Solution & Output
import sqlite3
import csv
# [1] Setup Database
conn = sqlite3.connect('library.db')
cursor = conn.cursor()
# [2] Read CSV and Import
try:
with open('Library.csv', 'r') as f:
reader = csv.reader(f)
next(reader) # Skip header row
for row in reader:
cursor.execute('INSERT INTO Books VALUES (?, ?, ?, ?, ?)', row)
conn.commit()
print("Data imported successfully from CSV to SQLite.")
except FileNotFoundError:
print("Error: Library.csv not found!")
finally:
conn.close()
Step-by-Step Explanation:
- Initialization: Import
sqlite3andcsvand open 'Library.csv' for reading. - Logic Flow: Use
next(reader)to skip the header and iterate through each row to execute anINSERTstatement. - Completion: Commit the changes to the database and close the connection.
Concept Deep Dive: Data Migration
In real-world applications, data is often moved from legacy systems (like CSV/Excel) to relational databases (like SQLite/MySQL). This process involves reading row-by-row, validating data types, and using transactions (commit) to ensure data integrity.
Q2: Viva Preparation
Max Marks: 5
Potential Viva Questions
- Q: Why use
next(reader)?- A: It skips the header row of the CSV so you don't try to insert the column names as actual data records.
- Q: What happens if
Book_Nois a duplicate?- A: Since
Book_Nois thePRIMARY KEY, SQLite will raise anIntegrityErrorif you try to insert a duplicate.
- A: Since
- Q: How can you check if the import was successful?
- A: By running a
SELECT count(*) FROM Booksquery orSELECT * FROM Booksto view the data.
- A: By running a
- Q: What is the purpose of the
try-except-finallyblock?- A: It provides robust error handling, ensuring the database connection is closed even if an error occurs during file reading.
- Q: Can you import a CSV without the
csvmodule?- A: Yes, by reading the file line-by-line and using
split(','), but thecsvmodule is safer as it handles quotes and delimiters better.
- A: Yes, by reading the file line-by-line and using
- Q: Why is
Pricestored asREAL?- A: To allow for currency values with decimal points.
- Encoding Issues: If your CSV contains special characters, specify
encoding='utf-8'in theopen()function. - Header Mismatch: Ensure the number of columns in your CSV exactly matches the columns in your
INSERTstatement.
Quick Navigation
Related Solutions
| Set | Link |
|---|---|
| Set A | Solutions |
| Set B | Solutions |
| Set C | Solutions |
| Set D | Current Page |
| Set E | Solutions |
| Set F | Solutions |
Last Updated: April 2026