<Python, SQlite> sqlite3 を使う。

データベースdatabasesqlite3を使う。

1.. まずは、モジュール呼び出し。
:memory:とすると、メモリ上にデータベースを作成。

In [1]: import sqlite3

In [2]: con = sqlite3.connect(':memory:')

2.. 初期化。
テーブルtableを作る。
データベースの各要素のカラムColumnを設定。

In [6]: con.execute('''
   ...: create table {table} (
   ...: {id} integer primary key autoincrement,
   ...: {name} text not null,
   ...: {brightness} integer not null
   ...: );
   ...: '''.format(id='id', table='hagemen', name='name', brightness='brightness'))
Out[6]: <sqlite3.Cursor at 0xb066030>

3.. データを追加。
注意、、、 文字列 {name}は、'{name}''で囲わないとエラーとなる。

In [8]: con.execute("insert into {table} values(null, '{name}', {brightness})".format(table='hagemen', name='jiro', brightness=5000))
Out[8]: <sqlite3.Cursor at 0xb0661f0>

4.. 書けたかな?とデータベースの中身を見てみる。

In [9]: cur = con.execute('select * from {table}'.format(table='hagemen'))

In [10]: for r in cur:
    ...:     print(r)
    ...:     
(1, 'jiro', 5000)

5.. カラム名指定で値を引き出しみたい場合、、、
そのままだと何も出てこない。

In [11]: for r in cur:
    ...:     print(r['name'])
    ...:     

で、下記呪文.row_factory.Rowをセットを唱えると、出てくる。

In [12]: con.row_factory = sqlite3.Row

In [14]: cur = con.execute('select * from {table}'.format(table='hagemans'))

In [15]: for r in cur:
    ...:     print(r['name'])
    ...:     
jiro

Python/SQlite マニュアル。

12.6. sqlite3 — SQLite データベースに対する DB-API 2.0 インタフェース — Python 3.5.1 ドキュメント

SQLite Home Page

参考。

ライブラリ:sqlite3 - Life with Python

msrx9.bitbucket.org

qiita.com