4
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

SQLiteのテーブル作成/データ挿入を簡易化するPythonライブラリSimpleSQLiteを作った

Last updated at Posted at 2016-02-21

PythonでSQLiteデータベースを簡単に作成するためのライブラリ。
以下にのような状況向け:

  • プログラムのプロトタイプのためにRDBを今すぐ作りたい
    • テーブル構成はひとまず後で考えたい
  • 事前にテーブル構成が不明 (動的なデータベース構築が必要)
  • INSERTするときに 辞書/namedtupleを使いたい
  • CSV/JSON/Excel/Google-Sheetsからテーブルを作成したい

GitHubリポジトリ

Documentation

Feature

  • データからテーブルの自動作成
  • INSERTできるデータ型:
  • dictionary
  • namedtuple
  • list
  • tuple
  • 色々なデータソースからテーブル作成
  • CSV ファイル/テキスト
  • JSON ファイル/テキスト
  • Excelファイル
  • Google Sheets

Installation

pip install SimpleSQLite

Usage

配列からテーブル作成

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')

CSVファイルからテーブル作成

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

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/namedtupleの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')

インメモリDB

インメモリDBを使用する場合は以下。

import simplesqlite
con = simplesqlite.connect_sqlite_db_mem()
4
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?