This document details my learnings on how to use SQL within Python using the SQLite library. It covers database connection, table creation, data insertion, querying, updating, and deleting records. The document provides detailed explanations and code examples to illustrate the concepts and techniques learned.


🔧 1. Setting Up the Environment

✅ Importing the SQLite Library:

To work with SQLite databases in Python, import the sqlite3 module.

import sqlite3 as sq

🔗 2. Database Connection

✅ Connecting to a Database:

Use sq.connect() to establish a connection to a database.

If the database does not exist, it will be created automatically.

db = sq.connect("data.db")

🗃️ 3. Creating Tables

✅ Executing SQL Commands:

Use the execute() method to run SQL commands.

This can be used to create tables if they do not already exist.

db.execute("CREATE TABLE IF NOT EXISTS user_info (name TEXT, age INTEGER, address TEXT)")

❌ 4. Closing the Database