見出し画像

PythonとMariaDBのつなげてみよう


どうも、じぇいかわさきです。

今日は、既にインストールしてあるMariaDBに対して、ターミナルからいちいち接続しなくても良いように、Pythonで接続できるように設定を行います。

実は、macを変更しているので、過去の設定がすべてなくなっているからやり直しなんです。

スクレイピングをしてDBへの登録でも使いそうですから、ここらでもう一度MariaDBへ接続できることを検証していきます。


まずは使うモジュールをインストールしよう

まずは、MariaDBに接続するためのモジュールをインストールしなければなりません。

インストールするモジュールは2つ

1つはクライアント用モジュールのmysqlclientと、PythonでMariaDBに接続するためのmysql-connector-pythonの2つになります。

多分、mysql-connector-pythonだけで良いと思うんですが、とりあえず入れておきます。


インストールするのはpipを用いてインストールするため、簡単にインストールできます。

*****@NERV ~ % sudo pip3 install mysqlclient
WARNING: The directory '/Users/******/Library/Caches/pip' or its parent directory is not owned or is not writable by the current user. The cache has been disabled. Check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
Collecting mysqlclient
Downloading mysqlclient-2.0.3.tar.gz (88 kB)
|████████████████████████████████| 88 kB 4.5 MB/s
Using legacy 'setup.py install' for mysqlclient, since package 'wheel' is not installed.
Installing collected packages: mysqlclient
Running setup.py install for mysqlclient ... done
Successfully installed mysqlclient-2.0.3
******@NERV ~ %
******@NERV ~ % sudo pip3 install mysql-connector-python
Password:
WARNING: The directory '/Users/******/Library/Caches/pip' or its parent directory is not owned or is not writable by the current user. The cache has been disabled. Check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
Collecting mysql-connector-python
Downloading mysql_connector_python-8.0.23-cp38-cp38-macosx_10_14_x86_64.whl (4.9 MB)
|████████████████████████████████| 4.9 MB 4.5 MB/s
Collecting protobuf>=3.0.0
Downloading protobuf-3.14.0-cp38-cp38-macosx_10_9_x86_64.whl (1.0 MB)
|████████████████████████████████| 1.0 MB 17.6 MB/s
Requirement already satisfied: six>=1.9 in /Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages (from protobuf>=3.0.0->mysql-connector-python) (1.15.0)
Installing collected packages: protobuf, mysql-connector-python
Successfully installed mysql-connector-python-8.0.23 protobuf-3.14.0
******@NERV ~ %


これでMariaDBにPythonで接続するための準備が整いました。過去に1度経験しているのですが、思い出せずに苦労しました。


MariaDBに接続するコードを書く

それでは、PythonでMariaDBに接続するためのコードを書いていきましょう。

接続するだけなら、特に問題なくできると思います。

まずはMariaDBへ接続するためのモジュールをインポートします。

import mysql.connector as mydb
import sys


次に、mysql-connectorを用いてDBへ接続します。

conn = mydb.connect(
 host='localhost',
 port='3306',
 user='***',
 password='--------',
 database='python_db'
)


これで接続はできたはずなんですが、単に接続をしただけなので、目に見える結果が有りません。

そこで、確かめる方法として以下のようのあprint文を書き、正しく接続ができていればTrueが帰ってくるので、その表示で判断します。

print(conn.is_connected())

もし正しく接続できていれば、Trueと表示されます.

今回はちゃんとTrueと帰ってきましたから大丈夫ですね。

これで接続する事が完了しました。

せっかくですので、テーブルからすべてのデータを取り出してみましょう。

cur = conn.cursor()
cur.execute("select * from result")
print(cur.fetchall())

このコードで取り出せますね。

最後に、DBとの接続を切り離します。ちゃんと切り離されているか、おなじように

print(conn.is_connected())

で確認します。 今度は、Falseが返ってきましたので、無事切り離されています。

これでPythonからMariaDBへの接続と、データの取り出しまでできることが確認できました。

次はデータを登録する方法を考えて行きましょう。


じぇいかわさきです。生産技術者として35年、今まで培った経験とスキルを元に、ものづくりに関わる世の出来事に対して思ったことをホンネで書いてます。ノウハウやアイデアもありますよ。 また写真も全力で撮っています、気に入った写真があればサポートや感想をぜひお寄せください。