Pythonでデータベースの操作するときはmysql-connector-pythonを使っているのですが、with構文を使えず毎回closeを書くのが面倒なのでwith構文に対応したラッパークラスを作りました。
with構文に対応したmysql-connector-pythonのラッパークラス
ソースコードは以下のとおりです。
使い方
使い方は以下のとおりです。
from db import Connector
account = {
"db": "databasename",
"host": "ip address",
"user": "username",
"passwd": "password"
}
with Connector(account) as connect:
cursor = connect.cursor()
cursor.execut("SELECT * FROM tbl")
results = cursor.fetchall()
cursor.close()
