pythonでmysqlを操作2

mysql-connector-pythonでも良いが、pymysqlを使う場合

$ pip3 install pymysql

import pymysql

print("hello world")

connection = pymysql.connect(
	host="localhost",
	db="hoge",
	user="fuga",
	password="asdf",
	charset="utf8",
	cursorclass=pymysql.cursors.DictCursor
)

sql = "SELECT * FROM avalanche"
cursor = connection.cursor()
cursor.execute(sql)
posts = cursor.fetchall()

cursor.close()
connection.close()

for post in posts:
	print(post["title"])

ほう