Sự khác biệt giữa Fetchone() và Fetchall() trong lập trình cơ sở dữ liệu Python là gì?

Tóm lược. trong hướng dẫn này, bạn sẽ học cách chọn dữ liệu từ Cơ sở dữ liệu Oracle bằng các phương thức

import cx_Oracle import config sql = 'select customer_id, name ' \ 'from customers ' \ 'order by name' try: with cx_Oracle.connect[ config.username, config.password, config.dsn, encoding=config.encoding] as connection: with connection.cursor[] as cursor: cursor.execute[sql] while True: row = cursor.fetchone[] if row is None: break print[row] except cx_Oracle.Error as error: print[error]

Code language: Python [python]
9,

import cx_Oracle import config sql = 'select customer_id, name ' \ 'from customers ' \ 'order by name' try: with cx_Oracle.connect[ config.username, config.password, config.dsn, encoding=config.encoding] as connection: with connection.cursor[] as cursor: cursor.execute[sql] while True: row = cursor.fetchone[] if row is None: break print[row] except cx_Oracle.Error as error: print[error]

Code language: Python [python]
0 và

import cx_Oracle import config sql = 'select customer_id, name ' \ 'from customers ' \ 'order by name' try: with cx_Oracle.connect[ config.username, config.password, config.dsn, encoding=config.encoding] as connection: with connection.cursor[] as cursor: cursor.execute[sql] while True: row = cursor.fetchone[] if row is None: break print[row] except cx_Oracle.Error as error: print[error]

Code language: Python [python]
1

Để chọn dữ liệu từ Cơ sở dữ liệu Oracle trong chương trình Python, bạn làm theo các bước sau

  • Đầu tiên, thiết lập kết nối tới Cơ sở dữ liệu Oracle bằng phương thức

    import cx_Oracle import config sql = 'select customer_id, name ' \ 'from customers ' \ 'order by name' try: with cx_Oracle.connect[ config.username, config.password, config.dsn, encoding=config.encoding] as connection: with connection.cursor[] as cursor: cursor.execute[sql] while True: row = cursor.fetchone[] if row is None: break print[row] except cx_Oracle.Error as error: print[error]

    Code language: Python [python]
    2
  • Thứ hai, tạo đối tượng

    import cx_Oracle import config sql = 'select customer_id, name ' \ 'from customers ' \ 'order by name' try: with cx_Oracle.connect[ config.username, config.password, config.dsn, encoding=config.encoding] as connection: with connection.cursor[] as cursor: cursor.execute[sql] while True: row = cursor.fetchone[] if row is None: break print[row] except cx_Oracle.Error as error: print[error]

    Code language: Python [python]
    3 từ đối tượng Kết nối bằng phương thức

    import cx_Oracle import config sql = 'select customer_id, name ' \ 'from customers ' \ 'order by name' try: with cx_Oracle.connect[ config.username, config.password, config.dsn, encoding=config.encoding] as connection: with connection.cursor[] as cursor: cursor.execute[sql] while True: row = cursor.fetchone[] if row is None: break print[row] except cx_Oracle.Error as error: print[error]

    Code language: Python [python]
    4
  • Thứ ba, thực thi một câu lệnh SQL để chọn dữ liệu từ một hoặc nhiều bảng bằng phương thức

    import cx_Oracle import config sql = 'select customer_id, name ' \ 'from customers ' \ 'order by name' try: with cx_Oracle.connect[ config.username, config.password, config.dsn, encoding=config.encoding] as connection: with connection.cursor[] as cursor: cursor.execute[sql] while True: row = cursor.fetchone[] if row is None: break print[row] except cx_Oracle.Error as error: print[error]

    Code language: Python [python]
    0
  • Thứ tư, tìm nạp các hàng bằng các phương thức

    import cx_Oracle import config sql = 'select customer_id, name ' \ 'from customers ' \ 'order by name' try: with cx_Oracle.connect[ config.username, config.password, config.dsn, encoding=config.encoding] as connection: with connection.cursor[] as cursor: cursor.execute[sql] while True: row = cursor.fetchone[] if row is None: break print[row] except cx_Oracle.Error as error: print[error]

    Code language: Python [python]
    1,

    import cx_Oracle import config sql = 'select customer_id, name ' \ 'from customers ' \ 'order by name' try: with cx_Oracle.connect[ config.username, config.password, config.dsn, encoding=config.encoding] as connection: with connection.cursor[] as cursor: cursor.execute[sql] while True: row = cursor.fetchone[] if row is None: break print[row] except cx_Oracle.Error as error: print[error]

    Code language: Python [python]
    2 và

    import cx_Oracle import config sql = 'select customer_id, name ' \ 'from customers ' \ 'order by name' try: with cx_Oracle.connect[ config.username, config.password, config.dsn, encoding=config.encoding] as connection: with connection.cursor[] as cursor: cursor.execute[sql] while True: row = cursor.fetchone[] if row is None: break print[row] except cx_Oracle.Error as error: print[error]

    Code language: Python [python]
    3
  • Cuối cùng, giải phóng các đối tượng

    import cx_Oracle import config sql = 'select customer_id, name ' \ 'from customers ' \ 'order by name' try: with cx_Oracle.connect[ config.username, config.password, config.dsn, encoding=config.encoding] as connection: with connection.cursor[] as cursor: cursor.execute[sql] while True: row = cursor.fetchone[] if row is None: break print[row] except cx_Oracle.Error as error: print[error]

    Code language: Python [python]
    3 và

    import cx_Oracle import config sql = 'select customer_id, name ' \ 'from customers ' \ 'order by name' try: with cx_Oracle.connect[ config.username, config.password, config.dsn, encoding=config.encoding] as connection: with connection.cursor[] as cursor: cursor.execute[sql] while True: row = cursor.fetchone[] if row is None: break print[row] except cx_Oracle.Error as error: print[error]

    Code language: Python [python]
    5 bằng phương thức

    import cx_Oracle import config sql = 'select customer_id, name ' \ 'from customers ' \ 'order by name' try: with cx_Oracle.connect[ config.username, config.password, config.dsn, encoding=config.encoding] as connection: with connection.cursor[] as cursor: cursor.execute[sql] while True: row = cursor.fetchone[] if row is None: break print[row] except cx_Oracle.Error as error: print[error]

    Code language: Python [python]
    6 và

    import cx_Oracle import config sql = 'select customer_id, name ' \ 'from customers ' \ 'order by name' try: with cx_Oracle.connect[ config.username, config.password, config.dsn, encoding=config.encoding] as connection: with connection.cursor[] as cursor: cursor.execute[sql] while True: row = cursor.fetchone[] if row is None: break print[row] except cx_Oracle.Error as error: print[error]

    Code language: Python [python]
    7. Nếu bạn muốn tự động giải phóng

    import cx_Oracle import config sql = 'select customer_id, name ' \ 'from customers ' \ 'order by name' try: with cx_Oracle.connect[ config.username, config.password, config.dsn, encoding=config.encoding] as connection: with connection.cursor[] as cursor: cursor.execute[sql] while True: row = cursor.fetchone[] if row is None: break print[row] except cx_Oracle.Error as error: print[error]

    Code language: Python [python]
    3 và

    import cx_Oracle import config sql = 'select customer_id, name ' \ 'from customers ' \ 'order by name' try: with cx_Oracle.connect[ config.username, config.password, config.dsn, encoding=config.encoding] as connection: with connection.cursor[] as cursor: cursor.execute[sql] while True: row = cursor.fetchone[] if row is None: break print[row] except cx_Oracle.Error as error: print[error]

    Code language: Python [python]
    5, bạn có thể sử dụng khối

    import cx_Oracle import config sql = 'select customer_id, name ' \ 'from customers ' \ 'order by name' batch_size = 20 try: with cx_Oracle.connect[ config.username, config.password, config.dsn, encoding=config.encoding] as connection: with connection.cursor[] as cursor: # execute the SQL statement cursor.execute[sql] while True: # fetch rows rows = cursor.fetchmany[batch_size] if not rows: break # display rows for row in rows: print[row] except cx_Oracle.Error as error: print[error]

    Code language: Python [python]
    0

Chúng tôi sẽ sử dụng bảng

import cx_Oracle import config sql = 'select customer_id, name ' \ 'from customers ' \ 'order by name' batch_size = 20 try: with cx_Oracle.connect[ config.username, config.password, config.dsn, encoding=config.encoding] as connection: with connection.cursor[] as cursor: # execute the SQL statement cursor.execute[sql] while True: # fetch rows rows = cursor.fetchmany[batch_size] if not rows: break # display rows for row in rows: print[row] except cx_Oracle.Error as error: print[error]

Code language: Python [python]
1 từ cơ sở dữ liệu mẫu

và mô-đun

import cx_Oracle import config sql = 'select customer_id, name ' \ 'from customers ' \ 'order by name' batch_size = 20 try: with cx_Oracle.connect[ config.username, config.password, config.dsn, encoding=config.encoding] as connection: with connection.cursor[] as cursor: # execute the SQL statement cursor.execute[sql] while True: # fetch rows rows = cursor.fetchmany[batch_size] if not rows: break # display rows for row in rows: print[row] except cx_Oracle.Error as error: print[error]

Code language: Python [python]
2 sau

import cx_Oracle import config sql = 'select customer_id, name ' \ 'from customers ' \ 'order by name' try: with cx_Oracle.connect[ config.username, config.password, config.dsn, encoding=config.encoding] as connection: with connection.cursor[] as cursor: cursor.execute[sql] while True: row = cursor.fetchone[] if row is None: break print[row] except cx_Oracle.Error as error: print[error]

Code language: Python [python]
1

Truy vấn dữ liệu bằng phương thức

import cx_Oracle import config sql = 'select customer_id, name ' \ 'from customers ' \ 'order by name' try: with cx_Oracle.connect[ config.username, config.password, config.dsn, encoding=config.encoding] as connection: with connection.cursor[] as cursor: cursor.execute[sql] while True: row = cursor.fetchone[] if row is None: break print[row] except cx_Oracle.Error as error: print[error]

Code language: Python [python]
1

import cx_Oracle import config sql = 'select customer_id, name ' \ 'from customers ' \ 'order by name' batch_size = 20 try: with cx_Oracle.connect[ config.username, config.password, config.dsn, encoding=config.encoding] as connection: with connection.cursor[] as cursor: # execute the SQL statement cursor.execute[sql] while True: # fetch rows rows = cursor.fetchmany[batch_size] if not rows: break # display rows for row in rows: print[row] except cx_Oracle.Error as error: print[error]

Code language: Python [python]
4 sau đây minh họa cách chọn dữ liệu từ bảng

import cx_Oracle import config sql = 'select customer_id, name ' \ 'from customers ' \ 'order by name' batch_size = 20 try: with cx_Oracle.connect[ config.username, config.password, config.dsn, encoding=config.encoding] as connection: with connection.cursor[] as cursor: # execute the SQL statement cursor.execute[sql] while True: # fetch rows rows = cursor.fetchmany[batch_size] if not rows: break # display rows for row in rows: print[row] except cx_Oracle.Error as error: print[error]

Code language: Python [python]
1

import cx_Oracle import config sql = 'select customer_id, name ' \ 'from customers ' \ 'order by name' try: with cx_Oracle.connect[ config.username, config.password, config.dsn, encoding=config.encoding] as connection: with connection.cursor[] as cursor: cursor.execute[sql] while True: row = cursor.fetchone[] if row is None: break print[row] except cx_Oracle.Error as error: print[error]

Code language: Python [python]

Mặc dù

import cx_Oracle import config sql = 'select customer_id, name ' \ 'from customers ' \ 'order by name' try: with cx_Oracle.connect[ config.username, config.password, config.dsn, encoding=config.encoding] as connection: with connection.cursor[] as cursor: cursor.execute[sql] while True: row = cursor.fetchone[] if row is None: break print[row] except cx_Oracle.Error as error: print[error]

Code language: Python [python]
1 trả về một hàng tại một thời điểm, nhưng nó luôn truy xuất dữ liệu từ Cơ sở dữ liệu Oracle theo lô với kích thước lô mặc định là

import cx_Oracle import config sql = 'select customer_id, name ' \ 'from customers ' \ 'order by name' batch_size = 20 try: with cx_Oracle.connect[ config.username, config.password, config.dsn, encoding=config.encoding] as connection: with connection.cursor[] as cursor: # execute the SQL statement cursor.execute[sql] while True: # fetch rows rows = cursor.fetchmany[batch_size] if not rows: break # display rows for row in rows: print[row] except cx_Oracle.Error as error: print[error]

Code language: Python [python]
7

Để cải thiện hiệu suất, bạn có thể điều chỉnh giá trị của

import cx_Oracle import config sql = 'select customer_id, name ' \ 'from customers ' \ 'order by name' batch_size = 20 try: with cx_Oracle.connect[ config.username, config.password, config.dsn, encoding=config.encoding] as connection: with connection.cursor[] as cursor: # execute the SQL statement cursor.execute[sql] while True: # fetch rows rows = cursor.fetchmany[batch_size] if not rows: break # display rows for row in rows: print[row] except cx_Oracle.Error as error: print[error]

Code language: Python [python]
7 trước khi gọi phương thức

import cx_Oracle import config sql = 'select customer_id, name ' \ 'from customers ' \ 'order by name' try: with cx_Oracle.connect[ config.username, config.password, config.dsn, encoding=config.encoding] as connection: with connection.cursor[] as cursor: cursor.execute[sql] while True: row = cursor.fetchone[] if row is None: break print[row] except cx_Oracle.Error as error: print[error]

Code language: Python [python]
0

Lưu ý rằng việc tăng giá trị của

import cx_Oracle import config sql = 'select customer_id, name ' \ 'from customers ' \ 'order by name' batch_size = 20 try: with cx_Oracle.connect[ config.username, config.password, config.dsn, encoding=config.encoding] as connection: with connection.cursor[] as cursor: # execute the SQL statement cursor.execute[sql] while True: # fetch rows rows = cursor.fetchmany[batch_size] if not rows: break # display rows for row in rows: print[row] except cx_Oracle.Error as error: print[error]

Code language: Python [python]
7 giúp giảm số lần truy cập cơ sở dữ liệu. Tuy nhiên, nó làm tăng dung lượng bộ nhớ cần thiết

Truy vấn dữ liệu bằng phương thức

import cx_Oracle import config sql = 'select customer_id, name ' \ 'from customers ' \ 'order by name' try: with cx_Oracle.connect[ config.username, config.password, config.dsn, encoding=config.encoding] as connection: with connection.cursor[] as cursor: cursor.execute[sql] while True: row = cursor.fetchone[] if row is None: break print[row] except cx_Oracle.Error as error: print[error]

Code language: Python [python]
2

Nếu bạn muốn xử lý các hàng theo lô, bạn có thể sử dụng phương thức

import cx_Oracle import config sql = 'select customer_id, name ' \ 'from customers ' \ 'order by name' try: with cx_Oracle.connect[ config.username, config.password, config.dsn, encoding=config.encoding] as connection: with connection.cursor[] as cursor: cursor.execute[sql] while True: row = cursor.fetchone[] if row is None: break print[row] except cx_Oracle.Error as error: print[error]

Code language: Python [python]
2. Trong trường hợp này, bạn chuyển kích thước lô cho phương thức

import cx_Oracle import config sql = 'select customer_id, name ' \ 'from customers ' \ 'order by name' try: with cx_Oracle.connect[ config.username, config.password, config.dsn, encoding=config.encoding] as connection: with connection.cursor[] as cursor: cursor.execute[sql] while True: row = cursor.fetchone[] if row is None: break print[row] except cx_Oracle.Error as error: print[error]

Code language: Python [python]
2. Kích thước lô mặc định là

import cx_Oracle import config sql = 'select customer_id, name ' \ 'from customers ' \ 'order by name' batch_size = 20 try: with cx_Oracle.connect[ config.username, config.password, config.dsn, encoding=config.encoding] as connection: with connection.cursor[] as cursor: # execute the SQL statement cursor.execute[sql] while True: # fetch rows rows = cursor.fetchmany[batch_size] if not rows: break # display rows for row in rows: print[row] except cx_Oracle.Error as error: print[error]

Code language: Python [python]
7

________số 8

Truy vấn dữ liệu bằng phương pháp

import cx_Oracle import config sql = 'select customer_id, name ' \ 'from customers ' \ 'order by name' try: with cx_Oracle.connect[ config.username, config.password, config.dsn, encoding=config.encoding] as connection: with connection.cursor[] as cursor: cursor.execute[sql] while True: row = cursor.fetchone[] if row is None: break print[row] except cx_Oracle.Error as error: print[error]

Code language: Python [python]
3

Nếu số lượng hàng ít và có thể vừa với bộ nhớ, bạn có thể sử dụng phương thức

import cx_Oracle import config sql = 'select customer_id, name ' \ 'from customers ' \ 'order by name' try: with cx_Oracle.connect[ config.username, config.password, config.dsn, encoding=config.encoding] as connection: with connection.cursor[] as cursor: cursor.execute[sql] while True: row = cursor.fetchone[] if row is None: break print[row] except cx_Oracle.Error as error: print[error]

Code language: Python [python]
3

import cx_Oracle import config sql = 'select customer_id, name ' \ 'from customers ' \ 'order by name' try: with cx_Oracle.connect[ config.username, config.password, config.dsn, encoding=config.encoding] as connection: with connection.cursor[] as cursor: cursor.execute[sql] while True: row = cursor.fetchone[] if row is None: break print[row] except cx_Oracle.Error as error: print[error]

Code language: Python [python]
8

Trong hướng dẫn này, bạn đã học cách sử dụng các phương thức

import cx_Oracle import config sql = 'select customer_id, name ' \ 'from customers ' \ 'order by name' try: with cx_Oracle.connect[ config.username, config.password, config.dsn, encoding=config.encoding] as connection: with connection.cursor[] as cursor: cursor.execute[sql] while True: row = cursor.fetchone[] if row is None: break print[row] except cx_Oracle.Error as error: print[error]

Code language: Python [python]
9,

import cx_Oracle import config sql = 'select customer_id, name ' \ 'from customers ' \ 'order by name' try: with cx_Oracle.connect[ config.username, config.password, config.dsn, encoding=config.encoding] as connection: with connection.cursor[] as cursor: cursor.execute[sql] while True: row = cursor.fetchone[] if row is None: break print[row] except cx_Oracle.Error as error: print[error]

Code language: Python [python]
0 và

import cx_Oracle import config sql = 'select customer_id, name ' \ 'from customers ' \ 'order by name' try: with cx_Oracle.connect[ config.username, config.password, config.dsn, encoding=config.encoding] as connection: with connection.cursor[] as cursor: cursor.execute[sql] while True: row = cursor.fetchone[] if row is None: break print[row] except cx_Oracle.Error as error: print[error]

Code language: Python [python]
1 của đối tượng

import cx_Oracle import config sql = 'select customer_id, name ' \ 'from customers ' \ 'order by name' try: with cx_Oracle.connect[ config.username, config.password, config.dsn, encoding=config.encoding] as connection: with connection.cursor[] as cursor: cursor.execute[sql] while True: row = cursor.fetchone[] if row is None: break print[row] except cx_Oracle.Error as error: print[error]

Code language: Python [python]
3 để lấy dữ liệu từ Cơ sở dữ liệu Oracle

Fetchone trong Python là gì?

fetchone[] Phương thức này lấy hàng tiếp theo của tập kết quả truy vấn và trả về một chuỗi đơn hoặc Không có nếu không còn hàng nào nữa. By default, the returned tuple consists of data returned by the MySQL server, converted to Python objects.

Có bao nhiêu bản ghi sẽ được trả về bởi phương thức Fetchone[]?

[a] Phương thức fetchone[] sẽ trả về bao nhiêu bản ghi? . phương thức fetchone[] trả về một bản ghi hoặc Không có nếu không còn hàng nào .

Chủ Đề