Máy chủ MySQL là một hệ thống quản lý cơ sở dữ liệu quan hệ nguồn mở, hỗ trợ chính cho các ứng dụng dựa trên web. Cơ sở dữ liệu và các bảng liên quan là thành phần chính của nhiều trang web và ứng dụng vì dữ liệu được lưu trữ và trao đổi trên web. Để truy cập cơ sở dữ liệu MySQL từ máy chủ web [ở đây là XAMPP], chúng tôi sử dụng các mô-đun khác nhau trong Python như PyMySQL, mysql. kết nối, vv.
Trong bài viết này, chúng ta sẽ xem cách lấy tất cả các hàng của bảng MySQL bằng cách tạo kết nối cơ sở dữ liệu giữa python và MySQL
Đầu tiên, chúng ta sẽ kết nối với cơ sở dữ liệu có bảng MySQL. Truy vấn SQL được sử dụng để lấy tất cả các hàng
SELECT * FROM table-name
Cuối cùng, sau khi nhận được tất cả các hàng, hãy hiển thị từng hàng trong bảng bằng trình vòng lặp
Dưới đây là một số chương trình mô tả cách trích xuất các hàng từ bảng MySQL trong Cơ sở dữ liệu
ví dụ 1
Dưới đây là bảng geeksdemo là cơ sở dữ liệu geek sẽ được truy cập bằng tập lệnh Python
Dưới đây là chương trình lấy tất cả các hàng trong bảng MYSQL
Python3
4Code 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]
5Code 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]
6Code 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]
7Code 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]
5Code 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]
9Code 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]
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]
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]
40Code 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]
41Code 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]
42Code 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]
43Code 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]
44Code 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]
45Code 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]
46Code 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]
47Code 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]
48Code 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]
50Code 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]
51Code 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]
41Code 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]
53Code 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]
55Code 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]
56____157Code 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]
58Code 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]
60
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]
61Code 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]
41Code 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]
63Code 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]
65Code 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]
66Code 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]
67Code 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]
68Code 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]
69Code 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]
70Code 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]
71Code 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]
72Code 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]
70Code 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]
71Code 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]
75Code 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]
76Code 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]
58Code 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 ra
ví dụ 2
Đây là một ví dụ khác để trích xuất tất cả các hàng từ một bảng trong cơ sở dữ liệu nhất định, bên dưới là sơ đồ bảng và các hàng
Trong hàm
1, chúng tôi đã 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]
1 để 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 [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] 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]
1 đã tìm nạp tất cả các nhiệm 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ả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ụ 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]
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]
Đây là chương trình đầy đủ
3Code 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