How to Run a SQL Statement in Python with sqlite3 execute()
Once a SQLite database is open, everything you do with it is a SQL statement: create a table, add a row, read rows back. connection.execute() sends one statement to the database. This guide creates a table of books, fills it and reads it, with real output from Python 3.15.2.
Syntax
cursor = connection.execute("<SQL statement>")
for row in cursor:
<use row>
| Part | What it means |
|---|---|
connection | An open connection, from sqlite3.connect() |
"<SQL statement>" | One SQL statement, as a string |
cursor | The Cursor that ran the statement. For a SELECT, looping over it gives one row at a time |
Python's sqlite3 documentation describes connection.execute() as a shortcut: "Create a new Cursor object and call execute() on it with the given sql and parameters. Return the new cursor object." And the cursor's own execute(): "Execute a single SQL statement".
Prerequisites
- uv and Python. This guide uses uv 0.13.10 with Python 3.15.2, in a project made with
uv init.
Step 1: Write the Script
import sqlite3
connection = sqlite3.connect(":memory:")
connection.execute("CREATE TABLE books (title TEXT, year INTEGER)")
connection.execute("INSERT INTO books VALUES ('The Glass Orchard', 2019)")
connection.execute("INSERT INTO books VALUES ('Rivers of Salt', 2023)")
for row in connection.execute("SELECT title, year FROM books"):
print(row)
connection.close()

":memory:"opens a database that lives only while the script runs, so the example leaves no file behind.- Each
execute()call runs one statement: theCREATE TABLE, then eachINSERT. - The
SELECTreturns a cursor, andfor row in ...reads its rows one by one.
Step 2: Run It
uv run python run_sql.py
('The Glass Orchard', 2019)
('Rivers of Salt', 2023)

Each row arrived as a tuple, with the columns in the order the SELECT named them: title first, then year. The text column became a Python str and the INTEGER column an int.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
Two statements in one execute() call | sqlite3.ProgrammingError: You can only execute one statement at a time. | Call execute() once per statement, or use executescript() for a script |
Inserting into a database file and closing without commit() | The rows are gone the next time you open the file | Call connection.commit() before close() |
Printing what execute() returns, expecting rows | It prints <sqlite3.Cursor object at 0x...> | Loop over the cursor, or call fetchall() on it |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
| Rows you inserted are missing after the script ends | The changes were never committed | Add connection.commit() after the inserts |
FAQ
Do I need to create a cursor first?
No. connection.execute() creates one for you and returns it, as the documentation quoted above says. You can also call connection.cursor() and use the cursor's execute(): the result is the same.
Why doesn't this example call commit()?
Its database is ":memory:" and disappears when the script ends, so there's nothing to keep. With a file, Python's documentation for close() says: "Make sure to commit() before closing to avoid losing pending changes."
Conclusion
Four execute() calls created a table, added two books and read them back: each call ran one statement, and the SELECT gave a cursor whose rows came out as tuples.
Additional Resources
- Connection.execute — the shortcut on the connection.
- Cursor.execute — running one statement, with placeholders.
- Connection.commit — saving changes to the file.
