Một trình kết nối được sử dụng khi chúng ta phải sử dụng MySQL với các ngôn ngữ lập trình khác. Công việc của trình kết nối MySQL là cung cấp quyền truy cập vào Trình điều khiển MySQL cho ngôn ngữ được yêu cầu. Do đó, nó tạo ra kết nối giữa ngôn ngữ lập trình và Máy chủ MySQL
Bản tóm tắt. hướng dẫn này hướng dẫn bạn qua các bước cần thiết để cập nhật dữ liệu trong bảng MySQL bằng cách sử dụng MySQL Connector/Python API
Để cập nhật dữ liệu trong bảng MySQL trong Python, bạn làm theo các bước sau
- Kết nối với cơ sở dữ liệu bằng cách tạo một đối tượng
3 mới
Code language: Python [python]from mysql.connector import MySQLConnection, Error from python_mysql_dbconfig import read_db_config def update_book[book_id, title]: # read database configuration db_config = read_db_config[] # prepare query and data query = """ UPDATE books SET title = %s WHERE id = %s """ data = [title, book_id] try: conn = MySQLConnection[**db_config] # update book title cursor = conn.cursor[] cursor.execute[query, data] # accept the changes conn.commit[] except Error as error: print[error] finally: cursor.close[] conn.close[] if __name__ == '__main__': update_book[37, 'The Giant on the Hill *** TEST ***']
- Tạo một đối tượng
4 mới từ đối tượng
Code language: Python [python]from mysql.connector import MySQLConnection, Error from python_mysql_dbconfig import read_db_config def update_book[book_id, title]: # read database configuration db_config = read_db_config[] # prepare query and data query = """ UPDATE books SET title = %s WHERE id = %s """ data = [title, book_id] try: conn = MySQLConnection[**db_config] # update book title cursor = conn.cursor[] cursor.execute[query, data] # accept the changes conn.commit[] except Error as error: print[error] finally: cursor.close[] conn.close[] if __name__ == '__main__': update_book[37, 'The Giant on the Hill *** TEST ***']
3 và gọi phương thức
Code language: Python [python]from mysql.connector import MySQLConnection, Error from python_mysql_dbconfig import read_db_config def update_book[book_id, title]: # read database configuration db_config = read_db_config[] # prepare query and data query = """ UPDATE books SET title = %s WHERE id = %s """ data = [title, book_id] try: conn = MySQLConnection[**db_config] # update book title cursor = conn.cursor[] cursor.execute[query, data] # accept the changes conn.commit[] except Error as error: print[error] finally: cursor.close[] conn.close[] if __name__ == '__main__': update_book[37, 'The Giant on the Hill *** TEST ***']
6 của đối tượng
Code language: Python [python]from mysql.connector import MySQLConnection, Error from python_mysql_dbconfig import read_db_config def update_book[book_id, title]: # read database configuration db_config = read_db_config[] # prepare query and data query = """ UPDATE books SET title = %s WHERE id = %s """ data = [title, book_id] try: conn = MySQLConnection[**db_config] # update book title cursor = conn.cursor[] cursor.execute[query, data] # accept the changes conn.commit[] except Error as error: print[error] finally: cursor.close[] conn.close[] if __name__ == '__main__': update_book[37, 'The Giant on the Hill *** TEST ***']
4. Để chấp nhận các thay đổi, bạn gọi phương thức
Code language: Python [python]from mysql.connector import MySQLConnection, Error from python_mysql_dbconfig import read_db_config def update_book[book_id, title]: # read database configuration db_config = read_db_config[] # prepare query and data query = """ UPDATE books SET title = %s WHERE id = %s """ data = [title, book_id] try: conn = MySQLConnection[**db_config] # update book title cursor = conn.cursor[] cursor.execute[query, data] # accept the changes conn.commit[] except Error as error: print[error] finally: cursor.close[] conn.close[] if __name__ == '__main__': update_book[37, 'The Giant on the Hill *** TEST ***']
1 của đối tượngUPDATE books SET title = 'The Giant on the Hill *** TEST ***' WHERE id = 37
3 sau khi gọi phương thức
Code language: Python [python]from mysql.connector import MySQLConnection, Error from python_mysql_dbconfig import read_db_config def update_book[book_id, title]: # read database configuration db_config = read_db_config[] # prepare query and data query = """ UPDATE books SET title = %s WHERE id = %s """ data = [title, book_id] try: conn = MySQLConnection[**db_config] # update book title cursor = conn.cursor[] cursor.execute[query, data] # accept the changes conn.commit[] except Error as error: print[error] finally: cursor.close[] conn.close[] if __name__ == '__main__': update_book[37, 'The Giant on the Hill *** TEST ***']
6. Nếu không, sẽ không có thay đổi nào được thực hiện đối với cơ sở dữ liệu
Code language: Python [python]from mysql.connector import MySQLConnection, Error from python_mysql_dbconfig import read_db_config def update_book[book_id, title]: # read database configuration db_config = read_db_config[] # prepare query and data query = """ UPDATE books SET title = %s WHERE id = %s """ data = [title, book_id] try: conn = MySQLConnection[**db_config] # update book title cursor = conn.cursor[] cursor.execute[query, data] # accept the changes conn.commit[] except Error as error: print[error] finally: cursor.close[] conn.close[] if __name__ == '__main__': update_book[37, 'The Giant on the Hill *** TEST ***']
- Đóng con trỏ và kết nối cơ sở dữ liệu
Ví dụ sau cập nhật tên sách được chỉ định bởi id sách
Code language: Python [python]
from mysql.connector import MySQLConnection, Error from python_mysql_dbconfig import read_db_config def update_book[book_id, title]: # read database configuration db_config = read_db_config[] # prepare query and data query = """ UPDATE books SET title = %s WHERE id = %s """ data = [title, book_id] try: conn = MySQLConnection[**db_config] # update book title cursor = conn.cursor[] cursor.execute[query, data] # accept the changes conn.commit[] except Error as error: print[error] finally: cursor.close[] conn.close[] if __name__ == '__main__': update_book[37, 'The Giant on the Hill *** TEST ***']
Trong mô-đun này, chúng tôi đã sử dụng hàm
4 từ mô-đun python_mysql_dbconfig mà chúng tôi đã tạo trong phần kết nối với cơ sở dữ liệu từ hướng dẫn PythonCode language: SQL [Structured Query Language] [sql]
UPDATE books SET title = 'The Giant on the Hill *** TEST ***' WHERE id = 37
Chúng tôi đặt hai phần giữ chỗ [%] bên trong câu lệnh CẬP NHẬT, một cho tên sách và một cho id cuốn sách. Chúng tôi đã chuyển cả hai câu lệnh
5 [Code language: SQL [Structured Query Language] [sql]
UPDATE books SET title = 'The Giant on the Hill *** TEST ***' WHERE id = 37
6 ] và bộ dữ liệuCode language: SQL [Structured Query Language] [sql]
UPDATE books SET title = 'The Giant on the Hill *** TEST ***' WHERE id = 37
7 cho phương thức
UPDATE books SET title = 'The Giant on the Hill *** TEST ***' WHERE id = 37
Code language: SQL [Structured Query Language] [sql]
6. Trình kết nối sẽ diễn giải truy vấn như sauCode language: Python [python]
from mysql.connector import MySQLConnection, Error from python_mysql_dbconfig import read_db_config def update_book[book_id, title]: # read database configuration db_config = read_db_config[] # prepare query and data query = """ UPDATE books SET title = %s WHERE id = %s """ data = [title, book_id] try: conn = MySQLConnection[**db_config] # update book title cursor = conn.cursor[] cursor.execute[query, data] # accept the changes conn.commit[] except Error as error: print[error] finally: cursor.close[] conn.close[] if __name__ == '__main__': update_book[37, 'The Giant on the Hill *** TEST ***']
Code language: SQL [Structured Query Language] [sql]
UPDATE books SET title = 'The Giant on the Hill *** TEST ***' WHERE id = 37
Điều quan trọng là phải hiểu rằng chúng ta phải luôn sử dụng trình giữ chỗ [
9] bên trong bất kỳ câu lệnh SQL nào có chứa đầu vào từ người dùng. Điều này giúp chúng tôi ngăn chặn SQL injectionCode language: SQL [Structured Query Language] [sql]
UPDATE books SET title = 'The Giant on the Hill *** TEST ***' WHERE id = 37
CẬP NHẬT Thao tác trên bất kỳ cơ sở dữ liệu nào cập nhật một hoặc nhiều bản ghi đã có sẵn trong cơ sở dữ liệu. Bạn có thể cập nhật giá trị của các bản ghi hiện có trong MySQL bằng cách sử dụng câu lệnh UPDATE. Để cập nhật các hàng cụ thể, bạn cần sử dụng mệnh đề WHERE cùng với nó
cú pháp
Sau đây là cú pháp của câu lệnh UPDATE trong MySQL −
________số 8Bạn có thể kết hợp N số điều kiện bằng toán tử AND hoặc OR
Thí dụ
Giả sử chúng ta đã tạo một bảng trong MySQL với tên EMPLOYEES là -
mysql> CREATE TABLE EMPLOYEE[ FIRST_NAME CHAR[20] NOT NULL, LAST_NAME CHAR[20], AGE INT, SEX CHAR[1], INCOME FLOAT ]; Query OK, 0 rows affected [0.36 sec]
Và nếu chúng ta đã chèn 4 bản ghi vào nó bằng cách sử dụng câu lệnh INSERT như -
0Code language: Python [python]
from mysql.connector import MySQLConnection, Error from python_mysql_dbconfig import read_db_config def update_book[book_id, title]: # read database configuration db_config = read_db_config[] # prepare query and data query = """ UPDATE books SET title = %s WHERE id = %s """ data = [title, book_id] try: conn = MySQLConnection[**db_config] # update book title cursor = conn.cursor[] cursor.execute[query, data] # accept the changes conn.commit[] except Error as error: print[error] finally: cursor.close[] conn.close[] if __name__ == '__main__': update_book[37, 'The Giant on the Hill *** TEST ***']
Câu lệnh MySQL sau tăng tuổi của tất cả nhân viên nam thêm một năm -
1Code language: Python [python]
from mysql.connector import MySQLConnection, Error from python_mysql_dbconfig import read_db_config def update_book[book_id, title]: # read database configuration db_config = read_db_config[] # prepare query and data query = """ UPDATE books SET title = %s WHERE id = %s """ data = [title, book_id] try: conn = MySQLConnection[**db_config] # update book title cursor = conn.cursor[] cursor.execute[query, data] # accept the changes conn.commit[] except Error as error: print[error] finally: cursor.close[] conn.close[] if __name__ == '__main__': update_book[37, 'The Giant on the Hill *** TEST ***']
Nếu bạn truy xuất nội dung của bảng, bạn có thể thấy các giá trị được cập nhật dưới dạng -
2Code language: Python [python]
from mysql.connector import MySQLConnection, Error from python_mysql_dbconfig import read_db_config def update_book[book_id, title]: # read database configuration db_config = read_db_config[] # prepare query and data query = """ UPDATE books SET title = %s WHERE id = %s """ data = [title, book_id] try: conn = MySQLConnection[**db_config] # update book title cursor = conn.cursor[] cursor.execute[query, data] # accept the changes conn.commit[] except Error as error: print[error] finally: cursor.close[] conn.close[] if __name__ == '__main__': update_book[37, 'The Giant on the Hill *** TEST ***']
Cập nhật nội dung của bảng bằng Python
Để cập nhật các bản ghi trong một bảng trong MySQL bằng python -
nhập mysql. gói kết nối
Tạo một đối tượng kết nối bằng mysql. kết nối. connect[], bằng cách chuyển tên người dùng, mật khẩu, máy chủ [mặc định tùy chọn. localhost] và, cơ sở dữ liệu [tùy chọn] làm tham số cho nó