0Pricing
Python Academy · Lesson

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

  1. Connecting and Creating Tables
  2. Insert, Select, Update, Delete
  3. Parameterized Queries
  4. Transactions and Context Managers
← Back to Python Academy