Làm cách nào để tìm nạp nhiều hàng bằng con trỏ trong MySQL?

Bài viết này chứng minh việc sử dụng các phương thức lớp con trỏ của Python là fetchall(),

Connected to database
Total rows are:   5

Printing each row
Id:  1
Name:  Emma
Email:  [email protected]
Salary:  12000.0


Id:  2
Name:  Scott
Email:  [email protected]
Salary:  22000.0


Id:  3
Name:  Jessa
Email:  [email protected]
Salary:  18000.0


Id:  4
Name:  Mike
Email:  [email protected]
Salary:  13000.0


Id:  5
Name:  Ricky
Email:  [email protected]
Salary:  19000.0


The Sqlite connection is closed
0 và
Connected to database
Total rows are:   5

Printing each row
Id:  1
Name:  Emma
Email:  [email protected]
Salary:  12000.0


Id:  2
Name:  Scott
Email:  [email protected]
Salary:  22000.0


Id:  3
Name:  Jessa
Email:  [email protected]
Salary:  18000.0


Id:  4
Name:  Mike
Email:  [email protected]
Salary:  13000.0


Id:  5
Name:  Ricky
Email:  [email protected]
Salary:  19000.0


The Sqlite connection is closed
1 để truy xuất các hàng từ một bảng cơ sở dữ liệu. Bài viết này áp dụng cho tất cả các cơ sở dữ liệu quan hệ, ví dụ SQLite, MySQL, PostgreSQL

cũng đọc

  • Giải bài tập Python SQLite
  • Đọc Hướng dẫn Python SQLite (Hướng dẫn đầy đủ)

Đặc tả API cơ sở dữ liệu Python v2. 0 (PEP 249) đã được thiết kế để khuyến khích và duy trì sự tương đồng giữa các mô-đun Python được sử dụng để truy cập cơ sở dữ liệu. Vì vậy, không quan trọng bạn sử dụng cơ sở dữ liệu nào. Có thể là cú pháp MySQL, PostgreSQL và SQLite, cú pháp của các hàm và cách truy cập cơ sở dữ liệu quan hệ đều giống nhau trong tất cả các mô-đun cơ sở dữ liệu

Chúng tôi thường sử dụng mô-đun Python sau để làm việc với Cơ sở dữ liệu

DatabaseModuleMySQLMySQL Connector PythonPostgreSQLPsycopg2SQLitesqlite3

Trên tất cả các mô-đun tuân thủ Đặc tả API cơ sở dữ liệu Python v2. 0 (PEP 249)

Bài học này sẽ hướng dẫn cách sử dụng fetchall(),

Connected to database
Total rows are:   5

Printing each row
Id:  1
Name:  Emma
Email:  [email protected]
Salary:  12000.0


Id:  2
Name:  Scott
Email:  [email protected]
Salary:  22000.0


Id:  3
Name:  Jessa
Email:  [email protected]
Salary:  18000.0


Id:  4
Name:  Mike
Email:  [email protected]
Salary:  13000.0


Id:  5
Name:  Ricky
Email:  [email protected]
Salary:  19000.0


The Sqlite connection is closed
0 và
Connected to database
Total rows are:   5

Printing each row
Id:  1
Name:  Emma
Email:  [email protected]
Salary:  12000.0


Id:  2
Name:  Scott
Email:  [email protected]
Salary:  22000.0


Id:  3
Name:  Jessa
Email:  [email protected]
Salary:  18000.0


Id:  4
Name:  Mike
Email:  [email protected]
Salary:  13000.0


Id:  5
Name:  Ricky
Email:  [email protected]
Salary:  19000.0


The Sqlite connection is closed
1 để lấy dữ liệu từ cơ sở dữ liệu MySQL, PostgreSQL, SQLite

Trước tiên hãy hiểu công dụng của hàm fetchall,fetchmany(),fetchone() là gì

Connected to database
Total rows are:   5

Printing each row
Id:  1
Name:  Emma
Email:  [email protected]
Salary:  12000.0


Id:  2
Name:  Scott
Email:  [email protected]
Salary:  22000.0


Id:  3
Name:  Jessa
Email:  [email protected]
Salary:  18000.0


Id:  4
Name:  Mike
Email:  [email protected]
Salary:  13000.0


Id:  5
Name:  Ricky
Email:  [email protected]
Salary:  19000.0


The Sqlite connection is closed
5 tìm nạp tất cả các hàng của kết quả truy vấn. Nó trả về tất cả các hàng dưới dạng danh sách các bộ dữ liệu. Một danh sách trống được trả về nếu không có bản ghi để tìm nạp

Connected to database
Total rows are:   5

Printing each row
Id:  1
Name:  Emma
Email:  [email protected]
Salary:  12000.0


Id:  2
Name:  Scott
Email:  [email protected]
Salary:  22000.0


Id:  3
Name:  Jessa
Email:  [email protected]
Salary:  18000.0


Id:  4
Name:  Mike
Email:  [email protected]
Salary:  13000.0


Id:  5
Name:  Ricky
Email:  [email protected]
Salary:  19000.0


The Sqlite connection is closed
6 trả về số hàng được chỉ định bởi đối số
Connected to database
Total rows are:   5

Printing each row
Id:  1
Name:  Emma
Email:  [email protected]
Salary:  12000.0


Id:  2
Name:  Scott
Email:  [email protected]
Salary:  22000.0


Id:  3
Name:  Jessa
Email:  [email protected]
Salary:  18000.0


Id:  4
Name:  Mike
Email:  [email protected]
Salary:  13000.0


Id:  5
Name:  Ricky
Email:  [email protected]
Salary:  19000.0


The Sqlite connection is closed
7. Khi được gọi lặp lại, phương thức này sẽ tìm nạp tập hợp các hàng tiếp theo của kết quả truy vấn và trả về một danh sách các bộ dữ liệu. Nếu không còn hàng nào nữa, nó sẽ trả về một danh sách trống

Phương thức

Connected to database
Total rows are:   5

Printing each row
Id:  1
Name:  Emma
Email:  [email protected]
Salary:  12000.0


Id:  2
Name:  Scott
Email:  [email protected]
Salary:  22000.0


Id:  3
Name:  Jessa
Email:  [email protected]
Salary:  18000.0


Id:  4
Name:  Mike
Email:  [email protected]
Salary:  13000.0


Id:  5
Name:  Ricky
Email:  [email protected]
Salary:  19000.0


The Sqlite connection is closed
8 trả về một bản ghi hoặc Không có nếu không còn hàng nào nữa

Tôi đã tạo một bảng database_developers trong cơ sở dữ liệu của mình. Bây giờ, nó chứa năm hàng. hãy xem cách sử dụng tìm nạp để tìm nạp tất cả các bản ghi

Hãy xem các ví dụ bây giờ

Làm cách nào để tìm nạp nhiều hàng bằng con trỏ trong MySQL?
Làm cách nào để tìm nạp nhiều hàng bằng con trỏ trong MySQL?
bảng sqlitedb_developers có dữ liệu

Mục lục

Tìm nạp tất cả các hàng từ bảng cơ sở dữ liệu bằng hàm tìm nạp () của con trỏ

Bây giờ, hãy xem cách sử dụng

Connected to database
Total rows are:   5

Printing each row
Id:  1
Name:  Emma
Email:  [email protected]
Salary:  12000.0


Id:  2
Name:  Scott
Email:  [email protected]
Salary:  22000.0


Id:  3
Name:  Jessa
Email:  [email protected]
Salary:  18000.0


Id:  4
Name:  Mike
Email:  [email protected]
Salary:  13000.0


Id:  5
Name:  Ricky
Email:  [email protected]
Salary:  19000.0


The Sqlite connection is closed
9 để tìm nạp tất cả các bản ghi. Để tìm nạp tất cả các hàng từ bảng cơ sở dữ liệu, bạn cần làm theo các bước đơn giản sau. –

  • Tạo kết nối cơ sở dữ liệu từ Python. Tham khảo kết nối Python SQLite, kết nối Python MySQL, kết nối Python PostgreSQL
  • Xác định truy vấn CHỌN. Ở đây bạn cần biết chi tiết bảng và cột của nó
  • Thực hiện truy vấn CHỌN bằng phương thức
    rows = cursor.fetchmany([size=cursor.arraysize])
    0
  • Nhận tập kết quả (tất cả các hàng) từ đối tượng con trỏ bằng cách sử dụng
    Connected to database
    Total rows are:   5
    
    Printing each row
    Id:  1
    Name:  Emma
    Email:  [email protected]
    Salary:  12000.0
    
    
    Id:  2
    Name:  Scott
    Email:  [email protected]
    Salary:  22000.0
    
    
    Id:  3
    Name:  Jessa
    Email:  [email protected]
    Salary:  18000.0
    
    
    Id:  4
    Name:  Mike
    Email:  [email protected]
    Salary:  13000.0
    
    
    Id:  5
    Name:  Ricky
    Email:  [email protected]
    Salary:  19000.0
    
    
    The Sqlite connection is closed
    5
  • Lặp lại Bộ kết quả bằng cách sử dụng vòng lặp
    rows = cursor.fetchmany([size=cursor.arraysize])
    2 và nhận các giá trị cột của mỗi hàng
  • Đóng kết nối cơ sở dữ liệu Python
  • Nắm bắt mọi ngoại lệ SQL có thể xuất hiện trong quá trình

Hãy thử tìm nạp tất cả các hàng từ bảng

import sqlite3

def getAllRows():
    try:
        connection = sqlite3.connect('SQLite_Python.db')
        cursor = connection.cursor()
        print("Connected to SQLite")

        sqlite_select_query = """SELECT * from database_developers"""
        cursor.execute(sqlite_select_query)
        records = cursor.fetchall()
        print("Total rows are:  ", len(records))
        print("Printing each row")
        for row in records:
            print("Id: ", row[0])
            print("Name: ", row[1])
            print("Email: ", row[2])
            print("Salary: ", row[3])
            print("\n")

        cursor.close()

    except sqlite3.Error as error:
        print("Failed to read data from table", error)
    finally:
        if connection:
            connection.close()
            print("The Sqlite connection is closed")

getAllRows()

đầu ra

Connected to database
Total rows are:   5

Printing each row
Id:  1
Name:  Emma
Email:  [email protected]
Salary:  12000.0


Id:  2
Name:  Scott
Email:  [email protected]
Salary:  22000.0


Id:  3
Name:  Jessa
Email:  [email protected]
Salary:  18000.0


Id:  4
Name:  Mike
Email:  [email protected]
Salary:  13000.0


Id:  5
Name:  Ricky
Email:  [email protected]
Salary:  19000.0


The Sqlite connection is closed

Cũng đọc

Truy xuất một vài hàng từ bảng bằng cách sử dụng Connected to database Total rows are: 5 Printing each row Id: 1 Name: Emma Email: [email protected] Salary: 12000.0 Id: 2 Name: Scott Email: [email protected] Salary: 22000.0 Id: 3 Name: Jessa Email: [email protected] Salary: 18000.0 Id: 4 Name: Mike Email: [email protected] Salary: 13000.0 Id: 5 Name: Ricky Email: [email protected] Salary: 19000.0 The Sqlite connection is closed6

Một điều tôi thích về API Python DB là tính linh hoạt. Trong thế giới thực, việc tìm nạp tất cả các hàng cùng một lúc có thể không khả thi. Vì vậy, Python DB API giải quyết vấn đề này bằng cách cung cấp các phiên bản khác nhau của chức năng tìm nạp của lớp Con trỏ. Phiên bản được sử dụng phổ biến nhất là

Connected to database
Total rows are:   5

Printing each row
Id:  1
Name:  Emma
Email:  [email protected]
Salary:  12000.0


Id:  2
Name:  Scott
Email:  [email protected]
Salary:  22000.0


Id:  3
Name:  Jessa
Email:  [email protected]
Salary:  18000.0


Id:  4
Name:  Mike
Email:  [email protected]
Salary:  13000.0


Id:  5
Name:  Ricky
Email:  [email protected]
Salary:  19000.0


The Sqlite connection is closed
6

Cú pháp của con trỏ

Connected to database
Total rows are:   5

Printing each row
Id:  1
Name:  Emma
Email:  [email protected]
Salary:  12000.0


Id:  2
Name:  Scott
Email:  [email protected]
Salary:  22000.0


Id:  3
Name:  Jessa
Email:  [email protected]
Salary:  18000.0


Id:  4
Name:  Mike
Email:  [email protected]
Salary:  13000.0


Id:  5
Name:  Ricky
Email:  [email protected]
Salary:  19000.0


The Sqlite connection is closed
0

rows = cursor.fetchmany([size=cursor.arraysize])
  • Ở đây kích thước là số lượng hàng được truy xuất. Phương thức này tìm nạp tập hợp các hàng tiếp theo của kết quả truy vấn và trả về một danh sách các bộ dữ liệu. Nếu không còn hàng nào nữa, nó sẽ trả về một danh sách trống
  • Phương thức
    Connected to database
    Total rows are:   5
    
    Printing each row
    Id:  1
    Name:  Emma
    Email:  [email protected]
    Salary:  12000.0
    
    
    Id:  2
    Name:  Scott
    Email:  [email protected]
    Salary:  22000.0
    
    
    Id:  3
    Name:  Jessa
    Email:  [email protected]
    Salary:  18000.0
    
    
    Id:  4
    Name:  Mike
    Email:  [email protected]
    Salary:  13000.0
    
    
    Id:  5
    Name:  Ricky
    Email:  [email protected]
    Salary:  19000.0
    
    
    The Sqlite connection is closed
    0 của con trỏ trả về số hàng được chỉ định bởi đối số kích thước. giá trị mặc định là 1. Nếu kích thước được chỉ định là 100, thì nó sẽ trả về 100 hàng

Hãy thử tìm nạp 3 hàng từ bảng bằng cách sử dụng

Connected to database
Total rows are:   5

Printing each row
Id:  1
Name:  Emma
Email:  [email protected]
Salary:  12000.0


Id:  2
Name:  Scott
Email:  [email protected]
Salary:  22000.0


Id:  3
Name:  Jessa
Email:  [email protected]
Salary:  18000.0


Id:  4
Name:  Mike
Email:  [email protected]
Salary:  13000.0


Id:  5
Name:  Ricky
Email:  [email protected]
Salary:  19000.0


The Sqlite connection is closed
6

import sqlite3

def getlimitedRows(size):
    try:
        connection = sqlite3.connect('SQLite_Python.db')
        cursor = connection.cursor()
        print("Connected to database")

        sqlite_select_query = """SELECT * from database_developers"""
        cursor.execute(sqlite_select_query)
        records = cursor.fetchmany(size)
        print("Fetching Total ", size," rows")
        print("Printing each row")
        for row in records:
            print("Id: ", row[0])
            print("Name: ", row[1])
            print("Email: ", row[2])
            print("Salary: ", row[3])
            print("\n")

        cursor.close()

    except sqlite3.Error as error:
        print("Failed to read data from table", error)
    finally:
        if (connection):
            connection.close()
            print("The Sqlite connection is closed")

getlimitedRows(3)

đầu ra

Connected to database
Fetching Total  3  rows
Printing each row

Id:  1
Name:  Emma
Email:  [email protected]
Salary:  12000.0

Id:  2
Name:  Scott
Email:  [email protected]
Salary:  22000.0

Id:  3
Name:  Jessa
Email:  [email protected]
Salary:  18000.0

The Sqlite connection is closed

Cũng đọc

Ghi chú

  • Connected to database
    Total rows are:   5
    
    Printing each row
    Id:  1
    Name:  Emma
    Email:  [email protected]
    Salary:  12000.0
    
    
    Id:  2
    Name:  Scott
    Email:  [email protected]
    Salary:  22000.0
    
    
    Id:  3
    Name:  Jessa
    Email:  [email protected]
    Salary:  18000.0
    
    
    Id:  4
    Name:  Mike
    Email:  [email protected]
    Salary:  13000.0
    
    
    Id:  5
    Name:  Ricky
    Email:  [email protected]
    Salary:  19000.0
    
    
    The Sqlite connection is closed
    0 trả về một danh sách trống khi không còn hàng nào trong bảng
  • Một 
    rows = cursor.fetchmany([size=cursor.arraysize])
    9 được nâng lên nếu lệnh gọi trước đó tới
    import sqlite3
    
    def getlimitedRows(size):
        try:
            connection = sqlite3.connect('SQLite_Python.db')
            cursor = connection.cursor()
            print("Connected to database")
    
            sqlite_select_query = """SELECT * from database_developers"""
            cursor.execute(sqlite_select_query)
            records = cursor.fetchmany(size)
            print("Fetching Total ", size," rows")
            print("Printing each row")
            for row in records:
                print("Id: ", row[0])
                print("Name: ", row[1])
                print("Email: ", row[2])
                print("Salary: ", row[3])
                print("\n")
    
            cursor.close()
    
        except sqlite3.Error as error:
            print("Failed to read data from table", error)
        finally:
            if (connection):
                connection.close()
                print("The Sqlite connection is closed")
    
    getlimitedRows(3)
    0 không tạo ra bất kỳ tập hợp kết quả nào hoặc chưa có lệnh gọi nào được đưa ra
  • Connected to database
    Total rows are:   5
    
    Printing each row
    Id:  1
    Name:  Emma
    Email:  [email protected]
    Salary:  12000.0
    
    
    Id:  2
    Name:  Scott
    Email:  [email protected]
    Salary:  22000.0
    
    
    Id:  3
    Name:  Jessa
    Email:  [email protected]
    Salary:  18000.0
    
    
    Id:  4
    Name:  Mike
    Email:  [email protected]
    Salary:  13000.0
    
    
    Id:  5
    Name:  Ricky
    Email:  [email protected]
    Salary:  19000.0
    
    
    The Sqlite connection is closed
    0 trả về ít hàng hơn nếu bảng chứa số lượng hàng được chỉ định bởi đối số
    import sqlite3
    
    def getlimitedRows(size):
        try:
            connection = sqlite3.connect('SQLite_Python.db')
            cursor = connection.cursor()
            print("Connected to database")
    
            sqlite_select_query = """SELECT * from database_developers"""
            cursor.execute(sqlite_select_query)
            records = cursor.fetchmany(size)
            print("Fetching Total ", size," rows")
            print("Printing each row")
            for row in records:
                print("Id: ", row[0])
                print("Name: ", row[1])
                print("Email: ", row[2])
                print("Salary: ", row[3])
                print("\n")
    
            cursor.close()
    
        except sqlite3.Error as error:
            print("Failed to read data from table", error)
        finally:
            if (connection):
                connection.close()
                print("The Sqlite connection is closed")
    
    getlimitedRows(3)
    2 ít hơn

Điều gì sẽ xảy ra nếu hàm tìm nạp(kích thước) của con trỏ được gọi liên tục

Điều gì sẽ xảy ra nếu chúng ta gọi liên tục

Connected to database
Total rows are:   5

Printing each row
Id:  1
Name:  Emma
Email:  [email protected]
Salary:  12000.0


Id:  2
Name:  Scott
Email:  [email protected]
Salary:  22000.0


Id:  3
Name:  Jessa
Email:  [email protected]
Salary:  18000.0


Id:  4
Name:  Mike
Email:  [email protected]
Salary:  13000.0


Id:  5
Name:  Ricky
Email:  [email protected]
Salary:  19000.0


The Sqlite connection is closed
6 sau khi thực hiện một truy vấn SQL

Ví dụ: chúng tôi đã chạy một truy vấn và nó đã trả về kết quả truy vấn gồm 10 hàng. Tiếp theo, chúng tôi tìm nạp hai hàng đầu tiên bằng cách sử dụng

import sqlite3

def getlimitedRows(size):
    try:
        connection = sqlite3.connect('SQLite_Python.db')
        cursor = connection.cursor()
        print("Connected to database")

        sqlite_select_query = """SELECT * from database_developers"""
        cursor.execute(sqlite_select_query)
        records = cursor.fetchmany(size)
        print("Fetching Total ", size," rows")
        print("Printing each row")
        for row in records:
            print("Id: ", row[0])
            print("Name: ", row[1])
            print("Email: ", row[2])
            print("Salary: ", row[3])
            print("\n")

        cursor.close()

    except sqlite3.Error as error:
        print("Failed to read data from table", error)
    finally:
        if (connection):
            connection.close()
            print("The Sqlite connection is closed")

getlimitedRows(3)
4. Một lần nữa, chúng tôi đã gọi
import sqlite3

def getlimitedRows(size):
    try:
        connection = sqlite3.connect('SQLite_Python.db')
        cursor = connection.cursor()
        print("Connected to database")

        sqlite_select_query = """SELECT * from database_developers"""
        cursor.execute(sqlite_select_query)
        records = cursor.fetchmany(size)
        print("Fetching Total ", size," rows")
        print("Printing each row")
        for row in records:
            print("Id: ", row[0])
            print("Name: ", row[1])
            print("Email: ", row[2])
            print("Salary: ", row[3])
            print("\n")

        cursor.close()

    except sqlite3.Error as error:
        print("Failed to read data from table", error)
    finally:
        if (connection):
            connection.close()
            print("The Sqlite connection is closed")

getlimitedRows(3)
4, sau đó nó sẽ trả về hai hàng tiếp theo. Hãy xem ví dụ để hiểu rõ hơn

import sqlite3

def getlimitedRows(size):
    try:
        connection = sqlite3.connect('SQLite_Python.db')
        cursor = connection.cursor()
        print("Connected to database")

        sqlite_select_query = """SELECT * from database_developers"""
        cursor.execute(sqlite_select_query)
        records = cursor.fetchmany(size)
        print("Fetching Total ", size, " rows")
        print("Printing each row")
        for row in records:
            print("Id: ", row[0])
            print("Name: ", row[1])
            print("Email: ", row[2])
            print("Salary: ", row[3])
            print("\n")

        records = cursor.fetchmany(size)
        print("Fetching next 2 rows from table")
        print("Printing each row")
        for row in records:
            print("Id: ", row[0])
            print("Name: ", row[1])
            print("Email: ", row[2])
            print("Salary: ", row[3])
            print("\n")

        cursor.close()

    except sqlite3.Error as error:
        print("Failed to read data from table", error)
    finally:
        if connection:
            connection.close()
            print("The Sqlite connection is closed")

getlimitedRows(2)

đầu ra

Connected to database
Fetching Total  2  rows
Printing each row

Id:  1
Name:  Emma
Email:  [email protected]
Salary:  12000.0


Id:  2
Name:  Scott
Email:  [email protected]
Salary:  22000.0


Fetching next 2 rows from a table

Printing each row
Id:  3
Name:  Jessa
Email:  [email protected]
Salary:  18000.0


Id:  4
Name:  Mike
Email:  [email protected]
Salary:  13000.0


The Sqlite connection is closed

Truy xuất một hàng từ một bảng bằng con trỏ. tìm kiếm

  • Python DB API cho phép chúng tôi chỉ tìm nạp một hàng. Để tìm nạp một hàng từ tập hợp kết quả, chúng ta có thể sử dụng
    Connected to database
    Total rows are:   5
    
    Printing each row
    Id:  1
    Name:  Emma
    Email:  [email protected]
    Salary:  12000.0
    
    
    Id:  2
    Name:  Scott
    Email:  [email protected]
    Salary:  22000.0
    
    
    Id:  3
    Name:  Jessa
    Email:  [email protected]
    Salary:  18000.0
    
    
    Id:  4
    Name:  Mike
    Email:  [email protected]
    Salary:  13000.0
    
    
    Id:  5
    Name:  Ricky
    Email:  [email protected]
    Salary:  19000.0
    
    
    The Sqlite connection is closed
    8. Phương thức này trả về một tuple duy nhất
  • Nó có thể trả về none nếu không có hàng nào trong tập kết quả.
    Connected to database
    Total rows are:   5
    
    Printing each row
    Id:  1
    Name:  Emma
    Email:  [email protected]
    Salary:  12000.0
    
    
    Id:  2
    Name:  Scott
    Email:  [email protected]
    Salary:  22000.0
    
    
    Id:  3
    Name:  Jessa
    Email:  [email protected]
    Salary:  18000.0
    
    
    Id:  4
    Name:  Mike
    Email:  [email protected]
    Salary:  13000.0
    
    
    Id:  5
    Name:  Ricky
    Email:  [email protected]
    Salary:  19000.0
    
    
    The Sqlite connection is closed
    8 tăng vị trí con trỏ lên một và trả về hàng tiếp theo

Hãy xem ví dụ ngay bây giờ

import sqlite3

def getSingleRows():
    try:
        connection = sqlite3.connect('SQLite_Python.db')
        cursor = connection.cursor()
        print("Connected to database")

        sqlite_select_query = """SELECT * from database_developers"""
        cursor.execute(sqlite_select_query)
        print("Fetching single row")
        record = cursor.fetchone()
        print(record)

        print("Fetching next row")
        record = cursor.fetchone()
        print(record)

        cursor.close()

    except sqlite3.Error as error:
        print("Failed to read data from table", error)
    finally:
        if connection:
            connection.close()
            print("The Sqlite connection is closed")

getSingleRows()

đầu ra

Connected to database

Fetching single row
(1, 'Emma', '[email protected]', 12000.0)

Fetching next row
(2, 'Scott', '[email protected]', 22000.0)
The Sqlite connection is closed

Bước tiếp theo

Để thực hành những gì bạn đã học trong bài viết này, Giải bài tập Python SQLite project để thực hành các thao tác với cơ sở dữ liệu

Con trỏ có thể tìm nạp nhiều hàng không?

Câu lệnh FETCH nhiều hàng có thể được sử dụng với cả con trỏ nối tiếp và con trỏ có thể cuộn . Các thao tác được sử dụng để xác định, mở và đóng con trỏ cho FETCH nhiều hàng vẫn giữ nguyên. Chỉ câu lệnh FETCH thay đổi để chỉ định số lượng hàng cần truy xuất và bộ lưu trữ nơi các hàng được đặt.

Làm cách nào để lấy dữ liệu từ con trỏ trong MySQL?

Khai báo con trỏ sử dụng câu lệnh DECLARE
Khai báo biến và điều kiện
Mở con trỏ đã khai báo bằng câu lệnh OPEN
Truy xuất các bản ghi mong muốn từ một bảng bằng Câu lệnh FETCH
Cuối cùng đóng con trỏ bằng câu lệnh CLOSE

Làm cách nào để sử dụng con trỏ trong MySQL?

Con trỏ MySQL .
Khai báo con trỏ. Con trỏ là một câu lệnh chọn, được định nghĩa trong phần khai báo trong MySQL
mở con trỏ. Sau khi khai báo con trỏ, bước tiếp theo là mở con trỏ bằng câu lệnh open
Tìm nạp con trỏ. Sau khi khai báo và mở con trỏ, bước tiếp theo là tìm nạp con trỏ. .
Đóng con trỏ

Làm cách nào để hiển thị nhiều hàng trong thủ tục được lưu trữ?

Chúng ta có thể chuyển một Bảng chứa nhiều bản ghi (hàng) sang một Thủ tục được lưu trữ bằng cách sử dụng các tham số Bảng có giá trị trong SQL Server . Tôi đã tạo một bảng đơn giản có tên là Khách hàng có lược đồ được hiển thị bên dưới. Sau khi tạo, Loại do người dùng xác định sẽ hiển thị trong Object Explorer như hình bên dưới.