litedbc


Namelitedbc JSON
Version 0.0.2 PyPI version JSON
download
home_pageNone
SummaryLite database connector
upload_time2024-11-25 22:19:15
maintainerNone
docs_urlNone
authorNone
requires_python>=3.8
licenseMIT License Copyright (c) 2024 Pyrustic Architect Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
keywords sqlite transactional threadsafe
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
[![PyPI package version](https://img.shields.io/pypi/v/litedbc)](https://pypi.org/project/litedbc)
[![Downloads](https://static.pepy.tech/badge/litedbc)](https://pepy.tech/project/litedbc)


<!-- Intro Text -->
# LiteDBC
<b>Lightweight Database Connector</b>

This library is a streamlined wrapper for Python's [sqlite3](https://docs.python.org/3/library/sqlite3.html) module, built to provide a user-friendly and thread-safe bridge between [Jinbase](https://github.com/pyrustic/jinbase) and [SQLite](https://www.sqlite.org/index.html).

**Connecting to an embedded database:**

```python
from litedbc import LiteDBC

dbc = LiteDBC("/path/to/file.db")

# manually initialize the database with a SQL script
if dbc.is_new:
    with dbc.transaction() as cursor:
        cursor.execute_script("-- <Initialization SQL Script> --")
# ...
dbc.close()
# the line above is optional, as the connection 
# closes automatically when the program exits
```

<p align="right"><a href="#readme">Back to top</a></p>

**Providing SQL script for automatic initialization:**

```python
from litedbc import LiteDBC

INIT_SCRIPT = """BEGIN TRANSACTION;
-- Create the USER table
CREATE TABLE user (
    name TEXT PRIMARY KEY,
    age INTEGER NOT NULL);
COMMIT;"""

with LiteDBC("/path/to/file.db", init_script=INIT_SCRIPT) as dbc:
    pass
```

<p align="right"><a href="#readme">Back to top</a></p>

**Interacting with a database:**

```python
from litedbc import LiteDBC

with LiteDBC("/path/to/file.db") as dbc:
    with dbc.cursor() as cur:
        # execute a SQL statement
        statement = "INSERT INTO user (id, name) VALUES (?, ?)"
        cur.execute(statement, (42, "alex"))  # returns the 'rowcount'
        
        # query data
        cur.execute("SELECT id, name FROM user")
        
        # get the rows iteratively
        for row in cur.fetch():
            print(row)
        
        # execute a SQL script in a single transaction
        cur.executescript("-- SQL Script --")
```

<p align="right"><a href="#readme">Back to top</a></p>

**Creating a transaction context to run complex logic:**

```python
from litedbc import LiteDBC

dbc = LiteDBC("/path/to/file.db")

# creating a transaction context
with dbc.transaction() as cur:  # accepts an optional transaction mode
    # from here, everything will be executed within a single transaction
    cur.execute("SELECT COUNT(*) FROM user")
    for row in cur.fetch():
        if row[0] >= 256:
            cur.execute("DELETE FROM user WHERE id=?", (1,))

dbc.close()
```

<p align="right"><a href="#readme">Back to top</a></p>

**Miscelleaneous:**

```python
from litedbc import LiteDBC

# the constructor also accepts conn_kwargs, on_create_db, on_create_conn
dbc = LiteDBC("/path/to/file.db")  # not filename provided -> in-memory db !

# return a tuple of tables present in the database
tables = dbc.list_tables()

# return a list of ColumnInfo instances that provide rich info
# about each column, such as whether it's a primary key column or not
table_info = dbc.inspect(table)

# export the database as a SQL script
sql_script = dbc.dump(dst=None)

# safely create a backup
dbc.backup("/path/to/file.backup")

# create a new instance of Dbc for the same database file
new_dbc = dbc.copy()

# Note that the Dbc class also exposes these properties:
# .new, .conn, .in_memory, .filename, .conn_kwargs, .closed, .deleted
```

<p align="right"><a href="#readme">Back to top</a></p>


# Testing and contributing
Feel free to **open an issue** to report a bug, suggest some changes, show some useful code snippets, or discuss anything related to this project. You can also directly email [me](https://pyrustic.github.io/#contact).

## Setup your development environment
Following are instructions to setup your development environment

```bash
# create and activate a virtual environment
python -m venv venv
source venv/bin/activate

# clone the project then change into its directory
git clone https://github.com/pyrustic/litedbc.git
cd litedbc

# install the package locally (editable mode)
pip install -e .

# run tests
python -m tests

# deactivate the virtual environment
deactivate
```

<p align="right"><a href="#readme">Back to top</a></p>

# Installation
**LiteDBC** is **cross-platform**. It is built on [Ubuntu](https://ubuntu.com/download/desktop) and should work on **Python 3.8** or **newer**.

## Create and activate a virtual environment
```bash
python -m venv venv
source venv/bin/activate
```

## Install for the first time

```bash
pip install litedbc
```

## Upgrade the package
```bash
pip install litedbc --upgrade --upgrade-strategy eager
```

## Deactivate the virtual environment
```bash
deactivate
```

<p align="right"><a href="#readme">Back to top</a></p>

# About the author
Hello world, I'm Alex, a tech enthusiast ! Feel free to get in touch with [me](https://pyrustic.github.io/#contact) !

<br>
<br>
<br>

[Back to top](#readme)

            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "litedbc",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.8",
    "maintainer_email": "Pyrustic Architect <rusticalex@yahoo.com>",
    "keywords": "sqlite, transactional, threadsafe",
    "author": null,
    "author_email": "Pyrustic Architect <rusticalex@yahoo.com>",
    "download_url": "https://files.pythonhosted.org/packages/dd/5e/114a964b73204ac8df64510a5a0be8244bd5189a3a0882caefdd2a24e3b1/litedbc-0.0.2.tar.gz",
    "platform": null,
    "description": "[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)\n[![PyPI package version](https://img.shields.io/pypi/v/litedbc)](https://pypi.org/project/litedbc)\n[![Downloads](https://static.pepy.tech/badge/litedbc)](https://pepy.tech/project/litedbc)\n\n\n<!-- Intro Text -->\n# LiteDBC\n<b>Lightweight Database Connector</b>\n\nThis library is a streamlined wrapper for Python's [sqlite3](https://docs.python.org/3/library/sqlite3.html) module, built to provide a user-friendly and thread-safe bridge between [Jinbase](https://github.com/pyrustic/jinbase) and [SQLite](https://www.sqlite.org/index.html).\n\n**Connecting to an embedded database:**\n\n```python\nfrom litedbc import LiteDBC\n\ndbc = LiteDBC(\"/path/to/file.db\")\n\n# manually initialize the database with a SQL script\nif dbc.is_new:\n    with dbc.transaction() as cursor:\n        cursor.execute_script(\"-- <Initialization SQL Script> --\")\n# ...\ndbc.close()\n# the line above is optional, as the connection \n# closes automatically when the program exits\n```\n\n<p align=\"right\"><a href=\"#readme\">Back to top</a></p>\n\n**Providing SQL script for automatic initialization:**\n\n```python\nfrom litedbc import LiteDBC\n\nINIT_SCRIPT = \"\"\"BEGIN TRANSACTION;\n-- Create the USER table\nCREATE TABLE user (\n    name TEXT PRIMARY KEY,\n    age INTEGER NOT NULL);\nCOMMIT;\"\"\"\n\nwith LiteDBC(\"/path/to/file.db\", init_script=INIT_SCRIPT) as dbc:\n    pass\n```\n\n<p align=\"right\"><a href=\"#readme\">Back to top</a></p>\n\n**Interacting with a database:**\n\n```python\nfrom litedbc import LiteDBC\n\nwith LiteDBC(\"/path/to/file.db\") as dbc:\n    with dbc.cursor() as cur:\n        # execute a SQL statement\n        statement = \"INSERT INTO user (id, name) VALUES (?, ?)\"\n        cur.execute(statement, (42, \"alex\"))  # returns the 'rowcount'\n        \n        # query data\n        cur.execute(\"SELECT id, name FROM user\")\n        \n        # get the rows iteratively\n        for row in cur.fetch():\n            print(row)\n        \n        # execute a SQL script in a single transaction\n        cur.executescript(\"-- SQL Script --\")\n```\n\n<p align=\"right\"><a href=\"#readme\">Back to top</a></p>\n\n**Creating a transaction context to run complex logic:**\n\n```python\nfrom litedbc import LiteDBC\n\ndbc = LiteDBC(\"/path/to/file.db\")\n\n# creating a transaction context\nwith dbc.transaction() as cur:  # accepts an optional transaction mode\n    # from here, everything will be executed within a single transaction\n    cur.execute(\"SELECT COUNT(*) FROM user\")\n    for row in cur.fetch():\n        if row[0] >= 256:\n            cur.execute(\"DELETE FROM user WHERE id=?\", (1,))\n\ndbc.close()\n```\n\n<p align=\"right\"><a href=\"#readme\">Back to top</a></p>\n\n**Miscelleaneous:**\n\n```python\nfrom litedbc import LiteDBC\n\n# the constructor also accepts conn_kwargs, on_create_db, on_create_conn\ndbc = LiteDBC(\"/path/to/file.db\")  # not filename provided -> in-memory db !\n\n# return a tuple of tables present in the database\ntables = dbc.list_tables()\n\n# return a list of ColumnInfo instances that provide rich info\n# about each column, such as whether it's a primary key column or not\ntable_info = dbc.inspect(table)\n\n# export the database as a SQL script\nsql_script = dbc.dump(dst=None)\n\n# safely create a backup\ndbc.backup(\"/path/to/file.backup\")\n\n# create a new instance of Dbc for the same database file\nnew_dbc = dbc.copy()\n\n# Note that the Dbc class also exposes these properties:\n# .new, .conn, .in_memory, .filename, .conn_kwargs, .closed, .deleted\n```\n\n<p align=\"right\"><a href=\"#readme\">Back to top</a></p>\n\n\n# Testing and contributing\nFeel free to **open an issue** to report a bug, suggest some changes, show some useful code snippets, or discuss anything related to this project. You can also directly email [me](https://pyrustic.github.io/#contact).\n\n## Setup your development environment\nFollowing are instructions to setup your development environment\n\n```bash\n# create and activate a virtual environment\npython -m venv venv\nsource venv/bin/activate\n\n# clone the project then change into its directory\ngit clone https://github.com/pyrustic/litedbc.git\ncd litedbc\n\n# install the package locally (editable mode)\npip install -e .\n\n# run tests\npython -m tests\n\n# deactivate the virtual environment\ndeactivate\n```\n\n<p align=\"right\"><a href=\"#readme\">Back to top</a></p>\n\n# Installation\n**LiteDBC** is **cross-platform**. It is built on [Ubuntu](https://ubuntu.com/download/desktop) and should work on **Python 3.8** or **newer**.\n\n## Create and activate a virtual environment\n```bash\npython -m venv venv\nsource venv/bin/activate\n```\n\n## Install for the first time\n\n```bash\npip install litedbc\n```\n\n## Upgrade the package\n```bash\npip install litedbc --upgrade --upgrade-strategy eager\n```\n\n## Deactivate the virtual environment\n```bash\ndeactivate\n```\n\n<p align=\"right\"><a href=\"#readme\">Back to top</a></p>\n\n# About the author\nHello world, I'm Alex, a tech enthusiast ! Feel free to get in touch with [me](https://pyrustic.github.io/#contact) !\n\n<br>\n<br>\n<br>\n\n[Back to top](#readme)\n",
    "bugtrack_url": null,
    "license": "MIT License  Copyright (c) 2024 Pyrustic Architect  Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the \"Software\"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:  The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.  THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ",
    "summary": "Lite database connector",
    "version": "0.0.2",
    "project_urls": {
        "Homepage": "https://github.com/pyrustic/litedbc"
    },
    "split_keywords": [
        "sqlite",
        " transactional",
        " threadsafe"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "8d60af60b001dc94b25b8698dd150e9aa6baf2fa199d315583714642b1e70a4b",
                "md5": "ac2651a4734f24433f38fffb767eda3b",
                "sha256": "57ab7c68edafc0f4ee7d31bd6b7fea63c06bc63a07f1e458eef7174cfbbed5ec"
            },
            "downloads": -1,
            "filename": "litedbc-0.0.2-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "ac2651a4734f24433f38fffb767eda3b",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.8",
            "size": 16054,
            "upload_time": "2024-11-25T22:19:13",
            "upload_time_iso_8601": "2024-11-25T22:19:13.487267Z",
            "url": "https://files.pythonhosted.org/packages/8d/60/af60b001dc94b25b8698dd150e9aa6baf2fa199d315583714642b1e70a4b/litedbc-0.0.2-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "dd5e114a964b73204ac8df64510a5a0be8244bd5189a3a0882caefdd2a24e3b1",
                "md5": "4814e6c4482e584a3e13314e0f72307d",
                "sha256": "ddbd5c4786c6fb149e225554e74ea1e94525d7189961391a7682ed65c1e5a664"
            },
            "downloads": -1,
            "filename": "litedbc-0.0.2.tar.gz",
            "has_sig": false,
            "md5_digest": "4814e6c4482e584a3e13314e0f72307d",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.8",
            "size": 18287,
            "upload_time": "2024-11-25T22:19:15",
            "upload_time_iso_8601": "2024-11-25T22:19:15.013961Z",
            "url": "https://files.pythonhosted.org/packages/dd/5e/114a964b73204ac8df64510a5a0be8244bd5189a3a0882caefdd2a24e3b1/litedbc-0.0.2.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-11-25 22:19:15",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "pyrustic",
    "github_project": "litedbc",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": false,
    "lcname": "litedbc"
}
        
Elapsed time: 2.06372s