Trong hàm
1, chúng ta đã tạo một con trỏ, thực thi 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]
2 và gọi là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]
3 để tìm nạp tất cả các tác vụ từ bảngCode 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]
0Code 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 này truy vấn các tác vụ theo mức độ ư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ức độ ưu tiên cụ thể. Dấu chấm hỏi [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]
6] trong truy vấn là phần giữ chỗ. Khi con trỏ thực thi 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]
2, nó đã thay thế dấu chấm 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] 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ứcCode 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]
3 đã tìm nạp tất cả các tác vụ phù hợp theo mức độ ưu tiênCode 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 main[] này tạo kết nối đến cơ sở dữ liệu
1 và gọi các hàm để truy vấn tất cả các hàng từ bảng
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]
Code language: Python [python]
0 và chọn các tác vụ có mức độ ưu tiên 1Code 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]
1Code 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]
Đây là chương trình đầy đủ
2Code 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ướng dẫn này, bạn đã họ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