Skip to main content

How to Run a SQL Statement in Python with sqlite3 execute()

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

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​

Syntax
cursor = connection.execute("<SQL statement>")
for row in cursor:
<use row>
PartWhat it means
connectionAn open connection, from sqlite3.connect()
"<SQL statement>"One SQL statement, as a string
cursorThe 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​

run_sql.py
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()

The run_sql.py script in a macOS-style code window: it opens an in-memory database, creates a books table, inserts two books with two execute calls, then loops over a SELECT and prints each row.

  • ":memory:" opens a database that lives only while the script runs, so the example leaves no file behind.
  • Each execute() call runs one statement: the CREATE TABLE, then each INSERT.
  • The SELECT returns a cursor, and for row in ... reads its rows one by one.

Step 2: Run It​

Terminal
uv run python run_sql.py
Output
('The Glass Orchard', 2019)
('Rivers of Salt', 2023)

The output in a terminal: two tuples, The Glass Orchard 2019 and 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​

MistakeWhat happensDo this instead
Two statements in one execute() callsqlite3.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 fileCall connection.commit() before close()
Printing what execute() returns, expecting rowsIt prints <sqlite3.Cursor object at 0x...>Loop over the cursor, or call fetchall() on it

Troubleshooting​

SymptomLikely causeFix
Rows you inserted are missing after the script endsThe changes were never committedAdd 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​