Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

This article is a Private article. Only a writer and users who know the URL can access it.
Please change open range to public in publish setting if you want to share this article with other users.

More than 3 years have passed since last update.

[Python] sqlite3メモ

Last updated at Posted at 2020-03-14

単語帳.転載多め.元URLあり.
これまでPythonを使っていてSQLに挑戦したい人向け?

  • MySQLやPostgreSQLなどSQLite以外のSQLにも共通する事項
  • Pythonに関係なくSQLiteの操作に関する事項
  • Pythonから他のSQLを操作するライブラリに共通する事項
  • PythonからSQLiteを操作するライブラリsqlite3に関する事項

が混在していますが,1番最後の項目を中心に.

基本的な事項がまとまっている文献

[sqlite3 — DB-API 2.0 interface for SQLite databases]
(https://docs.python.org/3.7/library/sqlite3.html#)
[Qiita@mas9612: Pythonでsqlite]
(https://qiita.com/mas9612/items/a881e9f14d20ee1c0703)
[Crane & to.: Python3でSQLite3を使う – 基本操作からエラー処理までサンプルコード付]
(https://www.craneto.co.jp/archives/1248/)

with構文を使った取り扱い

データベースを扱うときは,

  • データベースに接続
  • データの変更
  • 変更の反映 (commit)
  • 接続の切断 (close)

という流れになる.with構文を使ってこれを書くためには以下の通り.
with sqlite3.connect(db_path) as conn:だけではcommit()するだけでclose()まではしないことに注意.

import sqlite3
from contextlib import closing

with closing(sqlite3.connect(db_path)) as conn:
    # print(type(conn)) # <class 'sqlite3.Connection'>
    ...
# OR
with closing(sqlite3.connect(db_path).cursor()) as cur:
    print(type(cur)) # <class 'sqlite3.Cursor'>
    ...

[stackoverflow: using sqlite3 in python with “WITH” keyword]
(https://stackoverflow.com/questions/19522505/using-sqlite3-in-python-with-with-keyword)
https://docs.python.org/2/library/sqlite3.html#using-the-connection-as-a-context-manager
[stackoverflow: PythonでSQLiteを使う時にクローズ処理は行うべき?]
(https://ja.stackoverflow.com/questions/47318/python%e3%81%a7sqlite%e3%82%92%e4%bd%bf%e3%81%86%e6%99%82%e3%81%ab%e3%82%af%e3%83%ad%e3%83%bc%e3%82%ba%e5%87%a6%e7%90%86%e3%81%af%e8%a1%8c%e3%81%86%e3%81%b9%e3%81%8d)
[stacoverflow: SQLite cursor in Python with statement]
(https://stackoverflow.com/questions/16668623/sqlite-cursor-in-python-with-statement)

sqlite3.Connectionクラスとsqlite3.Cursorクラス

[11.13.2. Connection Objects]
(https://docs.python.org/2/library/sqlite3.html#connection-objects)
[11.13.3. Cursor Objects]
(https://docs.python.org/2/library/sqlite3.html#cursor-objects)

CREATE TABLEINSERTといったSQL文はどちらのクラスからでも.execute()メソッドで実行可能.
しかし結果の取得にはCursorクラスのfectchone()fetchall()が必要.
[stackoverflow: Why do you need to create a cursor when querying a sqlite database?]
(https://stackoverflow.com/questions/6318126/why-do-you-need-to-create-a-cursor-when-querying-a-sqlite-database)
[teratail: cursor()メソッドを使うときと使わないとき]
(https://teratail.com/questions/72049)

Pythonライブラリにおけるカーソルクラスの実装はマチマチで,fetchall()でなくfetchone()を使ったところで(generator的に)メモリの節約になるとは限らない.
[Qiita: PythonとDB: DBIのcursorを理解する]
(https://qiita.com/knoguchi/items/3d5631505b3f08fa37cc)

注意: 1つのコードで複数のexecute()を組み合わせることで,
SELECTによる結果の取得→その行をアップデート
等ということも (Pythonライクに?) できるが,それよりもSQL文に習熟した方が良い.
例えば上記の操作は以下の通り1文で実現可能.
[stackoverflow: Update row with select on same table]
(https://stackoverflow.com/questions/25266878/update-row-with-select-on-same-table)

その他

PythonでNULL値を追加するにはNone

辞書型を返すことも出来る

[stackoverflow: How can I get dict from sqlite query?]
(https://stackoverflow.com/questions/3300464/how-can-i-get-dict-from-sqlite-query)
[Qiita@wiwi7373: [Python3][SQLite3]レコードを辞書型(dict型)で取得する簡単な方法]
(https://qiita.com/wiwi7373/items/7d47decf85a77454074d)

タグ管理の設計

[衣食住よりプログラミング: 【MySQL】タグ付け機能の実装にオススメなテーブル設計(TOXI法)その1]
(https://senews.jp/toxi1/)

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?