keepcalculator

06 Numbers and Powers

Python sqlite3 Module: Database How-To

Card No. 06·55490 · Updated · Standard formula, shown below

This guide walks you through using Python’s built-in sqlite3 module to create, query, and manage a SQLite database. You’ll learn how to connect to a database file, create tables, insert and retrieve data, and handle common pitfalls—all with practical code examples you can run immediately.

What You’ll Need

  • Python 3.x installed (the sqlite3 module is part of the standard library, so no extra installation is required).
  • A text editor or IDE to write your Python scripts.
  • Basic familiarity with Python syntax and SQL (SELECT, INSERT, etc.).

Step-by-Step

  1. Import the module and connect to a database. Use sqlite3.connect('mydb.db'). If the file doesn’t exist, SQLite creates it. Use ':memory:' for a temporary in-RAM database.
  2. Create a cursor object. After connecting, call conn.cursor() to get a cursor. The cursor executes SQL statements and fetches results.
  3. Create a table. Use cursor.execute('''CREATE TABLE IF NOT EXISTS users (id INTEGER PRIMARY KEY, name TEXT, age INTEGER)'''). The IF NOT EXISTS prevents errors if the table already exists.
  4. Insert data. Use parameterized queries to avoid SQL injection: cursor.execute('INSERT INTO users (name, age) VALUES (?, ?)', ('Alice', 30)). Never use string formatting for values.
  5. Commit the transaction. After any INSERT, UPDATE, or DELETE, call conn.commit() to save changes. Without it, changes are lost when the connection closes.
  6. Query data. Use cursor.execute('SELECT * FROM users') then rows = cursor.fetchall() to get all rows as a list of tuples. Or use fetchone() for a single row and fetchmany(n) for a batch.
  7. Use context managers (optional but recommended). Wrap operations in with sqlite3.connect('mydb.db') as conn: to auto-commit on success and rollback on exceptions. Note that the with block does not close the connection; you still need to call conn.close() after the block.
  8. Close the connection. Call conn.close() when done to free resources. This is especially important in long-running applications.

Common Mistakes

  • Forgetting to commit. Changes are not saved until conn.commit() is called. This is the most frequent error.
  • Using string formatting for values. cursor.execute(f"INSERT INTO users VALUES ({name})") is unsafe and error-prone. Always use ? placeholders.
  • Not closing the connection. Leaving connections open can lock the database file, especially on Windows.
  • Assuming fetchall() returns dictionaries. By default, rows are tuples. Use conn.row_factory = sqlite3.Row to access columns by name.
  • Confusing commit() and close(). close() does not commit; you must commit before closing if you want to save changes.

Tips

  • Use parameterized queries everywhere—they protect against SQL injection and handle quoting automatically.
  • Set conn.row_factory = sqlite3.Row to get rows that behave like dictionaries, making code more readable.
  • Use PRAGMA foreign_keys = ON if you use foreign keys; SQLite disables them by default.
  • Wrap multiple operations in a transaction for atomicity. Start with conn.execute('BEGIN') and commit at the end.
  • Use EXPLAIN QUERY PLAN to debug slow queries.
  • Back up your database file regularly—SQLite is a single file, so copying it while the connection is closed is a simple backup.

Troubleshooting

  • “sqlite3.OperationalError: no such table” – You likely forgot to create the table, or you’re connecting to a different file. Check the database path and run the CREATE TABLE statement.
  • “sqlite3.IntegrityError: UNIQUE constraint failed” – You tried to insert a duplicate value in a column with a UNIQUE or PRIMARY KEY constraint. Check your data and handle duplicates gracefully.
  • “sqlite3.OperationalError: database is locked” – Another process has the database open. Ensure all connections are closed, or use a timeout: sqlite3.connect('mydb.db', timeout=10).
  • Changes not saved after program exits – You forgot to call conn.commit(). Add it after every write operation.
  • “sqlite3.ProgrammingError: Cannot operate on a closed database” – You tried to execute a query after closing the connection. Re-open the connection or move the close call to the end.

Frequently Asked Questions

Do I need to install anything to use sqlite3?

No. The sqlite3 module is included in Python’s standard library. Just import sqlite3 and you’re ready.

Can I use sqlite3 with an in-memory database?

Yes. Pass ':memory:' to sqlite3.connect(). The database exists only during the connection and is lost when it closes.

How do I handle NULL values?

SQLite treats NULL as missing data. In Python, NULL is represented as None. When inserting, use None for NULL; when fetching, check for None.

Is sqlite3 thread-safe?

By default, connections are not shared across threads. Create a new connection per thread, or use check_same_thread=False (not recommended for concurrent writes).

How do I see the SQL that SQLite executes?

Enable trace: conn.set_trace_callback(print) to print every SQL statement sent to the database.

Can I use sqlite3 with pandas?

Yes. Use pd.read_sql_query('SELECT * FROM table', conn) to load data into a DataFrame, and df.to_sql('table', conn, if_exists='replace') to write.

Related

Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.

More cards in this drawer

The whole Numbers and Powers drawer →