Đối với bài học này, tôi đã tạo thủ tục lưu sẵn
import mysql.connector
from mysql.connector import Error
try:
connection = mysql.connector.connect[host='localhost',
database='Electronics',
user='pynative',
password='pynative@#29']
cursor = connection.cursor[]
cursor.callproc['get_laptop', [1, ]]
# print results
print["Printing laptop details"]
for result in cursor.stored_results[]:
print[result.fetchall[]]
except mysql.connector.Error as error:
print["Failed to execute stored procedure: {}".format[error]]
finally:
if [connection.is_connected[]]:
cursor.close[]
connection.close[]
print["MySQL connection is closed"]
7 trong cơ sở dữ liệu “Điện tử”. Quy trình này tìm nạp chi tiết máy tính xách tay từ bảng Máy tính xách tay dựa trên id máy tính xách tay được truyền dưới dạng tham số INNếu một bảng không có trong máy chủ MySQL của bạn, bạn có thể tham khảo bài viết của chúng tôi để tạo một bảng MySQL từ Python
Bạn cũng có thể tải xuống tệp truy vấn SQL chứa các truy vấn SQL để tạo bảng và dữ liệu để bạn có thể sử dụng bảng này cho các thao tác SQL của mình
Nếu bạn đã biết thủ tục lưu trữ mà bạn muốn thực hiện, bạn có thể bỏ qua truy vấn sau. Nếu không, hãy mở bảng điều khiển MySQL và chạy truy vấn bên dưới để tạo Thủ tục lưu trữ MySQL
DELIMITER $
USE Electronics$
CREATE * from Laptop where id = d_id;
END $
DELIMITER ;
Bây giờ bạn đã biết thủ tục được lưu trữ để thực thi, vì vậy hãy chuyển sang ví dụ của chúng tôi
Ghi chú. Chúng tôi đang sử dụng mô-đun MySQL Connector Python để thực thi Quy trình lưu trữ MySQL
Các bước để thực thi Thủ tục lưu trữ MySQL trong Python
Để gọi thủ tục lưu trữ MySQL từ Python, bạn cần làm theo các bước sau. –
Cách thực thi thủ tục lưu sẵn MySQL trong Python
- Kết nối với MySQL từ Python
Tham khảo kết nối cơ sở dữ liệu MySQL trên Python để kết nối với cơ sở dữ liệu MySQL từ Python bằng mô-đun MySQL Connector
- Nhận đối tượng con trỏ từ kết nối
Tiếp theo, sử dụng phương thức
8 để tạo đối tượng con trỏ. Phương thức này tạo một đối tượngimport mysql.connector from mysql.connector import Error try: connection = mysql.connector.connect[host='localhost', database='Electronics', user='pynative', password='pynative@#29'] cursor = connection.cursor[] cursor.callproc['get_laptop', [1, ]] # print results print["Printing laptop details"] for result in cursor.stored_results[]: print[result.fetchall[]] except mysql.connector.Error as error: print["Failed to execute stored procedure: {}".format[error]] finally: if [connection.is_connected[]]: cursor.close[] connection.close[] print["MySQL connection is closed"]
9 mớiimport mysql.connector from mysql.connector import Error try: connection = mysql.connector.connect[host='localhost', database='Electronics', user='pynative', password='pynative@#29'] cursor = connection.cursor[] cursor.callproc['get_laptop', [1, ]] # print results print["Printing laptop details"] for result in cursor.stored_results[]: print[result.fetchall[]] except mysql.connector.Error as error: print["Failed to execute stored procedure: {}".format[error]] finally: if [connection.is_connected[]]: cursor.close[] connection.close[] print["MySQL connection is closed"]
- Thực hiện thủ tục được lưu trữ
Thực hiện thủ tục được lưu trữ bằng cách sử dụng
0. ở đây, bạn phải biết tên thủ tục được lưu trữ và các tham số IN và OUT của nó. Ví dụ,import mysql.connector from mysql.connector import Error try: connection = mysql.connector.connect[host='localhost', database='Electronics', user='pynative', password='pynative@#29'] cursor = connection.cursor[] cursor.callproc['get_laptop', [1, ]] # print results print["Printing laptop details"] for result in cursor.stored_results[]: print[result.fetchall[]] except mysql.connector.Error as error: print["Failed to execute stored procedure: {}".format[error]] finally: if [connection.is_connected[]]: cursor.close[] connection.close[] print["MySQL connection is closed"]
0import mysql.connector from mysql.connector import Error try: connection = mysql.connector.connect[host='localhost', database='Electronics', user='pynative', password='pynative@#29'] cursor = connection.cursor[] cursor.callproc['get_laptop', [1, ]] # print results print["Printing laptop details"] for result in cursor.stored_results[]: print[result.fetchall[]] except mysql.connector.Error as error: print["Failed to execute stored procedure: {}".format[error]] finally: if [connection.is_connected[]]: cursor.close[] connection.close[] print["MySQL connection is closed"]
- Tìm nạp kết quả
Sau khi quy trình được lưu trữ thực thi thành công, chúng tôi có thể trích xuất kết quả bằng cách sử dụng một
1import mysql.connector from mysql.connector import Error try: connection = mysql.connector.connect[host='localhost', database='Electronics', user='pynative', password='pynative@#29'] cursor = connection.cursor[] cursor.callproc['get_laptop', [1, ]] # print results print["Printing laptop details"] for result in cursor.stored_results[]: print[result.fetchall[]] except mysql.connector.Error as error: print["Failed to execute stored procedure: {}".format[error]] finally: if [connection.is_connected[]]: cursor.close[] connection.close[] print["MySQL connection is closed"]
- Đóng đối tượng con trỏ và đối tượng kết nối cơ sở dữ liệu
sử dụng phương pháp
2 vàimport mysql.connector from mysql.connector import Error try: connection = mysql.connector.connect[host='localhost', database='Electronics', user='pynative', password='pynative@#29'] cursor = connection.cursor[] cursor.callproc['get_laptop', [1, ]] # print results print["Printing laptop details"] for result in cursor.stored_results[]: print[result.fetchall[]] except mysql.connector.Error as error: print["Failed to execute stored procedure: {}".format[error]] finally: if [connection.is_connected[]]: cursor.close[] connection.close[] print["MySQL connection is closed"]
3 để đóng các kết nối mở sau khi công việc của bạn hoàn thànhimport mysql.connector from mysql.connector import Error try: connection = mysql.connector.connect[host='localhost', database='Electronics', user='pynative', password='pynative@#29'] cursor = connection.cursor[] cursor.callproc['get_laptop', [1, ]] # print results print["Printing laptop details"] for result in cursor.stored_results[]: print[result.fetchall[]] except mysql.connector.Error as error: print["Failed to execute stored procedure: {}".format[error]] finally: if [connection.is_connected[]]: cursor.close[] connection.close[] print["MySQL connection is closed"]
Bây giờ, hãy xem ví dụ. Trong ví dụ này, chúng ta sẽ thực thi thủ tục lưu sẵn
import mysql.connector
from mysql.connector import Error
try:
connection = mysql.connector.connect[host='localhost',
database='Electronics',
user='pynative',
password='pynative@#29']
cursor = connection.cursor[]
cursor.callproc['get_laptop', [1, ]]
# print results
print["Printing laptop details"]
for result in cursor.stored_results[]:
print[result.fetchall[]]
except mysql.connector.Error as error:
print["Failed to execute stored procedure: {}".format[error]]
finally:
if [connection.is_connected[]]:
cursor.close[]
connection.close[]
print["MySQL connection is closed"]
4 bằng Pythonimport mysql.connector
from mysql.connector import Error
try:
connection = mysql.connector.connect[host='localhost',
database='Electronics',
user='pynative',
password='pynative@#29']
cursor = connection.cursor[]
cursor.callproc['get_laptop', [1, ]]
# print results
print["Printing laptop details"]
for result in cursor.stored_results[]:
print[result.fetchall[]]
except mysql.connector.Error as error:
print["Failed to execute stored procedure: {}".format[error]]
finally:
if [connection.is_connected[]]:
cursor.close[]
connection.close[]
print["MySQL connection is closed"]
Bạn sẽ nhận được đầu ra sau
Printing laptop details [[1, 'Lenovo ThinkPad P71', 6459.0, datetime.date[2019, 8, 14]]] MySQL connection is closed
Ghi chú. Ngoài ra, hãy nắm bắt mọi ngoại lệ SQL có thể xảy ra trong quá trình này.
Sử dụng lớp Lỗi của mô-đun Trình kết nối MySQL, lớp này hiển thị cho chúng tôi lỗi khi chúng tôi không thể thực thi một thủ tục được lưu trữ. Ví dụ
import mysql.connector
from mysql.connector import Error
try:
connection = mysql.connector.connect[host='localhost',
database='Electronics',
user='pynative',
password='pynative@#29']
cursor = connection.cursor[]
cursor.callproc['get_laptop', [1, ]]
# print results
print["Printing laptop details"]
for result in cursor.stored_results[]:
print[result.fetchall[]]
except mysql.connector.Error as error:
print["Failed to execute stored procedure: {}".format[error]]
finally:
if [connection.is_connected[]]:
cursor.close[]
connection.close[]
print["MySQL connection is closed"]
5 khi tên người dùng hoặc mật khẩu sai. Phương thức callproc[] con trỏ
cú pháp
result_args = cursor.callproc[proc_name, args=[]]
Phương thức
import mysql.connector
from mysql.connector import Error
try:
connection = mysql.connector.connect[host='localhost',
database='Electronics',
user='pynative',
password='pynative@#29']
cursor = connection.cursor[]
cursor.callproc['get_laptop', [1, ]]
# print results
print["Printing laptop details"]
for result in cursor.stored_results[]:
print[result.fetchall[]]
except mysql.connector.Error as error:
print["Failed to execute stored procedure: {}".format[error]]
finally:
if [connection.is_connected[]]:
cursor.close[]
connection.close[]
print["MySQL connection is closed"]
6 gọi thủ tục được lưu trữ được đề cập trong đối số import mysql.connector
from mysql.connector import Error
try:
connection = mysql.connector.connect[host='localhost',
database='Electronics',
user='pynative',
password='pynative@#29']
cursor = connection.cursor[]
cursor.callproc['get_laptop', [1, ]]
# print results
print["Printing laptop details"]
for result in cursor.stored_results[]:
print[result.fetchall[]]
except mysql.connector.Error as error:
print["Failed to execute stored procedure: {}".format[error]]
finally:
if [connection.is_connected[]]:
cursor.close[]
connection.close[]
print["MySQL connection is closed"]
7. Chuỗi tham số import mysql.connector
from mysql.connector import Error
try:
connection = mysql.connector.connect[host='localhost',
database='Electronics',
user='pynative',
password='pynative@#29']
cursor = connection.cursor[]
cursor.callproc['get_laptop', [1, ]]
# print results
print["Printing laptop details"]
for result in cursor.stored_results[]:
print[result.fetchall[]]
except mysql.connector.Error as error:
print["Failed to execute stored procedure: {}".format[error]]
finally:
if [connection.is_connected[]]:
cursor.close[]
connection.close[]
print["MySQL connection is closed"]
8 phải chứa một mục nhập cho mỗi đối số mà thủ tục mong đợi. Ví dụ, thủ tục có thể có một hoặc nhiều tham số IN và OUTimport mysql.connector
from mysql.connector import Error
try:
connection = mysql.connector.connect[host='localhost',
database='Electronics',
user='pynative',
password='pynative@#29']
cursor = connection.cursor[]
cursor.callproc['get_laptop', [1, ]]
# print results
print["Printing laptop details"]
for result in cursor.stored_results[]:
print[result.fetchall[]]
except mysql.connector.Error as error:
print["Failed to execute stored procedure: {}".format[error]]
finally:
if [connection.is_connected[]]:
cursor.close[]
connection.close[]
print["MySQL connection is closed"]
6 trả về một bản sao đã sửa đổi của chuỗi đầu vào. Phương pháp này không thay đổi các tham số đầu vào. Tuy nhiên, Nó có thể thay thế các tham số đầu ra và đầu vào/đầu ra bằng các giá trị mới theo kết quả thực thiThủ tục được lưu trữ trả về đầu ra trong tập hợp kết quả và Nó được tự động tìm nạp và lưu trữ dưới dạng phiên bản
Printing laptop details [[1, 'Lenovo ThinkPad P71', 6459.0, datetime.date[2019, 8, 14]]] MySQL connection is closed0
Ví dụ: một thủ tục được lưu trữ "phép cộng" nhận hai tham số, cộng các giá trị và trả về tổng
import mysql.connector
from mysql.connector import Error
try:
connection = mysql.connector.connect[host='localhost',
database='Electronics',
user='pynative',
password='pynative@#29']
cursor = connection.cursor[]
cursor.callproc['get_laptop', [1, ]]
# print results
print["Printing laptop details"]
for result in cursor.stored_results[]:
print[result.fetchall[]]
except mysql.connector.Error as error:
print["Failed to execute stored procedure: {}".format[error]]
finally:
if [connection.is_connected[]]:
cursor.close[]
connection.close[]
print["MySQL connection is closed"]
5Ví dụ sau đây cho thấy cách thực hiện thủ tục Thêm này trong Python
import mysql.connector
from mysql.connector import Error
try:
connection = mysql.connector.connect[host='localhost',
database='Electronics',
user='pynative',
password='pynative@#29']
cursor = connection.cursor[]
cursor.callproc['get_laptop', [1, ]]
# print results
print["Printing laptop details"]
for result in cursor.stored_results[]:
print[result.fetchall[]]
except mysql.connector.Error as error:
print["Failed to execute stored procedure: {}".format[error]]
finally:
if [connection.is_connected[]]:
cursor.close[]
connection.close[]
print["MySQL connection is closed"]
6Bước tiếp theo
Để thực hành những gì bạn đã học trong bài viết này, vui lòng giải một dự án Bài tập về cơ sở dữ liệu Python để thực hành và thành thạo các thao tác với Cơ sở dữ liệu Python