Understanding Database Transactions and Commits
The connection maintains the link to the database. The cursor is the working tool created from that connection. Treating them as the same object makes it harder to understand how database operations work.
The Path from Python to a Database
A database operation in Python involves more than writing an SQL command. Python first needs a connection to the database. From that connection, you create a cursor. The cursor then sends SQL commands to the database and retrieves results. This connection-cursor relationship is the foundation for database work in Python.
The connection maintains the link to the database. The cursor is the working tool created from that connection. Treating them as the same object makes it harder to understand how database operations work.
Opening the Database Connection
The first step is to establish a database connection with sqlite3.connect(filename). The filename identifies the database file being connected to. A connection creates the link between the Python program and the database. The same general idea applies when the link is to a local database file or to a remote database server.
In this example, conn refers to the connection created by sqlite3.connect(). The connection is not the cursor and is not itself an SQL command. It is the link through which later database work takes place.
Creating and Using a Cursor
After creating the connection, create a cursor with conn.cursor(). The cursor acts as the tool for executing SQL commands on the database. You call the cursor's execute() method to send an SQL command through the connection to the database. When a command produces results, the cursor is also involved in retrieving those results.
The first line creates a cursor from conn. The second line uses that cursor to execute an SQL command. The cursor does not replace the connection: it operates through the connection that created it.
From Execution to Commit
Executing an SQL command and committing a transaction are related but distinct steps. The cursor executes the command, while the connection manages the link and the transaction that contains the database work. A commit confirms the changes in the transaction so that the executed changes become permanent in the database.
A Complete Database Operation
Connect to a database, create the tool for SQL commands, execute a command, commit the changes, and close the connection.
- 1Connect
sqlite3.connect("school.db")creates a connection to the database file. - 2Create the cursor
conn.cursor()creates the cursor that will execute SQL commands. - 3Execute
cursor.execute(...)sends an SQL command through the cursor. - 4Commit
conn.commit()confirms the transaction's changes so they become permanent. - 5Close
conn.close()closes the connection when the database work is finished.
The program has followed the connection, cursor, execution, commit, and cleanup sequence.
SQL as the Command Language
SQL is a standardized language for database commands. Python provides the connection and cursor objects, but the command passed to execute() is written in SQL. SQL keywords are conventionally written in uppercase, while names defined by the user are written in lowercase.
| Part of the command | Example | Role |
|---|---|---|
| SQL keyword | CREATE TABLE | A standardized database instruction |
| User-defined name | students | A name chosen for the database object |
| Column name | name | A user-defined name inside the table |
The example separates SQL keywords from names chosen by the programmer.
Mistakes That Break the Workflow
Common mistake Trying to execute SQL directly on the connection instead of using a cursor.
What it looks likeconn.execute("CREATE TABLE students (name TEXT)")Why it failsThe cursor is the tool identified for executing SQL commands.
Do this insteadCreate a cursor with conn.cursor() and call cursor.execute(...).
Common mistake Treating the cursor as the database connection.
What it looks likeThe cursor is described as the link to the database.
Why it failsThe connection maintains the database link, while the cursor sends commands and retrieves results.
Do this insteadKeep the roles separate: conn represents the connection and cursor represents the SQL operation tool.
Common mistake Assuming execute() alone makes changes permanent.
What it looks likecursor.execute("CREATE TABLE students (name TEXT)")Why it failsExecution sends the SQL command, but committing confirms the transaction's changes.
Do this insteadUse conn.commit() after the database changes that should become permanent.
Common mistake Leaving the connection open after finishing.
What it looks likeThe program performs database work and never closes conn.
Why it failsAn open connection can keep resources in use and can contribute to locking the database file.
Do this insteadCall conn.close() when the database work is done.
Common mistake Confusing SQL keywords with user-defined names.
What it looks likeWriting a command without distinguishing standardized SQL terms from names chosen by the programmer.
Why it failsSQL keywords and user-defined names have different roles in a command.
Do this insteadUse uppercase for SQL keywords and lowercase for user-defined names.
Practice the Sequence
Put these database actions in the correct order: create a cursor, close the connection, connect to the database, execute an SQL command, and commit the changes.
Hints
- The cursor must come from an existing connection.
- The SQL command is sent through the cursor.
- Closing belongs after the database work is finished.
What do you think happens?
A program has called cursor.execute() for a database change but has not called conn.commit(). Has the change been confirmed as permanent by the transaction?
Reveal answer
No, the change still needs to be committed
The cursor executes the SQL command, while the connection commits the transaction's changes so they become permanent.
The Complete Mental Model
- 1
sqlite3.connect(filename)creates the connection between Python and a database file or server. - 2
conn.cursor()creates a cursor from the connection. - 3The cursor's
execute()method sends SQL commands to the database and helps retrieve results. - 4The connection and cursor have different roles: the connection maintains the link, while the cursor performs SQL operations through it.
- 5A commit confirms transaction changes as permanent, and the connection should be closed when the work is complete.
Key Takeaways
- A database connection is the link between Python and the database.
- A cursor is created from the connection and is used to execute SQL commands.
- SQL provides standardized commands, with keywords conventionally written in uppercase and user-defined names in lowercase.
- Executing a command and committing its transaction are separate steps.
- Close the connection after database work to free resources and avoid locking the database file.