0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Pythonを使ってMySQLを操作する

Last updated at Posted at 2024-08-02

MySQLを操作するPythonドライバをインストールする。

py -m pip install mysql-connector-python

※Windowsではpyで動作する。

データベースの作成

import mysql.connector

print('Hello, World')

mydb = mysql.connector.connect(
  host="localhost",
  user="username",
  password="password"
)

mycursor = mydb.cursor()

mycursor.execute("CREATE DATABASE test_db")

print('Bye')

mycursor.executeでSQLコマンドを実行している。

実行結果後のデータベース
mysql> SHOW databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
| test_db            |
+--------------------+
5 rows in set (0.00 sec)

テーブルの作成

import mysql.connector

print('Hello, World')

mydb = mysql.connector.connect(
  host="localhost",
  user="username",
  password="password",
  database="test_db"
)

mycursor = mydb.cursor()

mycursor.execute("CREATE TABLE customers (name VARCHAR(255), address VARCHAR(255))")

print('Bye')
実行結果後のテーブル
mysql> SHOW tables;
+-------------------+
| Tables_in_test_db |
+-------------------+
| customers         |
+-------------------+
1 row in set (0.00 sec)

mysql> DESCRIBE customers;
+---------+--------------+------+-----+---------+-------+
| Field   | Type         | Null | Key | Default | Extra |
+---------+--------------+------+-----+---------+-------+
| name    | varchar(255) | YES  |     | NULL    |       |
| address | varchar(255) | YES  |     | NULL    |       |
+---------+--------------+------+-----+---------+-------+
2 rows in set (0.00 sec)

レコード挿入

import mysql.connector

mydb = mysql.connector.connect(
  host="localhost",
  user="root",
  password="root",
  database="test_db"
)

mycursor = mydb.cursor()

sql = "INSERT INTO customers (name, address) VALUES (%s, %s)"
val = ("John", "Highway 21")
mycursor.execute(sql, val)

mydb.commit()

print(mycursor.rowcount, "Record Inserted.")
実行結果後のテーブル
mysql> SELECT * FROM customers;
+------+------------+
| name | address    |
+------+------------+
| John | Highway 21 |
+------+------------+
1 row in set (0.00 sec)

テーブル表示

import mysql.connector

mydb = mysql.connector.connect(
  host="localhost",
  user="root",
  password="root",
  database="test_db"
)

mycursor = mydb.cursor()

mycursor.execute("SELECT * FROM customers")

myresult = mycursor.fetchall()

for x in myresult:
  print(x)

SELECT文の実行後、以下のエラーが発生し、MySQL文の実行ができなくなった。

AttributeError: cannot access submodule 'connector' of module 'mysql' (most likely due to a circular import)

※※2024/8/2追記
mysql.connector内とファイル名が重複するような名前だったためと考えられる。
(select.pyにしていた)
修正したが以下のエラーが発生したため引き続き調査中。

mysql.connector.errors.NotSupportedError: Authentication plugin 'caching_sha2_password' is not supported

※※2024/8/6追記
以下のページにて、エラーを解決。

引用

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?