Nội dung chính
- 1 Xem Làm cách nào để tìm nạp tất cả các hàng trong MySQL? 2024
- 2 Mục lục
- 3 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ỏ
- 4 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
- 5 Truy xuất một hàng từ một bảng bằng con trỏ. tìm kiếm
- 6 Bước tiếp theo
Xem Làm cách nào để tìm nạp tất cả các hàng trong MySQL? 2024
Bài viết này trình bày việc sử dụng các phương thức lớp con trỏ của Python 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: ricky@pynative.com Salary: 19000.0 The Sqlite connection is closed
6,
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: ricky@pynative.com 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: ricky@pynative.com 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
Cơ sở dữ liệuModuleMySQLMySQL 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
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: ricky@pynative.com Salary: 19000.0 The Sqlite connection is closed
6,
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: ricky@pynative.com 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: ricky@pynative.com 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: ricky@pynative.com 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: ricky@pynative.com 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: ricky@pynative.com 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: ricky@pynative.com 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ờ
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ỏ
- Truy xuất một vài hàng từ bảng bằng con trỏ. tìm nạp (kích thước)
- Đ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
- Truy xuất một hàng từ một bảng bằng con trỏ. tìm kiếm
- Bước tiếp theo
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: ricky@pynative.com 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: ricky@pynative.com 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
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: ricky@pynative.com Salary: 19000.0 The Sqlite connection is closed
2
đầ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: ricky@pynative.com Salary: 19000.0 The Sqlite connection is closed
Cũng đọc
- Tìm nạp tất cả các hàng từ bảng MySQL
- Tìm nạp tất cả các hàng từ bảng PostgreSQL
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: ricky@pynative.com 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: ricky@pynative.com 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: ricky@pynative.com 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: ricky@pynative.com Salary: 19000.0 The Sqlite connection is closed
6
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: ricky@pynative.com Salary: 19000.0 The Sqlite connection is closed
0
đầ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: ricky@pynative.com Salary: 19000.0 The Sqlite connection is closed
1
Cũng đọc
- Tìm nạp các hàng giới hạn từ bảng MySQL
- Tìm nạp các hàng giới hạn từ bảng PostgreSQL
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: ricky@pynative.com 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 tăng nếu lệnh gọi trước đó tới
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: ricky@pynative.com Salary: 19000.0 The Sqlite connection is closed
00 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: ricky@pynative.com 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ố
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: ricky@pynative.com Salary: 19000.0 The Sqlite connection is closed
02 í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: ricky@pynative.com Salary: 19000.0 The Sqlite connection is closed
6 sau khi thực hiện 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
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: ricky@pynative.com Salary: 19000.0 The Sqlite connection is closed
04. Một lần nữa, chúng tôi đã gọi
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: ricky@pynative.com Salary: 19000.0 The Sqlite connection is closed
04, sau đó nó sẽ trả về hai hàng tiếp theo. Hãy xem ví dụ để hiểu rõ hơn
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: ricky@pynative.com Salary: 19000.0 The Sqlite connection is closed
0
đầ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: ricky@pynative.com Salary: 19000.0 The Sqlite connection is closed
1
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 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: ricky@pynative.com 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: ricky@pynative.com 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ờ
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: ricky@pynative.com Salary: 19000.0 The Sqlite connection is closed
4
đầ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: ricky@pynative.com Salary: 19000.0 The Sqlite connection is closed
5
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
Làm cách nào để hiển thị tất cả các hàng trong SQL?
Làm cách nào tôi chỉ nhận được 10 bản ghi trong MySQL?
Bạn đang tìm hiểu bài viết: Làm cách nào để tìm nạp tất cả các hàng trong MySQL? 2024
HỆ THỐNG CỬA HÀNG TRÙM SỈ QUẢNG CHÂU
Điện thoại: 092.484.9483
Zalo: 092.484.9483
Facebook: https://facebook.com/giatlathuhuongcom/
Website: Trumsiquangchau.com
Địa chỉ: Ngõ 346 Nam Dư, Trần Phú, Hoàng Mai, Hà Nội.