Làm cách nào để tìm nạp tất cả các hàng trong MySQL?
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: [email protected] Salary: 19000.0 The Sqlite connection is closed6, 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 closed0 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 closed1 để 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 Show
cũng đọc
Đặ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 PythonPostgreSQLPsycopg2SQLitesqlite3Trê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: [email protected] Salary: 19000.0 The Sqlite connection is closed6, 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 closed0 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 closed1 để 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 closed5 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 closed6 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 closed7. 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 closed8 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ệuMụ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 closed9 để 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. –
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: [email protected] Salary: 19000.0 The Sqlite connection is closed2 đầ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 closed6Mộ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 closed6 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 closed0
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 closed6 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 closed0 đầ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 closed1 Cũng đọc
Ghi chú
Đ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 closed6 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: [email protected] Salary: 19000.0 The Sqlite connection is closed04. 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: [email protected] Salary: 19000.0 The Sqlite connection is closed04, 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: [email protected] Salary: 19000.0 The Sqlite connection is closed0 đầ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 closed1 Truy xuất một hàng từ một bảng bằng con trỏ. tìm kiếm
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: [email protected] Salary: 19000.0 The Sqlite connection is closed4 đầ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 closed5 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?Bạn chỉ cần làm Chọn * từ bảng . Nó sẽ chọn toàn bộ dữ liệu từ bảng của bạn.
Làm cách nào tôi chỉ nhận được 10 bản ghi trong MySQL?Để chọn 10 phần tử đầu tiên từ cơ sở dữ liệu bằng SQL Mệnh đề ORDER BY với GIỚI HẠN 10 . Chèn một số bản ghi vào bảng bằng lệnh chèn. Hiển thị tất cả các bản ghi từ bảng bằng cách sử dụng câu lệnh chọn. |