Hướng dẫn functions and procedures python - các hàm và thủ tục python

Bài trước mình đã giới thiệu với mọi người thao tác đầy đủ CRUD trên MySQL qua Python rồi, tiếp tục bài này mình sẽ hướng dẫn mọi người cách gọi procedure trong MySQL bằng Python.

Nội dung chính ShowShow

  • 1, Sử dụng phương thức execute().
  • 2, Sử dụng phương thức callproc().
  • Definition¶
  • Easy example¶
  • Example 2 - Use an argument¶
  • Example 3 - Use two arguments¶
  • Example 4 - Using a list as an argument¶
  • Example 5 - Using global variables¶
  • Key points¶
  • What is the difference between a function and a procedure in Python?
  • What is difference procedure and function?
  • What is the function in Python?
  • What are the 4 types of functions in Python?

Nội dung chính

  • 1, Sử dụng phương thức execute().
  • 2, Sử dụng phương thức callproc().
  • Definition¶
  • Easy example¶
  • Example 2 - Use an argument¶
  • Example 3 - Use two arguments¶
  • Example 4 - Using a list as an argument¶
  • Example 5 - Using global variables¶
  • Key points¶
  • What is the difference between a function and a procedure in Python?
  • What is difference procedure and function?
  • What is the function in Python?
  • What are the 4 types of functions in Python?

Nội dung chính

  • 1, Sử dụng phương thức execute().
  • 2, Sử dụng phương thức callproc().
  • Definition¶
  • Easy example¶
  • Example 2 - Use an argument¶
  • Example 3 - Use two arguments¶
  • Example 4 - Using a list as an argument¶
  • Example 5 - Using global variables¶
  • Key points¶
  • What is the difference between a function and a procedure in Python?
  • What is difference procedure and function?
  • What is the function in Python?
  • What are the 4 types of functions in Python?

Nội dung chính

DELIMITER $$

CREATE DEFINER=`root`@`localhost` PROCEDURE `get_all_user` ()  NO SQL
SELECT *  FROM users$$

DELIMITER ;

What is difference procedure and function?

1, Sử dụng phương thức execute().

Example 2 - Use an argument¶

VD::

import pymysql

connection = pymysql.connect('localhost', 'root', '', 'pymysql')

try:
    with connection.cursor() as cursor:
        
        sql = "CALL get_all_user()"
        result_count = cursor.execute(sql)

        print('Data Count: ' + str(result_count))

        print(cursor.fetchall())

finally:
    connection.close()

Để gọi được procedure thì trong database của bạn cần có procedure trước đã, ở đây mình sẽ tạo ra một procedure có tên là

import pymysql

connection = pymysql.connect('localhost', 'root', '', 'pymysql')

try:
    with connection.cursor() as cursor:
        
        sql = "CALL get_all_user()"
        result_count = cursor.execute(sql)

        print('Data Count: ' + str(result_count))

        print(cursor.fetchall())

finally:
    connection.close()
6 để lấy ra tất cả users có trong table users.

Data Count: 3

(
 (5, '[email protected]', '123456'),
 (6, '[email protected]', '123456'),
 (7, '[email protected]', '123456')
)

2, Sử dụng phương thức callproc().

Tiếp đó để gọi được procedure thì mọi người có thể sử dụng một trong hai cách sau:

cursor.callproc(proc_name, args)

Đối với cách này thì mọi ngừời vẫn thực hiện như bình thường đó là truyền cú pháp gọi procedure vào trong phương thức

import pymysql

connection = pymysql.connect('localhost', 'root', '', 'pymysql')

try:
    with connection.cursor() as cursor:
        
        sql = "CALL get_all_user()"
        result_count = cursor.execute(sql)

        print('Data Count: ' + str(result_count))

        print(cursor.fetchall())

finally:
    connection.close()
7.

  • Kết quả trả về:
  • Bạn cũng có thể call procedure qua phương thức callproc() với cú pháp như sau:

Trong đó:: Mình sẽ gọi procedure

import pymysql

connection = pymysql.connect('localhost', 'root', '', 'pymysql')

try:
    with connection.cursor() as cursor:
        
        sql = "CALL get_all_user()"
        result_count = cursor.execute(sql)

        print('Data Count: ' + str(result_count))

        print(cursor.fetchall())

finally:
    connection.close()
6 qua phương thức
Data Count: 3

(
 (5, '[email protected]', '123456'),
 (6, '[email protected]', '123456'),
 (7, '[email protected]', '123456')
)
1.

import pymysql

connection = pymysql.connect('localhost', 'root', '', 'pymysql')

try:
    with connection.cursor() as cursor:

        cursor.callproc('get_all_user')

        print(cursor.fetchall())

finally:
    connection.close()

Để gọi được procedure thì trong database của bạn cần có procedure trước đã, ở đây mình sẽ tạo ra một procedure có tên là

import pymysql

connection = pymysql.connect('localhost', 'root', '', 'pymysql')

try:
    with connection.cursor() as cursor:
        
        sql = "CALL get_all_user()"
        result_count = cursor.execute(sql)

        print('Data Count: ' + str(result_count))

        print(cursor.fetchall())

finally:
    connection.close()
6 để lấy ra tất cả users có trong table users.

(
 (5, '[email protected]', '123456'),
 (6, '[email protected]', '123456'),
 (7, '[email protected]', '123456')
)
Tiếp đó để gọi được procedure thì mọi người có thể sử dụng một trong hai cách sau:

Đối với cách này thì mọi ngừời vẫn thực hiện như bình thường đó là truyền cú pháp gọi procedure vào trong phương thức

import pymysql

connection = pymysql.connect('localhost', 'root', '', 'pymysql')

try:
    with connection.cursor() as cursor:
        
        sql = "CALL get_all_user()"
        result_count = cursor.execute(sql)

        print('Data Count: ' + str(result_count))

        print(cursor.fetchall())

finally:
    connection.close()
7.

Kết quả trả về:

Definition¶

Bạn cũng có thể call procedure qua phương thức callproc() với cú pháp như sau:procedure name. We can call the block of code from anywhere in the program to execute the instructions it contains. We can also pass values to the procedure to change how it works.

Nội dung chính

  • What is difference procedure and function?
  • Easy example¶
  • Example 2 - Use an argument¶
  • Example 3 - Use two arguments¶
  • Example 4 - Using a list as an argument¶
  • Example 5 - Using global variables¶
  • Key points¶
  • What is the difference between a function and a procedure in Python?
  • What is difference procedure and function?
  • What is the function in Python?
  • What are the 4 types of functions in Python?

Nội dung chính

What is difference procedure and function?

Easy example¶

def showMenu():
    print('Main Menu')
    print('1. Play game')
    print('2. View high scores')
    print('3. Quit')

showMenu()
Main Menu
1. Play game
2. View high scores
3. Quit

Nội dung chính

What is difference procedure and function?

Nội dung chính

What is difference procedure and function?

Example 2 - Use an argument¶

#define a procedure
def procedureName(arg1, arg2, ...):
    print('put instructions here')

#call the procedure
procedureName()

Để gọi được procedure thì trong database của bạn cần có procedure trước đã, ở đây mình sẽ tạo ra một procedure có tên là import pymysql connection = pymysql.connect('localhost', 'root', '', 'pymysql') try: with connection.cursor() as cursor: sql = "CALL get_all_user()" result_count = cursor.execute(sql) print('Data Count: ' + str(result_count)) print(cursor.fetchall()) finally: connection.close() 6 để lấy ra tất cả users có trong table users.

Example 2 - Use an argument¶

def storyStart(name):
    print('Once upon a time, ' + name + ' was imprisoned in a castle.')
    print('They were desperate to escape, but couldn\'t.')

userName = input('What is your name? ')

storyStart(userName)
import pymysql

connection = pymysql.connect('localhost', 'root', '', 'pymysql')

try:
    with connection.cursor() as cursor:
        
        sql = "CALL get_all_user()"
        result_count = cursor.execute(sql)

        print('Data Count: ' + str(result_count))

        print(cursor.fetchall())

finally:
    connection.close()
0

Example 3 - Use two arguments¶

Tiếp đó để gọi được procedure thì mọi người có thể sử dụng một trong hai cách sau:

import pymysql

connection = pymysql.connect('localhost', 'root', '', 'pymysql')

try:
    with connection.cursor() as cursor:
        
        sql = "CALL get_all_user()"
        result_count = cursor.execute(sql)

        print('Data Count: ' + str(result_count))

        print(cursor.fetchall())

finally:
    connection.close()
1
import pymysql

connection = pymysql.connect('localhost', 'root', '', 'pymysql')

try:
    with connection.cursor() as cursor:
        
        sql = "CALL get_all_user()"
        result_count = cursor.execute(sql)

        print('Data Count: ' + str(result_count))

        print(cursor.fetchall())

finally:
    connection.close()
2

Example 4 - Using a list as an argument¶

import pymysql

connection = pymysql.connect('localhost', 'root', '', 'pymysql')

try:
    with connection.cursor() as cursor:
        
        sql = "CALL get_all_user()"
        result_count = cursor.execute(sql)

        print('Data Count: ' + str(result_count))

        print(cursor.fetchall())

finally:
    connection.close()
3
import pymysql

connection = pymysql.connect('localhost', 'root', '', 'pymysql')

try:
    with connection.cursor() as cursor:
        
        sql = "CALL get_all_user()"
        result_count = cursor.execute(sql)

        print('Data Count: ' + str(result_count))

        print(cursor.fetchall())

finally:
    connection.close()
4

Example 5 - Using global variables¶

import pymysql

connection = pymysql.connect('localhost', 'root', '', 'pymysql')

try:
    with connection.cursor() as cursor:
        
        sql = "CALL get_all_user()"
        result_count = cursor.execute(sql)

        print('Data Count: ' + str(result_count))

        print(cursor.fetchall())

finally:
    connection.close()
5

Nội dung chính

What is difference procedure and function?

Key points¶

What is the difference between a function and a procedure in Python?

What is difference procedure and function? decompose a program into smaller parts, these will usually end up getting programmed in functions or procedures.

What is the difference between a function and a procedure in Python?

What is difference procedure and function? abstracting your program. If you can think of parts of the program that are similar, then it is best to abstract them into their own procedure or function.

Nội dung chính

What is difference procedure and function?

Sự khác biệt giữa một hàm và một thủ tục trong Python là gì?

Một thủ tục thực hiện một nhiệm vụ, trong khi một hàm tạo ra thông tin.Các chức năng khác với các quy trình trong các chức năng đó trả về các giá trị, không giống như các thủ tục không.Tuy nhiên, các tham số có thể được chuyển cho cả quy trình và chức năng.. Functions differ from procedures in that functions return values, unlike procedures which do not. However, parameters can be passed to both procedures and functions.

Quy trình và chức năng khác biệt là gì?

Sự khác biệt giữa chức năng và thủ tục.

Chức năng trong Python là gì?

Một hàm là một khối mã chỉ chạy khi nó được gọi.Bạn có thể truyền dữ liệu, được gọi là tham số, thành một hàm.Một chức năng có thể trả về dữ liệu như là kết quả.a block of code which only runs when it is called. You can pass data, known as parameters, into a function. A function can return data as a result.

4 loại chức năng trong Python là gì?

Sau đây là các loại chức năng Python khác nhau:..

Chức năng tích hợp Python ..

Chức năng đệ quy Python ..

Chức năng Python Lambda ..

Các chức năng do người dùng Python xác định ..