Pythonチュートリアル

Pythonホーム Pythonイントロ Pythonはじめに Python構文 Pythonコメント Python変数 Pythonデータ型 Python番号 Pythonキャスティング Python文字列 Pythonブール値 Python演算子 Pythonリスト Pythonタプル Pythonセット Python辞書 Python If ... Else PythonのWhileループ PythonForループ Python関数 Python Lambda Python配列 Pythonクラス/オブジェクト Pythonの継承 Pythonイテレータ Pythonスコープ Pythonモジュール Pythonの日付 Python数学 Python JSON Python RegEx Python PIP Python試してみてください... Pythonユーザー入力 Python文字列フォーマット

ファイル処理

Pythonファイル処理 Python読み取りファイル Python書き込み/ファイルの作成 Pythonファイルの削除

Pythonモジュール

NumPyチュートリアル パンダ攻略 Scipyチュートリアル

Python Matplotlib

Matplotlibイントロ Matplotlibはじめに Matplotlib Pyplot Matplotlibプロット Matplotlibマーカー Matplotlibライン Matplotlibラベル Matplotlibグリッド Matplotlibサブプロット Matplotlib散布図 Matplotlibバー Matplotlibヒストグラム Matplotlib円グラフ

機械学習

入門 平均中央値モード 標準偏差 パーセンタイル データ配信 正規データ分布 散布図 線形回帰 多項式回帰 重回帰 規模 トレーニング/テスト デシジョンツリー

Python MySQL

MySQLはじめに MySQLデータベースの作成 MySQLテーブルの作成 MySQL挿入 MySQL Select MySQL Where MySQL Order By MySQL削除 MySQLドロップテーブル MySQLアップデート MySQLの制限 MySQL参加

Python MongoDB

MongoDBはじめに MongoDBデータベースの作成 MongoDBCreateコレクション MongoDBインサート MongoDB検索 MongoDBクエリ MongoDBソート MongoDB削除 MongoDBドロップコレクション MongoDBアップデート MongoDBの制限

Pythonリファレンス

Pythonの概要 Python組み込み関数 Python文字列メソッド Pythonリストメソッド Python辞書メソッド Pythonタプルメソッド Pythonセットメソッド Pythonファイルメソッド Pythonキーワード Pythonの例外 Python用語集

モジュールリファレンス

ランダムモジュール リクエストモジュール 統計モジュール 数学モジュール cMathモジュール

Pythonハウツー

リストの重複を削除する 文字列を逆にする 2つの数字を追加する

Pythonの例

Pythonの例 Pythonコンパイラ Python演習 Pythonクイズ Python証明書

PythonMySQLをテーブルに挿入


テーブルに挿入

MySQLでテーブルを埋めるには、「INSERTINTO」ステートメントを使用します。

「customers」テーブルにレコードを挿入します。

import mysql.connector

mydb = mysql.connector.connect(
  host="localhost",
  user="yourusername",
  password="yourpassword",
  database="mydatabase"
)

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.")

重要!:次のステートメントに注意してください: mydb.commit()変更を加える必要があります。変更しない場合、テーブルは変更されません。


複数の行を挿入

テーブルに複数の行を挿入するには、この executemany()メソッドを使用します。

メソッドの2番目のパラメーターは、executemany()挿入するデータを含むタプルのリストです。

「顧客」テーブルにデータを入力します。

import mysql.connector

mydb = mysql.connector.connect(
  host="localhost",
  user="yourusername",
  password="yourpassword",
  database="mydatabase"
)

mycursor = mydb.cursor()

sql = "INSERT INTO customers (name, address) VALUES (%s, %s)"
val = [
  ('Peter', 'Lowstreet 4'),
  ('Amy', 'Apple st 652'),
  ('Hannah', 'Mountain 21'),
  ('Michael', 'Valley 345'),
  ('Sandy', 'Ocean blvd 2'),
  ('Betty', 'Green Grass 1'),
  ('Richard', 'Sky st 331'),
  ('Susan', 'One way 98'),
  ('Vicky', 'Yellow Garden 2'),
  ('Ben', 'Park Lane 38'),
  ('William', 'Central st 954'),
  ('Chuck', 'Main Road 989'),
  ('Viola', 'Sideway 1633')
]

mycursor.executemany(sql, val)

mydb.commit()

print(mycursor.rowcount, "was inserted.")

挿入されたIDを取得する

カーソルオブジェクトに問い合わせることで、挿入したばかりの行のIDを取得できます。

注:複数の行を挿入すると、最後に挿入された行のIDが返されます。

1行を挿入し、IDを返します。

import mysql.connector

mydb = mysql.connector.connect(
  host="localhost",
  user="yourusername",
  password="yourpassword",
  database="mydatabase"
)

mycursor = mydb.cursor()

sql = "INSERT INTO customers (name, address) VALUES (%s, %s)"
val = ("Michelle", "Blue Village")
mycursor.execute(sql, val)

mydb.commit()

print("1 record inserted, ID:", mycursor.lastrowid)