{"id":13545695,"url":"https://github.com/thombashi/SimpleSQLite","last_synced_at":"2025-04-02T15:31:49.078Z","repository":{"id":57467654,"uuid":"52209201","full_name":"thombashi/SimpleSQLite","owner":"thombashi","description":"SimpleSQLite is a Python library to simplify SQLite database operations: table creation, data insertion and get data as other data formats. Simple ORM functionality for SQLite.","archived":false,"fork":false,"pushed_at":"2023-12-18T03:50:33.000Z","size":1306,"stargazers_count":128,"open_issues_count":5,"forks_count":15,"subscribers_count":6,"default_branch":"master","last_synced_at":"2024-05-22T23:05:19.112Z","etag":null,"topics":["database","database-library","orm","pandas-dataframe","python-library","sqlite"],"latest_commit_sha":null,"homepage":"https://simplesqlite.rtfd.io/","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/thombashi.png","metadata":{"files":{"readme":"README.rst","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null},"funding":{"github":"thombashi","patreon":null,"open_collective":null,"ko_fi":null,"tidelift":null,"community_bridge":null,"liberapay":null,"issuehunt":null,"otechie":null,"custom":null}},"created_at":"2016-02-21T14:01:23.000Z","updated_at":"2024-05-19T06:45:15.000Z","dependencies_parsed_at":"2023-12-19T07:39:18.213Z","dependency_job_id":null,"html_url":"https://github.com/thombashi/SimpleSQLite","commit_stats":{"total_commits":1417,"total_committers":3,"mean_commits":472.3333333333333,"dds":0.429075511644319,"last_synced_commit":"596f3f48d4c5588042dffc0ebe548d7106ccc466"},"previous_names":[],"tags_count":126,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thombashi%2FSimpleSQLite","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thombashi%2FSimpleSQLite/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thombashi%2FSimpleSQLite/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thombashi%2FSimpleSQLite/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/thombashi","download_url":"https://codeload.github.com/thombashi/SimpleSQLite/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246730084,"owners_count":20824394,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["database","database-library","orm","pandas-dataframe","python-library","sqlite"],"created_at":"2024-08-01T11:01:09.954Z","updated_at":"2025-04-02T15:31:44.068Z","avatar_url":"https://github.com/thombashi.png","language":"Python","readme":".. contents:: **SimpleSQLite**\n   :backlinks: top\n   :depth: 2\n\nSummary\n=========\n`SimpleSQLite \u003chttps://github.com/thombashi/SimpleSQLite\u003e`__ is a Python library to simplify SQLite database operations: table creation, data insertion and get data as other data formats. Simple ORM functionality for SQLite.\n\n.. image:: https://badge.fury.io/py/SimpleSQLite.svg\n    :target: https://badge.fury.io/py/SimpleSQLite\n    :alt: PyPI package version\n\n.. image:: https://img.shields.io/pypi/pyversions/SimpleSQLite.svg\n    :target: https://pypi.org/project/SimpleSQLite\n    :alt: Supported Python versions\n\n.. image:: https://img.shields.io/pypi/implementation/SimpleSQLite.svg\n    :target: https://pypi.org/project/SimpleSQLite\n    :alt: Supported Python implementations\n\n.. image:: https://github.com/thombashi/SimpleSQLite/actions/workflows/ci.yml/badge.svg\n    :target: https://github.com/thombashi/SimpleSQLite/actions/workflows/ci.yml\n    :alt: CI status of Linux/macOS/Windows\n\n.. image:: https://github.com/thombashi/SimpleSQLite/actions/workflows/github-code-scanning/codeql/badge.svg\n    :target: https://github.com/thombashi/SimpleSQLite/actions/workflows/github-code-scanning/codeql\n    :alt: CodeQL\n\n.. image:: https://coveralls.io/repos/github/thombashi/SimpleSQLite/badge.svg?branch=master\n    :target: https://coveralls.io/github/thombashi/SimpleSQLite?branch=master\n    :alt: Test coverage\n\nFeatures\n--------\n- Automated SQLite table creation from data\n- Support various data types of record(s) insertion into a table:\n    - ``dict``\n    - ``namedtuple``\n    - ``list``\n    - ``tuple``\n- Create table(s) from:\n    - CSV file/text\n    - JSON file/text\n    - `pandas.DataFrame \u003chttps://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.html\u003e`__ instance\n    - `tabledata.TableData \u003chttps://tabledata.readthedocs.io/en/latest/pages/reference/data.html\u003e`__ instance loaded by `pytablereader \u003chttps://github.com/thombashi/pytablereader\u003e`__\n- Get data from a table as:\n    - `pandas.DataFrame \u003chttps://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.html\u003e`__ instance\n    - `tabledata.TableData \u003chttps://github.com/thombashi/tabledata\u003e`__ instance\n- Simple object-relational mapping (ORM) functionality\n\nExamples\n==========\nCreate a table\n----------------\nCreate a table from a data matrix\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n:Sample Code:\n    .. code-block:: python\n\n        from simplesqlite import SimpleSQLite\n\n\n        table_name = \"sample_table\"\n        con = SimpleSQLite(\"sample.sqlite\", \"w\")\n\n        # create table -----\n        data_matrix = [[1, 1.1, \"aaa\", 1, 1], [2, 2.2, \"bbb\", 2.2, 2.2], [3, 3.3, \"ccc\", 3, \"ccc\"]]\n        con.create_table_from_data_matrix(\n            table_name,\n            [\"attr_a\", \"attr_b\", \"attr_c\", \"attr_d\", \"attr_e\"],\n            data_matrix,\n        )\n\n        # display data type for each column in the table -----\n        print(con.schema_extractor.fetch_table_schema(table_name).dumps())\n\n        # display values in the table -----\n        print(\"records:\")\n        result = con.select(select=\"*\", table_name=table_name)\n        for record in result.fetchall():\n            print(record)\n\n:Output:\n    .. code-block::\n\n        .. table:: sample_table\n\n            +---------+-------+-----------+--------+------+-----+\n            |Attribute| Type  |PRIMARY KEY|NOT NULL|UNIQUE|Index|\n            +=========+=======+===========+========+======+=====+\n            |attr_a   |INTEGER|           |        |      |     |\n            +---------+-------+-----------+--------+------+-----+\n            |attr_b   |REAL   |           |        |      |     |\n            +---------+-------+-----------+--------+------+-----+\n            |attr_c   |TEXT   |           |        |      |     |\n            +---------+-------+-----------+--------+------+-----+\n            |attr_d   |REAL   |           |        |      |     |\n            +---------+-------+-----------+--------+------+-----+\n            |attr_e   |TEXT   |           |        |      |     |\n            +---------+-------+-----------+--------+------+-----+\n\n\n        records:\n        (1, 1.1, 'aaa', 1.0, '1')\n        (2, 2.2, 'bbb', 2.2, '2.2')\n        (3, 3.3, 'ccc', 3.0, 'ccc')\n\nCreate a table from CSV\n~~~~~~~~~~~~~~~~~~~~~~~~~\n:Sample Code:\n    .. code-block:: python\n\n        from simplesqlite import SimpleSQLite\n\n        with open(\"sample_data.csv\", \"w\") as f:\n            f.write(\"\\n\".join([\n                '\"attr_a\",\"attr_b\",\"attr_c\"',\n                '1,4,\"a\"',\n                '2,2.1,\"bb\"',\n                '3,120.9,\"ccc\"',\n            ]))\n\n        # create table ---\n        con = SimpleSQLite(\"sample.sqlite\", \"w\")\n        con.create_table_from_csv(\"sample_data.csv\")\n\n        # output ---\n        table_name = \"sample_data\"\n        print(con.fetch_attr_names(table_name))\n        result = con.select(select=\"*\", table_name=table_name)\n        for record in result.fetchall():\n            print(record)\n\n:Output:\n    .. code-block::\n\n        ['attr_a', 'attr_b', 'attr_c']\n        (1, 4.0, 'a')\n        (2, 2.1, 'bb')\n        (3, 120.9, 'ccc')\n\nCreate a table from pandas.DataFrame\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n:Sample Code:\n    .. code-block:: python\n\n        from simplesqlite import SimpleSQLite\n        import pandas\n\n        con = SimpleSQLite(\"pandas_df.sqlite\")\n\n        con.create_table_from_dataframe(pandas.DataFrame(\n            [\n                [0, 0.1, \"a\"],\n                [1, 1.1, \"bb\"],\n                [2, 2.2, \"ccc\"],\n            ],\n            columns=['id', 'value', 'name']\n        ), table_name=\"pandas_df\")\n\n:Output:\n    .. code-block:: sql\n\n        $ sqlite3 pandas_df.sqlite\n        sqlite\u003e .schema\n        CREATE TABLE 'pandas_df' (id INTEGER, value REAL, name TEXT);\n\nInsert records into a table\n-----------------------------\nInsert dictionary\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n\n:Sample Code:\n    .. code-block:: python\n\n        from simplesqlite import SimpleSQLite\n\n        table_name = \"sample_table\"\n        con = SimpleSQLite(\"sample.sqlite\", \"w\")\n        con.create_table_from_data_matrix(\n            table_name,\n            [\"attr_a\", \"attr_b\", \"attr_c\", \"attr_d\", \"attr_e\"],\n            [[1, 1.1, \"aaa\", 1,   1]])\n\n        con.insert(\n            table_name,\n            record={\n                \"attr_a\": 4,\n                \"attr_b\": 4.4,\n                \"attr_c\": \"ddd\",\n                \"attr_d\": 4.44,\n                \"attr_e\": \"hoge\",\n            })\n        con.insert_many(\n            table_name,\n            records=[\n                {\n                    \"attr_a\": 5,\n                    \"attr_b\": 5.5,\n                    \"attr_c\": \"eee\",\n                    \"attr_d\": 5.55,\n                    \"attr_e\": \"foo\",\n                },\n                {\n                    \"attr_a\": 6,\n                    \"attr_c\": \"fff\",\n                },\n            ])\n\n        result = con.select(select=\"*\", table_name=table_name)\n        for record in result.fetchall():\n            print(record)\n\n:Output:\n    .. code-block::\n\n        (1, 1.1, 'aaa', 1, 1)\n        (4, 4.4, 'ddd', 4.44, 'hoge')\n        (5, 5.5, 'eee', 5.55, 'foo')\n        (6, None, 'fff', None, None)\n\n\nInsert list/tuple/namedtuple\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n\n:Sample Code:\n    .. code-block:: python\n\n        from collections import namedtuple\n        from simplesqlite import SimpleSQLite\n\n        table_name = \"sample_table\"\n        con = SimpleSQLite(\"sample.sqlite\", \"w\")\n        con.create_table_from_data_matrix(\n            table_name,\n            [\"attr_a\", \"attr_b\", \"attr_c\", \"attr_d\", \"attr_e\"],\n            [[1, 1.1, \"aaa\", 1, 1]],\n        )\n\n        # insert namedtuple\n        SampleTuple = namedtuple(\"SampleTuple\", \"attr_a attr_b attr_c attr_d attr_e\")\n\n        con.insert(table_name, record=[7, 7.7, \"fff\", 7.77, \"bar\"])\n        con.insert_many(\n            table_name,\n            records=[(8, 8.8, \"ggg\", 8.88, \"foobar\"), SampleTuple(9, 9.9, \"ggg\", 9.99, \"hogehoge\")],\n        )\n\n        # print\n        result = con.select(select=\"*\", table_name=table_name)\n        for record in result.fetchall():\n            print(record)\n\n:Output:\n    .. code-block::\n\n        (1, 1.1, 'aaa', 1, 1)\n        (7, 7.7, 'fff', 7.77, 'bar')\n        (8, 8.8, 'ggg', 8.88, 'foobar')\n        (9, 9.9, 'ggg', 9.99, 'hogehoge')\n\nFetch data from a table as pandas DataFrame\n---------------------------------------------\n:Sample Code:\n    .. code-block:: python\n\n        from simplesqlite import SimpleSQLite\n\n        con = SimpleSQLite(\"sample.sqlite\", \"w\", profile=True)\n\n        con.create_table_from_data_matrix(\n            \"sample_table\",\n            [\"a\", \"b\", \"c\", \"d\", \"e\"],\n            [\n                [1, 1.1, \"aaa\", 1,   1],\n                [2, 2.2, \"bbb\", 2.2, 2.2],\n                [3, 3.3, \"ccc\", 3,   \"ccc\"],\n            ])\n\n        print(con.select_as_dataframe(table_name=\"sample_table\"))\n\n:Output:\n    .. code-block::\n\n        $ sample/select_as_dataframe.py\n           a    b    c    d    e\n        0  1  1.1  aaa  1.0    1\n        1  2  2.2  bbb  2.2  2.2\n        2  3  3.3  ccc  3.0  ccc\n\nORM functionality\n-------------------\n:Sample Code:\n    .. code-block:: python\n\n        from simplesqlite import connect_memdb\n        from simplesqlite.model import Integer, Model, Real, Text\n\n\n        class Sample(Model):\n            foo_id = Integer(primary_key=True)\n            name = Text(not_null=True, unique=True)\n            value = Real(default=0)\n\n\n        def main() -\u003e None:\n            con = connect_memdb()\n\n            Sample.attach(con)\n            Sample.create()\n            Sample.insert(Sample(name=\"abc\", value=0.1))\n            Sample.insert(Sample(name=\"xyz\", value=1.11))\n            Sample.insert(Sample(name=\"bar\"))\n\n            print(Sample.fetch_schema().dumps())\n            print(\"records:\")\n            for record in Sample.select():\n                print(f\"    {record}\")\n\n\n        if __name__ == \"__main__\":\n            main()\n\n:Output:\n    .. code-block::\n\n        .. table:: sample\n\n            +--------+---------+----------+-----+---------+-------+-------+\n            | Field  |  Type   | Nullable | Key | Default | Index | Extra |\n            +========+=========+==========+=====+=========+=======+=======+\n            | foo_id | INTEGER | YES      | PRI | NULL    |   X   |       |\n            +--------+---------+----------+-----+---------+-------+-------+\n            | name   | TEXT    | NO       | UNI |         |   X   |       |\n            +--------+---------+----------+-----+---------+-------+-------+\n            | value  | REAL    | YES      |     | 0       |       |       |\n            +--------+---------+----------+-----+---------+-------+-------+\n\n        records:\n            Sample (foo_id=1, name=abc, value=0.1)\n            Sample (foo_id=2, name=xyz, value=1.11)\n            Sample (foo_id=3, name=bar, value=0.0)\n\nFor more information\n----------------------\nMore examples are available at \nhttps://simplesqlite.rtfd.io/en/latest/pages/examples/index.html\n\nInstallation\n============\nInstall from PyPI\n------------------------------\n::\n\n    pip install SimpleSQLite\n\nInstall from PPA (for Ubuntu)\n------------------------------\n::\n\n    sudo add-apt-repository ppa:thombashi/ppa\n    sudo apt update\n    sudo apt install python3-simplesqlite\n\n\nDependencies\n============\n- Python 3.7+\n- `Python package dependencies (automatically installed) \u003chttps://github.com/thombashi/SimpleSQLite/network/dependencies\u003e`__\n\nOptional Dependencies\n----------------------------------\n- `loguru \u003chttps://github.com/Delgan/loguru\u003e`__\n    - Used for logging if the package installed\n- `pandas \u003chttps://pandas.pydata.org/\u003e`__\n- `pytablereader \u003chttps://github.com/thombashi/pytablereader\u003e`__\n\nDocumentation\n===============\nhttps://simplesqlite.rtfd.io/\n\nRelated Project\n=================\n- `sqlitebiter \u003chttps://github.com/thombashi/sqlitebiter\u003e`__: CLI tool to convert CSV/Excel/HTML/JSON/LTSV/Markdown/TSV/Google-Sheets SQLite database by using SimpleSQLite\n\nSponsors\n====================================\n.. image:: https://avatars.githubusercontent.com/u/44389260?s=48\u0026u=6da7176e51ae2654bcfd22564772ef8a3bb22318\u0026v=4\n   :target: https://github.com/chasbecker\n   :alt: Charles Becker (chasbecker)\n.. image:: https://avatars.githubusercontent.com/u/46711571?s=48\u0026u=57687c0e02d5d6e8eeaf9177f7b7af4c9f275eb5\u0026v=4\n   :target: https://github.com/Arturi0\n   :alt: onetime: Arturi0\n.. image:: https://avatars.githubusercontent.com/u/3658062?s=48\u0026v=4\n   :target: https://github.com/b4tman\n   :alt: onetime: Dmitry Belyaev (b4tman)\n\n`Become a sponsor \u003chttps://github.com/sponsors/thombashi\u003e`__\n\n","funding_links":["https://github.com/sponsors/thombashi"],"categories":["Python"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fthombashi%2FSimpleSQLite","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fthombashi%2FSimpleSQLite","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fthombashi%2FSimpleSQLite/lists"}