GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing zetcode.com — Python sqlite3.ProgrammingError - Complete Guide Python sqlite3.ProgrammingError ExceptionLast modified April 15, 2025
This comprehensive guide explores Python's Basic Definitions
The Key characteristics: it signals developer mistakes like invalid SQL syntax, incorrect parameter binding, or improper resource usage. It's raised before any SQL execution reaches the database engine. Invalid SQL SyntaxThis example demonstrates how malformed SQL statements raise ProgrammingError. The error occurs during statement preparation before execution. invalid_syntax.py
import sqlite3
try:
with sqlite3.connect(':memory:') as conn:
cursor = conn.cursor()
# Missing closing parenthesis
cursor.execute("CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT")
except sqlite3.ProgrammingError as e:
print(f"ProgrammingError occurred: {e}")
The error occurs because the SQL statement is syntactically incorrect. The missing closing parenthesis triggers the exception during statement preparation. Always validate SQL syntax before execution. Use tools like SQL linters or database GUI clients to test queries independently. Incorrect Parameter BindingProgrammingError is raised when parameter binding fails due to mismatched placeholders and parameters. parameter_binding.py
import sqlite3
try:
with sqlite3.connect(':memory:') as conn:
cursor = conn.cursor()
cursor.execute("CREATE TABLE test (id INTEGER, name TEXT)")
# Mismatch between placeholders (2) and parameters (1)
cursor.execute("INSERT INTO test VALUES (?, ?)", (1,))
except sqlite3.ProgrammingError as e:
print(f"Parameter binding error: {e}")
The example shows a common mistake where the number of placeholders doesn't match the number of parameters provided. The error occurs during parameter binding. Always ensure parameter counts match placeholders. Consider using named parameters for complex queries to improve readability and reduce errors. Closed Cursor UsageAttempting to use a closed cursor raises ProgrammingError. This example demonstrates proper resource management. closed_cursor.py
import sqlite3
with sqlite3.connect(':memory:') as conn:
cursor = conn.cursor()
cursor.execute("CREATE TABLE data (value TEXT)")
cursor.close() # Explicitly close cursor
try:
# Attempt to use closed cursor
cursor.execute("INSERT INTO data VALUES ('test')")
except sqlite3.ProgrammingError as e:
print(f"Closed cursor error: {e}")
The error occurs when trying to execute SQL using a cursor that was explicitly closed. Cursors become invalid after closure and can't be reused. The with statement automatically closes cursors when the block exits. Explicit closure is only needed in specific scenarios like long-running connections. Invalid Column OperationsProgrammingError occurs when attempting operations on non-existent columns or tables. This example shows proper error handling. invalid_column.py
import sqlite3
try:
with sqlite3.connect(':memory:') as conn:
cursor = conn.cursor()
cursor.execute("CREATE TABLE products (id INTEGER, name TEXT)")
# Non-existent column 'price'
cursor.execute("SELECT price FROM products")
except sqlite3.ProgrammingError as e:
print(f"Column error: {e}")
The error occurs because the query references a column that doesn't exist in the table. The database engine detects this during statement preparation.
Always verify table schemas before writing queries. Use Transaction Management ErrorsImproper transaction management can raise ProgrammingError. This example shows correct transaction handling. transaction_error.py
import sqlite3
try:
with sqlite3.connect(':memory:') as conn:
conn.isolation_level = None # Autocommit mode
cursor = conn.cursor()
# Attempt to commit when not in transaction
conn.commit()
except sqlite3.ProgrammingError as e:
print(f"Transaction error: {e}")
The error occurs because Understand your isolation level settings. Use context managers for transactions when possible to avoid manual commit/rollback calls. Resource Cleanup ErrorsProgrammingError can occur during improper resource cleanup. This example shows correct resource management patterns. resource_cleanup.py
import sqlite3
try:
conn = sqlite3.connect(':memory:')
cursor = conn.cursor()
cursor.execute("CREATE TABLE temp (id INTEGER)")
conn.close() # Close connection first
# Attempt to use cursor after connection closed
cursor.execute("SELECT * FROM temp")
except sqlite3.ProgrammingError as e:
print(f"Resource error: {e}")
finally:
cursor.close() # Still need to close cursor
The error occurs because the cursor is used after its parent connection was closed. Cursors depend on their connection and become invalid when it closes. Always close resources in reverse order: cursors first, then connections. Better yet, use context managers to handle cleanup automatically. Type Mismatch ErrorsProgrammingError can signal type mismatches during parameter binding. This example demonstrates proper type handling. type_mismatch.py
import sqlite3
try:
with sqlite3.connect(':memory:') as conn:
cursor = conn.cursor()
cursor.execute("CREATE TABLE items (id INTEGER, data BLOB)")
# Incorrect parameter type for BLOB
cursor.execute("INSERT INTO items VALUES (?, ?)",
(1, "string instead of bytes"))
except sqlite3.ProgrammingError as e:
print(f"Type error: {e}")
The error occurs because a string is provided for a BLOB column instead of bytes. SQLite performs type checking during parameter binding. Always match Python types to SQLite column types. Convert data appropriately before binding, especially for BLOB and numeric types. Best Practices
Source ReferencesAuthorMy name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming. List all Python tutorials. |
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |