cursor.execute("SELECT name, email FROM users WHERE age > ?", (28,)) # Fetch all matching results results = cursor.fetchall() for row in results: print(f"Name: row[0], Email: row[1]") Use code with caution. 5. Updating and Deleting
This will return all the rows in the users table.
Leo opened the spreadsheet. 500,000 rows. Excel froze. His soul left his body. sqlite3 tutorial
# Inserting a single row user_data = ("Alice", "alice@example.com", 30) cursor.execute("INSERT INTO users (name, email, age) VALUES (?, ?, ?)", user_data) # Inserting multiple rows many_users = [ ("Bob", "bob@example.com", 25), ("Charlie", "charlie@example.com", 35) ] cursor.executemany("INSERT INTO users (name, email, age) VALUES (?, ?, ?)", many_users) connection.commit() Use code with caution. 4. Fetching and Reading Data
Leo typed:
Sam sent him a file: company.db . "That's your new best friend. Now, open your terminal."
CREATE VIEW user_emails AS SELECT email FROM users; cursor
import sqlite3 # Connect to a database file (or :memory: for a temporary DB) connection = sqlite3.connect('my_app.db') # Create a cursor object to execute commands cursor = connection.cursor() Use code with caution. 2. Creating Your First Table
It is important to close the connection once you are finished to free up resources. Alternatively, you can use the connection as a to handle commits automatically, though you must still manually close the connection at the end. Leo opened the spreadsheet
"To dig through data without blowing up your laptop. Stop using Excel like a sledgehammer. Use SQLite. It’s a database that lives in a single file. No server. No password. Just you and the data."