pythonからmysqlを使う.とりあえずcygwinは後回しにして,Windowsで.
python3では,mysqlclient が良さそうだというようなことが stack overflow に書いてあったので,それを使う.
pypiのサイト から,wheel ファイル (mysqlclient-1.3.12-cp36-cp36m-win_amd64.whl) を持って来て,
pip3 install wheel pip3 install --use-wheel --no-index mysqlclient-1.3.12-cp36-cp36m-win_amd64.whl
で入った.
こんな感じで実行できる.
import MySQLdb
from MySQLdb.cursors import DictCursor
db = MySQLdb.connect(
host="localhost",
user="root",
passwd="",
db="dbe1",
charset='utf8'
)
c = db.cursor()
c.executemany(
"INSERT INTO 家計簿 VALUES (%s, %s, %s, %s, %s)",
[('2018/03/15', '食費', 'スターバックスでコーヒー', 0, 420),
('2018/03/16', '光熱費', '3月水道料金', 0, 3510)]
)
db.commit()
c = db.cursor(DictCursor)
sql="""SELECT * FROM 家計簿"""
c.execute(sql)
for x in c:
print(x)
print('---')
c = db.cursor(DictCursor)
c.execute('SELECT * FROM 家計簿 WHERE 費目 = %s', ('食費',))
for x in c:
print(f"{x['日付']}の日付で,「{x['メモ']}」")