Tóm tắt: Trong hướng dẫn này, chúng tôi sẽ chỉ cho bạn từng bước cách truy vấn dữ liệu trong sqlite từ Python.: in this tutorial, we will show you step by step how to query data in SQLite from Python.
Để truy vấn dữ liệu trong cơ sở dữ liệu SQLite từ Python, bạn sử dụng các bước sau:
- Đầu tiên, thiết lập kết nối với cơ sở dữ liệu SQLite bằng cách tạo đối tượng
Connection
. - Tiếp theo, tạo một đối tượng
Cursor
bằng phương thức con trỏ của đối tượngConnection
. - Sau đó, thực thi A & NBSP; Tuyên bố
SELECT
. - Sau đó, hãy gọi phương thức
fetchall[]
của đối tượng con trỏ để tìm nạp dữ liệu. - Cuối cùng, lặp con trỏ và xử lý từng hàng riêng lẻ.
Trong ví dụ sau, chúng tôi sẽ sử dụng bảng
0 được tạo trong hướng dẫn tạo bảng.Code language: Python [python]
def select_all_tasks[conn]: """ Query all rows in the tasks table :param conn: the Connection object :return: """ cur = conn.cursor[] cur.execute["SELECT * FROM tasks"] rows = cur.fetchall[] for row in rows: print[row]
Đầu tiên, hãy tạo kết nối với cơ sở dữ liệu SQLite được chỉ định bởi một tệp:
Code language: Python [python]
def create_connection[db_file]: """ create a database connection to the SQLite database specified by the db_file :param db_file: database file :return: Connection object or None """ conn = None try: conn = sqlite3.connect[db_file] except Error as e: print[e] return conn
Chức năng này chọn tất cả các hàng từ bảng tác vụ và hiển thị dữ liệu:
Code language: Python [python]
def select_all_tasks[conn]: """ Query all rows in the tasks table :param conn: the Connection object :return: """ cur = conn.cursor[] cur.execute["SELECT * FROM tasks"] rows = cur.fetchall[] for row in rows: print[row]
Trong hàm
1, chúng tôi đã tạo một con trỏ, thực hiện câu lệnhCode language: Python [python]
def select_all_tasks[conn]: """ Query all rows in the tasks table :param conn: the Connection object :return: """ cur = conn.cursor[] cur.execute["SELECT * FROM tasks"] rows = cur.fetchall[] for row in rows: print[row]
SELECT
và được gọi là & nbsp; ________ 9 đến & nbsp; lấy tất cả các tác vụ từ bảng 0.Code language: Python [python]
def select_all_tasks[conn]: """ Query all rows in the tasks table :param conn: the Connection object :return: """ cur = conn.cursor[] cur.execute["SELECT * FROM tasks"] rows = cur.fetchall[] for row in rows: print[row]
Chức năng này truy vấn các nhiệm vụ theo ưu tiên:
Code language: Python [python]
def select_task_by_priority[conn, priority]: """ Query tasks by priority :param conn: the Connection object :param priority: :return: """ cur = conn.cursor[] cur.execute["SELECT * FROM tasks WHERE priority=?", [priority,]] rows = cur.fetchall[] for row in rows: print[row]
Trong hàm
5, chúng tôi đã chọn các tác vụ dựa trên một ưu tiên cụ thể. Dấu câu hỏi [
def select_all_tasks[conn]: """ Query all rows in the tasks table :param conn: the Connection object :return: """ cur = conn.cursor[] cur.execute["SELECT * FROM tasks"] rows = cur.fetchall[] for row in rows: print[row]
Code language: Python [python]
6] trong truy vấn là trình giữ chỗ. Khi con trỏ thực hiện câu lệnhCode language: Python [python]
def select_all_tasks[conn]: """ Query all rows in the tasks table :param conn: the Connection object :return: """ cur = conn.cursor[] cur.execute["SELECT * FROM tasks"] rows = cur.fetchall[] for row in rows: print[row]
SELECT
, nó đã thay thế dấu câu hỏi [6] bằng đối sốCode language: Python [python]
def select_all_tasks[conn]: """ Query all rows in the tasks table :param conn: the Connection object :return: """ cur = conn.cursor[] cur.execute["SELECT * FROM tasks"] rows = cur.fetchall[] for row in rows: print[row]
9. Phương thức & nbsp; ________ 9 đã tìm nạp tất cả các tác vụ phù hợp theo mức độ ưu tiên.Code language: Python [python]
def select_all_tasks[conn]: """ Query all rows in the tasks table :param conn: the Connection object :return: """ cur = conn.cursor[] cur.execute["SELECT * FROM tasks"] rows = cur.fetchall[] for row in rows: print[row]
Hàm chính [] này tạo kết nối với cơ sở dữ liệu & nbsp;
1 và gọi các chức năng để truy vấn tất cả các hàng từ bảngCode language: Python [python]
def select_task_by_priority[conn, priority]: """ Query tasks by priority :param conn: the Connection object :param priority: :return: """ cur = conn.cursor[] cur.execute["SELECT * FROM tasks WHERE priority=?", [priority,]] rows = cur.fetchall[] for row in rows: print[row]
0 và chọn các tác vụ với ưu tiên 1:Code language: Python [python]
def select_all_tasks[conn]: """ Query all rows in the tasks table :param conn: the Connection object :return: """ cur = conn.cursor[] cur.execute["SELECT * FROM tasks"] rows = cur.fetchall[] for row in rows: print[row]
Code language: Python [python]
def main[]: database = r"C:\sqlite\db\pythonsqlite.db" # create a database connection conn = create_connection[database] with conn: print["1. Query task by priority:"] select_task_by_priority[conn, 1] print["2. Query all tasks"] select_all_tasks[conn]
Đây là chương trình đầy đủ:
Code language: Python [python]
import sqlite3 from sqlite3 import Error def create_connection[db_file]: """ create a database connection to the SQLite database specified by the db_file :param db_file: database file :return: Connection object or None """ conn = None try: conn = sqlite3.connect[db_file] except Error as e: print[e] return conn def select_all_tasks[conn]: """ Query all rows in the tasks table :param conn: the Connection object :return: """ cur = conn.cursor[] cur.execute["SELECT * FROM tasks"] rows = cur.fetchall[] for row in rows: print[row] def select_task_by_priority[conn, priority]: """ Query tasks by priority :param conn: the Connection object :param priority: :return: """ cur = conn.cursor[] cur.execute["SELECT * FROM tasks WHERE priority=?", [priority,]] rows = cur.fetchall[] for row in rows: print[row] def main[]: database = r"C:\sqlite\db\pythonsqlite.db" # create a database connection conn = create_connection[database] with conn: print["1. Query task by priority:"] select_task_by_priority[conn, 1] print["2. Query all tasks"] select_all_tasks[conn] if __name__ == '__main__': main[]
Trong hướng dẫn này, bạn đã học được cách phát triển chương trình Python để truy vấn dữ liệu từ các bảng trong cơ sở dữ liệu SQLite.
Hướng dẫn này có hữu ích không?