Created Simple SQLite, a Python library that simplifies SQLite table creation / data insertion

A library for easily creating SQLite databases in Python. For situations such as:

--I want to create an RDB for a prototype of a program now ――I want to think about the table structure later --Table structure unknown in advance (dynamic database construction required) --I want to use the dictionary / namedtuple when doing INSERT --CSV / JSON / Excel / Google-I want to create a table from Sheets

GitHub repository

Documentation

Feature --Automatic table creation from data --Data types that can be INSERTed:

Installation

pip install SimpleSQLite

Usage

Create table from array

Sample


from simplesqlite import SimpleSQLite

con = SimpleSQLite("sample.sqlite")

data_matrix = [
    [1, 1.1, "aaa", 1,   1],
    [2, 2.2, "bbb", 2.2, 2.2],
    [3, 3.3, "ccc", 3,   "ccc"],
]
con.create_table_with_data(
    table_name="sample_table",
    attribute_name_list=["attr_a", "attr_b", "attr_c", "attr_d", "attr_e"],
    data_matrix=data_matrix)

# display values -----
print(con.get_attribute_name_list("sample_table"))
result = con.select(select="*", table_name="sample_table")
for record in result.fetchall():
    print(record)

# display data type for each column -----
print(con.get_attribute_type_list(table_name="sample_table"))

Output


['attr_a', 'attr_b', 'attr_c', 'attr_d', 'attr_e']
(1, 1.1, u'aaa', 1.0, u'1')
(2, 2.2, u'bbb', 2.2, u'2.2')
(3, 3.3, u'ccc', 3.0, u'ccc')
(u'integer', u'real', u'text', u'real', u'text')

Create table from CSV file

sample_data.csv


"attr_a","attr_b","attr_c"
1,4,"a"
2,2.1,"bb"
3,120.9,"ccc"

Sample-code


from simplesqlite import SimpleSQLite

con = SimpleSQLite("sample.sqlite", "w")
con.create_table_from_csv(csv_path="sample_data.csv")

print(con.get_attribute_name_list("sample_data"))
result = con.select(select="*", table_name="sample_data")
for record in result.fetchall():
    print(record)

Output


['attr_a', 'attr_b', 'attr_c']
(1, 4.0, u'a')
(2, 2.1, u'bb')
(3, 120.9, u'ccc')

Insert data into a table

Dictionary INSERT

Sample-code


from simplesqlite import SimpleSQLite

con = SimpleSQLite("sample.sqlite", "w")
con.create_table_with_data(
    table_name="sample_table",
    attribute_name_list=["attr_a", "attr_b", "attr_c", "attr_d", "attr_e"],
    data_matrix=[[1, 1.1, "aaa", 1,   1]])

con.insert(
    table_name="sample_table",
    insert_record={
        "attr_a": 4,
        "attr_b": 4.4,
        "attr_c": "ddd",
        "attr_d": 4.44,
        "attr_e": "hoge",
    }
)
con.insert_many(
    table_name="sample_table",
    insert_record_list=[
        {
            "attr_a": 5,
            "attr_b": 5.5,
            "attr_c": "eee",
            "attr_d": 5.55,
            "attr_e": "foo",
        },
        {
            "attr_a": 6,
            "attr_c": "fff",
        },
    ]
)

result = con.select(select="*", table_name="sample_table")
for record in result.fetchall():
    print(record)

Output


(1, 1.1, u'aaa', 1, 1)
(4, 4.4, u'ddd', 4.44, u'hoge')
(5, 5.5, u'eee', 5.55, u'foo')
(6, u'NULL', u'fff', u'NULL', u'NULL')

list / tuple / named tuple INSERT

Sample-code


from collections import namedtuple
from simplesqlite import SimpleSQLite

con = SimpleSQLite("sample.sqlite", "w")
con.create_table_with_data(
    table_name="sample_table",
    attribute_name_list=["attr_a", "attr_b", "attr_c", "attr_d", "attr_e"],
    data_matrix=[[1, 1.1, "aaa", 1,   1]])

SampleTuple = namedtuple(
    "SampleTuple", "attr_a attr_b attr_c attr_d attr_e")

con.insert(
    table_name="sample_table",
    insert_record=[7, 7.7, "fff", 7.77, "bar"])
con.insert_many(
    table_name="sample_table",
    insert_record_list=[
        (8, 8.8, "ggg", 8.88, "foobar"),
        SampleTuple(9, 9.9, "ggg", 9.99, "hogehoge"),
    ]
)

result = con.select(select="*", table_name="sample_table")
for record in result.fetchall():
    print(record)

Output


(1, 1.1, u'aaa', 1, 1)
(7, 7.7, u'fff', 7.77, u'bar')
(8, 8.8, u'ggg', 8.88, u'foobar')
(9, 9.9, u'ggg', 9.99, u'hogehoge')

In-memory DB

When using in-memory DB:

import simplesqlite
con = simplesqlite.connect_sqlite_db_mem()

Recommended Posts

Created Simple SQLite, a Python library that simplifies SQLite table creation / data insertion
Created a Python library DateTimeRange that handles time ranges
Published a library that hides character data in Python images
Created a library for python that can easily handle morpheme division
Try using APSW, a Python library that SQLite can get serious about
Code reading of faker, a library that generates test data in Python
A simple to-do list created with Python + Django
A simple Python HTTP server that supports Range Requests
A server that echoes data POSTed with flask / python
[Python] A convenient library that converts kanji to hiragana
Publish / upload a library created in Python to PyPI
A Python program that converts ical data into text
A class that creates DB creation-data insertion with SQLite3 quickly
A memo that reads data from dashDB with Python & Spark
A Python program that aggregates time usage from icalendar data
A python script that converts Oracle Database data to csv
A simple data analysis of Bitcoin provided by CoinMetrics in Python
Use networkx, a library that handles graphs in python (Part 2: Tutorial)
I created a Python library to call the LINE WORKS API
About psd-tools, a library that can process psd files in Python
[For beginners] How to register a library created in Python in PyPI
[Python] Analyze Splatoon 2 league match data using a correlation coefficient table
I made a library that adds docstring to a Python stub file.
Created a service that allows you to search J League data
A python script that deletes ._DS_Store and ._ * files created on Mac
Introduction of "scikit-mobility", a library that allows you to easily analyze human flow data with Python (Part 1)