LoginSignup
0
1

More than 1 year has passed since last update.

Python3からMySQLのテーブルデータを取得してみた。

Last updated at Posted at 2021-06-03

勉強中。
すぐ忘れるのでノートとしてのQiita便利じゃない?

の続き。
データベースの接続まで出来たので、実行したいのもを書いていく。

python.png

上記の画像、カーソルってなんやねんの答え ↓

平たく言えば、「検索条件に合致するレコードを、1件ずつ取り出すための仕組み」となります。

だそうです。

前回と同様に

このページを見ていく。

いろいろテーブルを初期化したり作成したりしてるが
私がやりたいのはもうすでに作られてるテーブルの取得のみ。

なので

#!/usr/bin/env python3

import MySQLdb

connection = MySQLdb.connect(
        host='localhost',
        user='root',
        passwd='MySQLで設定したPW',
        db='school',
        charset='utf8')  #日本語を取り扱うために記入
cursor = connection.cursor()   #カーソルの生成

#一覧を表示
cursor.execute("SELECT * FROM students") #studentsテーブルを表示したい

for row in cursor:
    print(row)

# 接続を閉じる
connection.close()

内容としてはこんな感じに記入した。

0
1
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
0
1