Skip to main content

Create an SQLite table if missing or check that the existing table matches the given creation statement.

Project description


title: README author: Jan-Michael Rye

Synopsis

A Python package that provides a simple utility function for creating SQLite tables. The function will compare an existing table's SQL statement from the sqlite_master table to the one expected for a given SQL table creation statement. If the existing table does not match the expected SQL statement then it can either be dropped an recreated, or a ValueError can be raised.

The primary purpose of this function is to facilitate the creation of tables for caching temporary data that can be discarded, such as memoizing calls to remote server APIs.

Links

GitLab

Usage

import sqlite3
from sqlite3_create_and_check_table import create_and_check_table

sql = "CREATE TABLE test (prim TEXT PRIMARY KEY, int INTEGER, blob BLOB NON NULL)"
conn = sqlite3.connect("/path/to/some/sqlite3/db.sqlite")
with conn as cur:
    # Create the table with the given SQL statement. If a table with the same
    # name exists but differs from what would result from the given SQL table
    # creation statement, it will be dropped and recreated to match the given
    # statement.
    # If drop is set to False then a ValueError will be raised instead.
    create_and_check_table(cur, sql, drop=True)

The package also provides a row factory function for returning rows in the table as dicts as well as an sqlite3 cursor context manager for temporarily setting the the row factory to the provided dict factory:

import sqlite3
from sqlite3_create_and_check_table import dict_factory, dict_factory_cursor

conn = sqlite3.connect("/path/to/some/sqlite3/db.sqlite")
# Set the row factory within a context and restore the previous row factory when
# leaving the context.
with dict_factory_cursor(conn) as cur:
    # cur.execute(...)

# Set the row factory for all queries.
conn.row_factory = dict_factory

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

sqlite3_create_and_check_table-2024.1.tar.gz (7.8 kB view details)

Uploaded Source

Built Distribution

File details

Details for the file sqlite3_create_and_check_table-2024.1.tar.gz.

File metadata

File hashes

Hashes for sqlite3_create_and_check_table-2024.1.tar.gz
Algorithm Hash digest
SHA256 434b0ce6171fac6f88e5d40374e50fb98620dd929ca1e674d63c19b88b203ad0
MD5 a1956546a6f03f186bbcb9c9e8e69fab
BLAKE2b-256 eea1008d11bbf6b251daa4a6cb13f39ceb8ca6671f652ac97d0ae9c5a44172ca

See more details on using hashes here.

File details

Details for the file sqlite3_create_and_check_table-2024.1-py3-none-any.whl.

File metadata

File hashes

Hashes for sqlite3_create_and_check_table-2024.1-py3-none-any.whl
Algorithm Hash digest
SHA256 7800e8b132b88f8b1025fb3153c13dc97098b0c55831ab2a903ebb5ac26a13d2
MD5 e92837ac3524c0d6a6d854165c15d8c7
BLAKE2b-256 9f235a455cef334789669828fc8909a922ac7486378a5c9b1239f96772e8b882

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page