python MySQl

python から MySQL へ接続しデータを取得

mysqlclient

Install

mysqlclient

tuple で返す

import MySQLdb

conn = MySQLdb.connect(user=DB_USER, passwd=DB_PASSWORD, host=DB_HOST, db=DB_NAME, port=DB_PORT)
cur = conn.cursor()
cur.execute("select * from users; ")
rows = cur.fetchall()
print(rows)

dict で返す

import MySQLdb
from MySQLdb.cursors import DictCursor

conn = MySQLdb.connect(user=DB_USER, passwd=DB_PASSWORD, host=DB_HOST, db=DB_NAME, port=DB_PORT)
cur = conn.cursor(DictCursor)
cur.execute("select * from users; ")
rows = cur.fetchall()
print(rows)

おっさんWEBエンジニア奮闘記©2007 WEBDIMENSION