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.
To work with SQLite databases in Python, import the sqlite3
module.
import sqlite3 as sq
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")
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)")