0% found this document useful (0 votes)
15 views1 page

Rishit Sharma S22 108 Python EXP6

The document outlines an experiment aimed at demonstrating CRUD operations using Python's sqlite3 module. It explains the features of SQLite and provides a code example for creating, reading, updating, and deleting records in a database. The experiment concludes with the successful completion of the objectives outlined in the lab work.
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)
15 views1 page

Rishit Sharma S22 108 Python EXP6

The document outlines an experiment aimed at demonstrating CRUD operations using Python's sqlite3 module. It explains the features of SQLite and provides a code example for creating, reading, updating, and deleting records in a database. The experiment concludes with the successful completion of the objectives outlined in the lab work.
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/ 1

Python Experiment No: - 06

Aim: To study and implement program on demonstrating CRUD (create, read, update and delete)
operations on database (SQLite/ MySQL) using python

Theory:
sqlite3 is a built-in Python module that provides an interface for interacting with SQLite databases.
SQLite is a lightweight, self-contained, serverless database engine that stores data in a single file. It is
commonly used for small to medium-sized applications, including mobile apps and desktop software.
Features of sqlite3 in Python:
• No external dependencies: It comes pre-installed with Python.
• Lightweight: Doesn't require a separate server or setup.
• ACID-compliant: Ensures reliability through transactions.
• Uses SQL syntax: Supports standard SQL commands.

Code:
import sqlite3
# Create a database connection
conn = sqlite3.connect('example.db')
c = conn.cursor()

# 1. CREATE: Creating a table


c.execute('''CREATE TABLE IF NOT EXISTS users
(id INTEGER PRIMARY KEY, name TEXT, age INTEGER)''')

# 2. INSERT: Inserting data into the table


c.execute("INSERT INTO users (name, age) VALUES ('Alice', 30)")
c.execute("INSERT INTO users (name, age) VALUES ('Bob', 25)")
conn.commit()

# 3. READ: Fetching data from the table


c.execute("SELECT * FROM users")
rows = c.fetchall()
for row in rows:
print(row)

# 4. UPDATE: Updating a record


c.execute("UPDATE users SET age = 31 WHERE name = 'Alice'")
conn.commit()

# 5. DELETE: Deleting a record


c.execute("DELETE FROM users WHERE name = 'Bob'")
conn.commit()

# Fetch updated data


c.execute("SELECT * FROM users")
rows = c.fetchall()
for row in rows:
print(row)

# Close the connection


conn.close()
Output:

Conclusion: Hence LO1, LO3(Exp-6) has been completed.


1
TSEC Batch:-S22 Name & Roll No:-Rishit Sharma (108)

You might also like