สมัครเรียนโทร. 085-350-7540 , 084-88-00-255 , ntprintf@gmail.com

Tutorial Python MySQL

LM29PYTHON MYSQL LM30PYTHON MYSQL CREATE DATABASE LM31PYTHON MYSQL CREATE TABLE LM32PYTHON MYSQL INSERT INTO TABLE LM33PYTHON MYSQL SELECT FROM LM34PYTHON MYSQL WHERE LM35PYTHON MYSQL ORDER BY LM36PYTHON MYSQL DELETE FROM BY LM37PYTHON MYSQL DROP TABLE LM38PYTHON MYSQL UPDATE TABLE LM39PYTHON MYSQL LIMIT LM40PYTHON MYSQL JOIN

Python MySQL Insert Into Table

เรียนเขียนโปรแกรมง่ายๆกับexpert Programming Tutor ในบท MySQL Insert Into Table
เนื้อหาในบทนี้เราจะเรียนรู้การเเทรกตารางลงไปในตาราง ซึ่งง่ายมากๆยังไงเดี๋ยวเรามาเรียนรู้กันเลยดีกว่า
การแทรกลงในตาราง(
Insert Into Table)
ในการเติมตารางใน MySQL ให้ใช้คำสั่ง "INSERT INTO"
ตัวอย่าง
การแทรกบันทึกในตาราง "customer"

import mysql.connector
mydb = mysql.connector.connect(
    host="localhost",
    user="root",
    passwd="",
    database="myept")
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.")

ผลลัพธ์
ผลลัพธ์ 1 record

C:\Users\My Name>python demo_mysql_insert.py
1 record inserted.

สำคัญ!สังเกต mydb.commit() จำเป็นต้องทำการเปลี่ยนแปลงไม่เช่นนั้นจะไม่มีการเปลี่ยนแปลงใดๆ ในตาราง
การแทรกหลายแถว
ในการแทรกหลายแถวลงในตารางใช้เมธอด executeemany()พารามิเตอร์ที่สองของเมธอด executemany() คือรายการของ tuples ซึ่งมีข้อมูลที่นักเรียนต้องการแทรก
ตัวอย่าง
กรอกข้อมูลในตาราง"customer"

import mysql.connector
mydb = mysql.connector.connect(
  host="localhost",
  user="yourusername",
  passwd="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."

ผลลัพธ์
ผลลัพธ์ idle 13 was inserted
แสดงใน customer 13 was sql

C:\Users\My Name>python demo_mysql_insert_many.py
13 record was inserted.

รับ ID ที่ถูกแทรก
นักเรียนสามารถรับ id ของแถวที่คุณเพิ่งแทรกโดยขอให้วัตถุเคอร์เซอร์
หมายเหตุ: ถ้านักเรียนแทรกมากกว่าหนึ่งแถวจะมีการส่งคืน id ของแถวที่แทรกล่าสุด
ตัวอย่าง
แทรกหนึ่งแถวและส่งคืน ID

import mysql.connector
mydb = mysql.connector.connect(
  host="localhost",
  user="root",
  passwd="",
  database="myept"
)
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)

ผลลัพธ์
แสดงผลลัพธ์ 1 record id:15

C:\Users\My Name>python demo_mysql_insert_id.py
1 record inserted, ID: 15

แปลจากhttps://www.w3schools.com/python/python_mysql_insert.asp



บทความนี้อาจจะมีที่ผิด กรุณาตรวจสอบก่อนใช้

หากมีข้อผิดพลาด/ต้องการพูดคุยเพิ่มเติมเกี่ยวกับบทความนี้ กรุณาแจ้งที่ http://m.me/Expert.Programming.Tutor

ไม่อยากอ่าน Tutorial อยากมาเรียนเลยทำอย่างไร?

สมัครเรียน ONLINE ได้ทันทีที่ https://elearn.expert-programming-tutor.com

หรือติดต่อ

085-350-7540 (DTAC)
084-88-00-255 (AIS)
026-111-618
หรือทาง EMAIL: NTPRINTF@GMAIL.COM

แผนที่ ที่ตั้งของอาคารของเรา

C Article


C++ Article


Java Article


C#.NET Article


VB.NET Article


Python Article


Golang Article


JavaScript Article


Perl Article


Lua Article


Rust Article


Article


Python


Python Numpy


Python Machine Learning



แผนผังการเรียนเขียนโปรแกรม

Link อื่นๆ

Allow sites to save and read cookie data.
Cookies are small pieces of data created by sites you visit. They make your online experience easier by saving browsing information. We use cookies to improve your experience on our website. By browsing this website, you agree to our use of cookies.

Copyright (c) 2013 expert-programming-tutor.com. All rights reserved. | 085-350-7540 | 084-88-00-255 | ntprintf@gmail.com

ติดต่อเราได้ที่

085-350-7540 (DTAC)
084-88-00-255 (AIS)
026-111-618
หรือทาง EMAIL: NTPRINTF@GMAIL.COM
แผนที่ ที่ตั้งของอาคารของเรา