Đố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 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 để tạo đối tượng con trỏ. Phương thức này tạo một đối 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") 9 mới - 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 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") 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") 0 - 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 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") 1 - Đó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 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") 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ành Thực thi thủ tục lưu trữ MySQL trong pythonBâ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 closed 0Ví 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
Làm cách nào để thực thi thủ tục lưu trữ SQL Server trong Python?
Cách thực thi thủ tục lưu trữ SQL Server từ Python? . THỦ TỤC TẠO [Bán hàng]. [Mtb_GetCustomers] @SearchText nvarchar(100), @MaximumRowsToReturn int BẮT ĐẦU c. Mã khách hàng, c. Tên khách hàng TỪ bán hàng. Khách hàng AS c ĐẶT HÀNG THEO c. Tên khách hàng; ĐI. Chương trình Python. . nhập pyodbc dưới dạng po. # Biến kết nối
Tôi có thể gọi tập lệnh Python từ SQL không?
Dịch vụ máy học SQL Server cho phép bạn thực thi tập lệnh Python và R trong cơ sở dữ liệu . Bạn có thể sử dụng nó để chuẩn bị và làm sạch dữ liệu, thực hiện kỹ thuật tính năng cũng như đào tạo, đánh giá và triển khai các mô hình máy học trong cơ sở dữ liệu.
Làm cách nào để truyền tham số cho tập lệnh SQL trong Python?
Bạn có thể chuyển tham số/đối số cho câu lệnh SQL của mình bằng cách tạo chuỗi SQL theo chương trình bằng Scala/Python và chuyển nó tới sqlContext. sql (chuỗi). Lưu ý 's' trước chữ """ đầu tiên. . thông số của bạn. val p1 = "('0001','0002','0003')". Xây dựng truy vấn. . Sau đó, bạn có thể truy vấn nó
Tôi có thể sử dụng Python với SQL Server không?
Bạn có thể kết nối với Cơ sở dữ liệu SQL bằng Python trên Windows, Linux hoặc macOS . |