- from tinydb import TinyDB, Query
- # データベースオープン 引数はファイル名
- db = TinyDB('db.json')
- # insertでデータを登録
- # 辞書型のオブジェクトをそのまま渡せばOK
- db.insert({'type': 'apple', 'count': 7})
- db.insert({'type': 'peach', 'count': 3})
- # db.all()で登録しているデータを全て出力
- print db.all()
- from tinydb import TinyDB, Query
- # データベースオープン 引数はファイル名
- db = TinyDB('db.json')
- # Queryオブジェクトを生成
- Fruit = Query()
- # 'type'が'peach'の値を検索
- print db.search(Fruit.type == 'peach')
- # countが5より大きい値を検索
- print db.search(Fruit.count > 5)
- from tinydb import TinyDB, Query
- # データベースオープン 引数はファイル名
- db = TinyDB('db.json')
- # Queryオブジェクトを生成
- Fruit = Query()
- # countが1より大きい値を検索
- for row in db.search(Fruit.count > 1):
- print row
- from tinydb import TinyDB, Query
- # データベースオープン 引数はファイル名
- db = TinyDB('db.json')
- # Queryオブジェクトを生成
- Fruit = Query()
- # 'type'が'apple'のcountを10に変更
- db.update({'count': 10}, Fruit.type == 'apple')
- # 結果確認
- print db.all()
- from tinydb import TinyDB, Query
- # データベースオープン 引数はファイル名
- db = TinyDB('db.json')
- # Queryオブジェクトを生成
- Fruit = Query()
- # countが5より小さいデータを削除
- db.remove(Fruit.count < 5)
- # 結果を確認
- print db.all()
- # データベースオープン 引数はファイル名
- db = TinyDB('db.json')
- # 全てのデータを削除
- db.purge()
- # 結果を確認
- print db.all()
- from tinydb import TinyDB, Query
- # データベースオープン 引数はファイル名
- db = TinyDB('db.json')
- # テーブルを作成
- table_1 = db.table('table_1')
- # データ登録
- table_1.insert({'value':'table_1_value'})
- # 2つめのテーブルを作成
- table_2 = db.table('table_2')
- # データ登録
- table_2.insert({'value':'table_2_value'})
- # 各々、結果を確認
- print table_1.all()
- print '-' * 10
- print table_2.all()
- {"_default": {}, "table_2": {"1": {"value": "table_2_value"}}, "table_1": {"1": {"value": "table_1_value"}}}
Author:symfo
富士通製データベースSymfowareについて考察する男
blog形式だと探しにくいので、まとめサイト作成中です。
Symfoware まとめ