Câu lệnh INSERT INTO được sử dụng để thêm một bản ghi vào bảng. Trong python, chúng ta có thể đề cập đến định dạng định dạng [%s] thay cho các giá trị
Chúng tôi cung cấp các giá trị thực ở dạng tuple trong phương thức exec[] của con trỏ
Xem xét ví dụ sau
Thí dụ
đầu ra
Chèn nhiều hàng
Chúng tôi cũng có thể chèn nhiều hàng cùng một lúc bằng tập lệnh python. Nhiều hàng được đề cập dưới dạng danh sách các bộ dữ liệu khác nhau
Mỗi phần tử của danh sách được coi là một hàng cụ thể, trong khi mỗi phần tử của bộ dữ liệu được coi là một giá trị cột cụ thể [thuộc tính]
Xem xét ví dụ sau
Thí dụ
đầu ra
ID hàng
Trong SQL, một hàng cụ thể được biểu thị bằng id chèn được gọi là id hàng. Chúng ta có thể lấy id hàng được chèn cuối cùng bằng cách sử dụng thuộc tính lastrowid của đối tượng con trỏ
MySQL là Hệ thống quản lý cơ sở dữ liệu quan hệ [RDBMS] trong khi Ngôn ngữ truy vấn có cấu trúc [SQL] là ngôn ngữ được sử dụng để xử lý RDBMS bằng các lệnh i. e Tạo, chèn, cập nhật và xóa dữ liệu khỏi cơ sở dữ liệu. Các lệnh SQL không phân biệt chữ hoa chữ thường. e CREATE và tạo signify cùng một lệnh
Ghi chú. Trước khi chèn dữ liệu vào cơ sở dữ liệu, chúng ta cần tạo một bảng. Để làm như vậy, hãy tham khảo Python. Bảng tạo MySQL
Chèn dữ liệu
Bạn có thể chèn một hàng hoặc nhiều hàng cùng một lúc. Cần có mã trình kết nối để kết nối các lệnh với cơ sở dữ liệu cụ thể
Truy vấn trình kết nối
# Enter the server name in host
# followed by your user and
# password along with the database
# name provided by you.
import
mysql.connector
7 details inserted1
7 details inserted2
7 details inserted3
7 details inserted4_______0_______5_______0_______2
7 details inserted7
7 details inserted8
7 details inserted4_______23_______0_______0_______2
# Enter the server name in host
27 details inserted8
7 details inserted4_______23_______5
7 details inserted2
# Enter the server name in host
77 details inserted8
7 details inserted4_______24_______0
7 details inserted2
# followed by your user and
2# followed by your user and
3
# followed by your user and
5
7 details inserted2
# followed by your user and
7Bây giờ, Chèn vào Truy vấn có thể được viết như sau
Thí dụ. Giả sử bản ghi trông như thế này -
# followed by your user and
8
7 details inserted2
# password along with the database
0# password along with the database
1
7 details inserted2
# password along with the database
3# password along with the database
4_______0_______8# password along with the database
6# password along with the database
7
# password along with the database
9
# name provided by you.
0
# name provided by you.
2_______26_______3# name provided by you.
4# password along with the database
7
# name provided by you.
7
# name provided by you.
8
đầu ra
1 details inserted
Để chèn nhiều giá trị cùng một lúc, phương pháp # name provided by you.
9 được sử dụng. Phương thức này lặp qua chuỗi các tham số, truyền tham số hiện tại cho phương thức thực thi
7 của đối tượngCode language: Python [python]
from mysql.connector import MySQLConnection, Error from python_mysql_dbconfig import read_db_config def insert_books[books]: query = "INSERT INTO books[title,isbn] " \ "VALUES[%s,%s]" try: db_config = read_db_config[] conn = MySQLConnection[**db_config] cursor = conn.cursor[] cursor.executemany[query, books] conn.commit[] except Error as e: print['Error:', e] finally: cursor.close[] conn.close[] def main[]: books = [['Harry Potter And The Order Of The Phoenix', '9780439358071'], ['Gone with the Wind', '9780446675536'], ['Pride and Prejudice [Modern Library Classics]', '9780679783268']] insert_books[books] if __name__ == '__main__': main[]
MySQLCursor
, Trình kết nối MySQL/Python dịch câu lệnh 3 thành câu lệnh chứa nhiều danh sách giá trịCode language: SQL [Structured Query Language] [sql]
INSERT INTO books[title,isbn] VALUES['Harry Potter And The Order Of The Phoenix', '9780439358071'], ['Gone with the Wind', '9780446675536'], ['Pride and Prejudice [Modern Library Classics]', '9780679783268'];