Python mysql cập nhật truy vấn tham số hóa

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

  1. Kết nối với cơ sở dữ liệu bằng cách tạo một đối tượng

    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: Python (python)
    3 mới
  2. Tạo một đối tượng

    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: Python (python)
    4 mới từ đối tượng

    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: Python (python)
    3 và gọi phương thức

    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: Python (python)
    6 của đối tượng

    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: Python (python)
    4. Để chấp nhận các thay đổi, bạn gọi phương thức

    UPDATE books SET title = 'The Giant on the Hill *** TEST ***' WHERE id = 37

    Code language: SQL (Structured Query Language) (sql)
    1 của đối tượng

    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: Python (python)
    3 sau khi gọi phương thức

    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: Python (python)
    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
  3. Đó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

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: Python (python)

Trong mô-đun này, chúng tôi đã sử dụng hàm

UPDATE books SET title = 'The Giant on the Hill *** TEST ***' WHERE id = 37

Code language: SQL (Structured Query Language) (sql)
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 Python

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 

UPDATE books SET title = 'The Giant on the Hill *** TEST ***' WHERE id = 37

Code language: SQL (Structured Query Language) (sql)
5 (

UPDATE books SET title = 'The Giant on the Hill *** TEST ***' WHERE id = 37

Code language: SQL (Structured Query Language) (sql)
6 ) và  bộ dữ liệu

UPDATE books SET title = 'The Giant on the Hill *** TEST ***' WHERE id = 37

Code language: SQL (Structured Query Language) (sql)
7 cho phương thức 

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: Python (python)
6. Trình kết nối sẽ diễn giải truy vấn như sau

UPDATE books SET title = 'The Giant on the Hill *** TEST ***' WHERE id = 37

Code language: SQL (Structured Query Language) (sql)

Đ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ỗ (

UPDATE books SET title = 'The Giant on the Hill *** TEST ***' WHERE id = 37

Code language: SQL (Structured Query Language) (sql)
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 injection

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ố 8

Bạ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ư -

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: Python (python)
0

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 -

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: Python (python)
1

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 -

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: Python (python)
2

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ó