Skip to main content

Helper module for simple sqlite3 use cases

Project description

ansqlite

A python3 module to assist in small sqlite3 database use cases.

Install

python3 -m pip install ansqlite

Usage

from ansqlite import Database, Datatype, PrimaryKeyType

tablename = 'tablename'
 
db = Database(
    database_path='/path/to/database/file.db',
    schemas={
        tablename: [
            {
                "name": "timestamp",
                "datatype": Datatype.INTEGER,
                "primary_key": PrimaryKeyType.Descending,
            },
            {
                "name": "value",
                "datatype": Datatype.REAL,
            },            
        ],
        'othertablename': [
            {
                "name": "hash",
                "datatype": Datatype.TEXT,
                "primary_key": PrimaryKeyType.Ascending,
            },
            {
                "name": "text",
                "datatype": Datatype.TEXT,
            },            
        ],        
    }
)

db.insert_data(
    table_name=tablename,
    data=[
        {'timestamp': 1699304400, 'value': 4.496},
        {'timestamp': 1699300800, 'value': 6.812},
        {'timestamp': 1699297200, 'value': 7.847},
        {'timestamp': 1699293600, 'value': 9.548}
    ]
)

db.execute_and_commit(
    sql=f'UPDATE {tablename} SET value=1.00 WHERE timestamp=1699300800;',
    errmsg='Failed to set value'
)

rows = db.execute_and_fetchall(
    sql=f'SELECT * FROM {tablename} where timestamp >= 1699297200 and timestamp < 1699304400 limit 10;',
    errmsg='Failed to retrieve data'
  )

print(rows)

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

ansqlite-0.3.0.tar.gz (4.2 kB view hashes)

Uploaded Source

Built Distribution

ansqlite-0.3.0-py3-none-any.whl (4.9 kB view hashes)

Uploaded Python 3

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