Insert, Select, Update, Delete
Run CRUD operations.
CRUD Operations
The four basic database actions are Create, Read, Update, Delete (CRUD). In SQL these map to INSERT, SELECT, UPDATE, and DELETE.
import sqlite3
conn = sqlite3.connect(':memory:')
cur = conn.cursor()
cur.execute('CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT, age INTEGER)')
print('Ready for CRUD')
conn.close()Inserting a Row
Use INSERT INTO to add a row. List the columns, then the matching values.
import sqlite3
conn = sqlite3.connect(':memory:')
cur = conn.cursor()
cur.execute('CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT, age INTEGER)')
cur.execute("INSERT INTO users (name, age) VALUES ('Alice', 30)")
print('Inserted Alice')
conn.close()All lessons in this course
- Connecting and Creating Tables
- Insert, Select, Update, Delete
- Parameterized Queries
- Transactions and Context Managers