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.PARSE_COLNAMES - Complete Guide Python sqlite3.PARSE_COLNAMES ConstantLast modified April 15, 2025
This comprehensive guide explores Python's Basic Definitions
The
Key characteristics: it's used with Basic PARSE_COLNAMES Usage
Here's a simple example showing how to enable column name parsing with
basic_parse_colnames.py
import sqlite3
with sqlite3.connect(':memory:', detect_types=sqlite3.PARSE_COLNAMES) as conn:
cursor = conn.cursor()
cursor.execute('''CREATE TABLE test (id INTEGER, data TEXT)''')
cursor.execute("INSERT INTO test VALUES (1, 'Sample data')")
# Query with column name annotation for type conversion
cursor.execute('''SELECT data AS "data [str]" FROM test''')
row = cursor.fetchone()
print(type(row[0])) # <class 'str'>
This example creates an in-memory database with
The type in brackets (like Date Type Conversion
This example demonstrates using date_conversion.py
import sqlite3
import datetime
def adapt_date(date):
return date.isoformat()
def convert_date(value):
return datetime.date.fromisoformat(value.decode())
# Register type adapters
sqlite3.register_adapter(datetime.date, adapt_date)
sqlite3.register_converter("date", convert_date)
with sqlite3.connect(':memory:',
detect_types=sqlite3.PARSE_COLNAMES) as conn:
cursor = conn.cursor()
cursor.execute('''CREATE TABLE events
(id INTEGER, event_date TEXT)''')
today = datetime.date.today()
cursor.execute("INSERT INTO events VALUES (1, ?)", (today,))
# Query with date type conversion
cursor.execute('''SELECT event_date AS "event_date [date]"
FROM events''')
row = cursor.fetchone()
print(type(row[0]), row[0]) # <class 'datetime.date'> YYYY-MM-DD
We first register adapter and converter functions for This pattern is useful for working with dates in databases while maintaining proper Python date objects in your application logic. Custom Type Conversion
This example shows how to use custom_type.py
import sqlite3
class Point:
def __init__(self, x, y):
self.x = x
self.y = y
def __repr__(self):
return f"Point({self.x}, {self.y})"
def adapt_point(point):
return f"{point.x};{point.y}".encode()
def convert_point(value):
x, y = value.decode().split(';')
return Point(float(x), float(y))
# Register custom type
sqlite3.register_adapter(Point, adapt_point)
sqlite3.register_converter("point", convert_point)
with sqlite3.connect(':memory:',
detect_types=sqlite3.PARSE_COLNAMES) as conn:
cursor = conn.cursor()
cursor.execute('''CREATE TABLE shapes
(id INTEGER, coordinates TEXT)''')
p = Point(3.5, 4.2)
cursor.execute("INSERT INTO shapes VALUES (1, ?)", (p,))
# Query with custom type conversion
cursor.execute('''SELECT coordinates AS "coordinates [point]"
FROM shapes''')
row = cursor.fetchone()
print(type(row[0]), row[0]) # <class '__main__.Point'> Point(3.5, 4.2)
We define a custom This technique is powerful for storing complex Python objects in SQLite while maintaining their original type when retrieved. Multiple Column Conversion
This example demonstrates converting multiple columns in a single query using
multi_column.py
import sqlite3
import datetime
# Register date converters
sqlite3.register_adapter(datetime.date, lambda d: d.isoformat())
sqlite3.register_converter("date", lambda b: datetime.date.fromisoformat(b.decode()))
with sqlite3.connect(':memory:',
detect_types=sqlite3.PARSE_COLNAMES) as conn:
cursor = conn.cursor()
cursor.execute('''CREATE TABLE records
(id INTEGER, record_date TEXT, value REAL)''')
cursor.execute("INSERT INTO records VALUES (1, ?, 42.5)",
(datetime.date.today(),))
# Convert multiple columns with different types
cursor.execute('''SELECT
id AS "id [int]",
record_date AS "record_date [date]",
value AS "value [float]"
FROM records''')
row = cursor.fetchone()
print(type(row[0]), type(row[1]), type(row[2]))
# <class 'int'> <class 'datetime.date'> <class 'float'>
The query converts three columns to different Python types: integer, date, and
float. Each column uses the This approach provides fine-grained control over the types of each returned column, which can simplify application code that processes query results. Combining with PARSE_DECLTYPES
This example shows how combined_parsing.py
import sqlite3
import datetime
# Register type handlers
sqlite3.register_adapter(datetime.date, lambda d: d.isoformat())
sqlite3.register_converter("date", lambda b: datetime.date.fromisoformat(b.decode()))
# Use both PARSE_DECLTYPES and PARSE_COLNAMES
with sqlite3.connect(':memory:',
detect_types=sqlite3.PARSE_DECLTYPES |
sqlite3.PARSE_COLNAMES) as conn:
cursor = conn.cursor()
# Declare column type in table definition
cursor.execute('''CREATE TABLE events
(id INTEGER, event_date DATE)''')
today = datetime.date.today()
cursor.execute("INSERT INTO events VALUES (1, ?)", (today,))
# Override declared type with column name annotation
cursor.execute('''SELECT
event_date AS "event_date [str]"
FROM events''')
row = cursor.fetchone()
print(type(row[0])) # <class 'str'> (overridden by column name)
We combine both flags using bitwise OR ( This combination provides maximum flexibility: declared types for normal operations with the ability to override when needed via column names. Handling NULL Values
This example demonstrates how null_handling.py
import sqlite3
import datetime
# Register date converter
sqlite3.register_converter("date", lambda b: datetime.date.fromisoformat(b.decode()))
with sqlite3.connect(':memory:',
detect_types=sqlite3.PARSE_COLNAMES) as conn:
cursor = conn.cursor()
cursor.execute('''CREATE TABLE tasks
(id INTEGER, due_date TEXT)''')
# Insert both date and NULL values
cursor.execute("INSERT INTO tasks VALUES (1, ?)",
(datetime.date.today().isoformat(),))
cursor.execute("INSERT INTO tasks VALUES (2, NULL)")
# Query with type conversion
cursor.execute('''SELECT due_date AS "due_date [date]" FROM tasks''')
rows = cursor.fetchall()
for row in rows:
print(type(row[0]), row[0])
# <class 'datetime.date'> YYYY-MM-DD
# <class 'NoneType'> None
The example shows that NULL values in the database remain as This behavior is important to remember when writing code that processes query results with optional fields that might be NULL in the database. Complex Type Conversion
This advanced example demonstrates using json_conversion.py
import sqlite3
import json
def adapt_json(data):
return json.dumps(data).encode()
def convert_json(value):
return json.loads(value.decode())
# Register JSON converter
sqlite3.register_adapter(dict, adapt_json)
sqlite3.register_converter("json", convert_json)
with sqlite3.connect(':memory:',
detect_types=sqlite3.PARSE_COLNAMES) as conn:
cursor = conn.cursor()
cursor.execute('''CREATE TABLE configs
(id INTEGER, settings TEXT)''')
config = {'theme': 'dark', 'notifications': True}
cursor.execute("INSERT INTO configs VALUES (1, ?)", (config,))
# Query with JSON conversion
cursor.execute('''SELECT settings AS "settings [json]"
FROM configs''')
row = cursor.fetchone()
print(type(row[0]), row[0]['theme']) # <class 'dict'> dark
We register converters for Python dictionaries to JSON strings and back. The
query uses This pattern is extremely useful for storing structured configuration data or other complex objects in SQLite while maintaining easy access to the original Python data structure. 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 01402520 — 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. |