sqlite3.connect(path) opens, creating if necessary, a SQLite database file, or an in-memory database if you pass a special in-memory identifier, useful for tests that shouldn't touch the filesystem. A cursor, obtained from the connection, is used to execute SQL statements and fetch results; execute() takes a SQL string and, separately, a tuple of parameters to safely substitute into placeholders, which is the correct way to include variable data in a query, avoiding SQL injection. Changes made by INSERT/UPDATE/DELETE statements aren't persisted to the database file until you call conn.commit().
1Understanding sqlite3 Module
sqlite3.connect(path) opens, creating if necessary, a SQLite database file, or an in-memory database if you pass a special in-memory identifier, useful for tests that shouldn't touch the filesystem. A cursor, obtained from the connection, is used to execute SQL statements and fetch results; execute() takes a SQL string and, separately, a tuple of parameters to safely substitute into placeholders, which is the correct way to include variable data in a query, avoiding SQL injection. Changes made by INSERT/UPDATE/DELETE statements aren't persisted to the database file until you call conn.commit().
Always pass variable data as parameters to execute(), using placeholder syntax, never by formatting it directly into the SQL string — building SQL with string formatting or f-strings is exactly how SQL injection vulnerabilities happen.
import sqlite3
conn = sqlite3.connect(":memory:")
cursor = conn.cursor()
cursor.execute("CREATE TABLE users (id INTEGER, name TEXT)")
cursor.execute("INSERT INTO users VALUES (1, 'Alice')")
conn.commit()
cursor.execute("SELECT * FROM users")
print(cursor.fetchall())2Practical Example
Here is a real-world application of sqlite3 Module showing how it is used in production Python code.
import sqlite3
conn = sqlite3.connect(":memory:")
cursor = conn.cursor()
cursor.execute("CREATE TABLE users (id INTEGER, name TEXT)")
user_id = 1
name = "Bob"
cursor.execute("INSERT INTO users VALUES (?, ?)", (user_id, name))
conn.commit()
print(cursor.execute("SELECT name FROM users WHERE id = ?", (1,)).fetchone())3Best Practices
Follow these guidelines when working with sqlite3 Module:
1. Always use parameterized queries, execute(sql, params), for any variable data, never string formatting or concatenation into the SQL text
2. Call conn.commit() after INSERT/UPDATE/DELETE statements, or changes won't be saved to the database file
3. Use a with block, or explicit close(), on the connection so it's properly closed and any pending transaction is handled
Tip: Always pass variable data as parameters to execute(), using placeholder syntax, never by formatting it directly into the SQL string — building SQL with string formatting or f-strings is exactly how SQL injection vulnerabilities happen.
import sqlite3
conn = sqlite3.connect(":memory:")
cursor = conn.cursor()
cursor.execute("CREATE TABLE users (id INTEGER, name TEXT)")
cursor.execute("INSERT INTO users VALUES (1, 'Alice')")
conn.commit()
cursor.execute("SELECT * FROM users")
print(cursor.fetchall())