Hướng dẫn cx_oracle python example - ví dụ về python cx_oracle

Cần kết nối Python với cơ sở dữ liệu Oracle bằng CX_Oracle Connect?

Nếu vậy, trong hướng dẫn ngắn này, bạn sẽ thấy các bước để thiết lập loại kết nối này từ đầu.

Bước 1: Cài đặt gói cx_oracle

Nếu bạn đã thực hiện như vậy, hãy cài đặt gói cx_oracle. Bạn có thể sử dụng cú pháp sau để cài đặt gói cx_oracle trong Windows:

pip install cx_Oracle

Bước 2: Lấy thông tin kết nối

Tiếp theo, truy xuất thông tin kết nối. Bạn có thể làm điều đó bằng cách định vị tệp tnsnames.ora của bạn trên máy tính của bạn (ví dụ: nhập tnsnames.ora trong thanh tìm kiếm windows).

Bây giờ, hãy mở tệp tnsnames.ora của bạn và tìm kết nối mong muốn của bạn.

Nó sẽ trông giống như thông tin bên dưới (được tô sáng trong màu 3 yếu tố mà bạn thường cần tìm trước khi bạn có thể thiết lập kết nối giữa Python và cơ sở dữ liệu Oracle của bạn):

System_ocon = (description = (adression = (protucol = tcp) (host = tên máy chủ) (cổng = số cổng)) (load_balance = có) (kết nối_data = (server = chuyên dụng) = Select) (Phương thức = cơ bản) (thử lại = 180) (độ trễ = 5)
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = Host Name)(PORT = Port Number))
(LOAD_BALANCE = YES)
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = Service Name)
(FAILOVER_MODE =
(TYPE = SELECT)
(METHOD = BASIC)
(RETRIES = 180)
(DELAY = 5)

Bước 3: Kết nối Python với Oracle bằng CX_Oracle Connect

Cuối cùng, sao chép/nhập cú pháp sau trong Python trong khi thêm thông tin cần thiết dựa trên kết nối Oracle của bạn:

import cx_Oracle

dsn_tns = cx_Oracle.makedsn('Host Name', 'Port Number', service_name='Service Name') # if needed, place an 'r' before any parameter in order to address special characters such as '\'.
conn = cx_Oracle.connect(user=r'User Name', password='Personal Password', dsn=dsn_tns) # if needed, place an 'r' before any parameter in order to address special characters such as '\'. For example, if your user name contains '\', you'll need to place 'r' before the user name: user=r'User Name'

c = conn.cursor()
c.execute('select * from database.table') # use triple quotes if you want to spread your query across multiple lines
for row in c:
    print (row[0], '-', row[1]) # this only shows the first two columns. To add an additional column you'll need to add , '-', row[2], etc.
#conn.close()

Xin lưu ý rằng có nhiều cách bổ sung để truy xuất thông tin cần thiết để tạo điều kiện cho kết nối của bạn với cơ sở dữ liệu Oracle.

Ví dụ: bạn có thể chạy truy vấn sau để lấy tên dịch vụ:

select sys_context('userenv','service_name') from dual

Bạn cũng có thể chạy truy vấn sau để có được danh sách người dùng:

select username from dba_users

Kết luận và tài nguyên bổ sung

Bạn chỉ thấy cách kết nối Python với Oracle bằng CX_Oracle Connect. Khi bạn đã thiết lập một kết nối như vậy, bạn có thể bắt đầu sử dụng SQL trong Pythonto Quản lý dữ liệu của mình.

Bạn có thể tìm hiểu thêm về các loại kết nối khác nhau giữa Python và các ứng dụng cơ sở dữ liệu khác bằng cách truy cập các hướng dẫn này:

  • Kết nối Python với SQL Server bằng PYODBC
  • Kết nối cơ sở dữ liệu Python với MS Access bằng PYODBC

Để biết thêm thông tin về gói & nbsp; cx_oracle, vui lòng tham khảo tài liệu & nbsp; cx_oracle.

Tổng quan

Hướng dẫn này là giới thiệu về việc sử dụng Python với cơ sở dữ liệu Oracle. Nó chứa vật liệu mới bắt đầu và nâng cao. Các phần có thể được thực hiện theo bất kỳ thứ tự. Chọn nội dung mà bạn và cấp độ kỹ năng của bạn quan tâm. Hướng dẫn có tập lệnh để chạy và sửa đổi, và đã đề xuất các giải pháp.

Python là một ngôn ngữ kịch bản động mục đích chung phổ biến. Giao diện CX_Oracle cung cấp API Python để truy cập cơ sở dữ liệu Oracle.

Nếu bạn chưa quen với Python xem lại Phụ lục: Python Primer để có được sự hiểu biết về ngôn ngữ.

Khi bạn đã hoàn thành hướng dẫn này, chúng tôi khuyên bạn nên xem xét tài liệu CX_Oracle.

Bản gốc của những hướng dẫn mà bạn đang đọc là ở đây.

Kiến trúc CX_Oracle

Các chương trình Python gọi các chức năng CX_Oracle. Bên trong CX_Oracle tự động tải Thư viện khách hàng của Oracle để truy cập cơ sở dữ liệu Oracle. Cơ sở dữ liệu có thể nằm trên cùng một máy với Python hoặc nó có thể ở xa. Nếu cơ sở dữ liệu là cục bộ, các thư viện máy khách từ cài đặt phần mềm cơ sở dữ liệu Oracle có thể được sử dụng.

Hướng dẫn cx_oracle python example - ví dụ về python cx_oracle

Thành lập

  • Cài đặt phần mềm

    Để đi, hãy làm theo một trong hai hướng dẫn bắt đầu nhanh:

    • Bắt đầu nhanh: Phát triển các ứng dụng Python cho cơ sở dữ liệu Oracle (tại chỗ)

    • Bắt đầu nhanh: Phát triển các ứng dụng Python cho cơ sở dữ liệu tự trị của Oracle

    Đối với hướng dẫn này, bạn sẽ cần Python 3.6 (hoặc muộn hơn), CX_ORACLE 7.3 (hoặc muộn hơn) và truy cập vào cơ sở dữ liệu của Oracle.

    Phần xếp hàng nâng cao yêu cầu Python cx_oracle phải sử dụng các thư viện khách hàng của Oracle 12.2 trở lên. Phần soda yêu cầu cơ sở dữ liệu Oracle 18 trở lên và Python CX_oracle phải được sử dụng các thư viện Oracle từ 18,5 hoặc muộn hơn.

  • Tải xuống các tập lệnh hướng dẫn

    Các tập lệnh Python được sử dụng trong ví dụ này là trong kho lưu trữ github cx_oracle.

    Tải xuống một tệp zip của kho lưu trữ từ đây và giải nén nó. Ngoài ra, bạn có thể sử dụng 'git' để sao chép kho lưu trữ với

    import cx_Oracle
    
    dsn_tns = cx_Oracle.makedsn('Host Name', 'Port Number', service_name='Service Name') # if needed, place an 'r' before any parameter in order to address special characters such as '\'.
    conn = cx_Oracle.connect(user=r'User Name', password='Personal Password', dsn=dsn_tns) # if needed, place an 'r' before any parameter in order to address special characters such as '\'. For example, if your user name contains '\', you'll need to place 'r' before the user name: user=r'User Name'
    
    c = conn.cursor()
    c.execute('select * from database.table') # use triple quotes if you want to spread your query across multiple lines
    for row in c:
        print (row[0], '-', row[1]) # this only shows the first two columns. To add an additional column you'll need to add , '-', row[2], etc.
    #conn.close()
    
    42

    Thư mục

    import cx_Oracle
    
    dsn_tns = cx_Oracle.makedsn('Host Name', 'Port Number', service_name='Service Name') # if needed, place an 'r' before any parameter in order to address special characters such as '\'.
    conn = cx_Oracle.connect(user=r'User Name', password='Personal Password', dsn=dsn_tns) # if needed, place an 'r' before any parameter in order to address special characters such as '\'. For example, if your user name contains '\', you'll need to place 'r' before the user name: user=r'User Name'
    
    c = conn.cursor()
    c.execute('select * from database.table') # use triple quotes if you want to spread your query across multiple lines
    for row in c:
        print (row[0], '-', row[1]) # this only shows the first two columns. To add an additional column you'll need to add , '-', row[2], etc.
    #conn.close()
    
    43 có tập lệnh để chạy và sửa đổi. Thư mục
    import cx_Oracle
    
    dsn_tns = cx_Oracle.makedsn('Host Name', 'Port Number', service_name='Service Name') # if needed, place an 'r' before any parameter in order to address special characters such as '\'.
    conn = cx_Oracle.connect(user=r'User Name', password='Personal Password', dsn=dsn_tns) # if needed, place an 'r' before any parameter in order to address special characters such as '\'. For example, if your user name contains '\', you'll need to place 'r' before the user name: user=r'User Name'
    
    c = conn.cursor()
    c.execute('select * from database.table') # use triple quotes if you want to spread your query across multiple lines
    for row in c:
        print (row[0], '-', row[1]) # this only shows the first two columns. To add an additional column you'll need to add , '-', row[2], etc.
    #conn.close()
    
    44 có các tập lệnh với các thay đổi mã được đề xuất.

  • Tạo người dùng cơ sở dữ liệu

    Nếu bạn có người dùng hiện tại, bạn có thể sử dụng nó cho hầu hết các ví dụ (một số ví dụ có thể yêu cầu thêm quyền).

    Nếu bạn cần tạo một người dùng mới, hãy xem lại các khoản tài trợ được tạo trong

    import cx_Oracle
    
    dsn_tns = cx_Oracle.makedsn('Host Name', 'Port Number', service_name='Service Name') # if needed, place an 'r' before any parameter in order to address special characters such as '\'.
    conn = cx_Oracle.connect(user=r'User Name', password='Personal Password', dsn=dsn_tns) # if needed, place an 'r' before any parameter in order to address special characters such as '\'. For example, if your user name contains '\', you'll need to place 'r' before the user name: user=r'User Name'
    
    c = conn.cursor()
    c.execute('select * from database.table') # use triple quotes if you want to spread your query across multiple lines
    for row in c:
        print (row[0], '-', row[1]) # this only shows the first two columns. To add an additional column you'll need to add , '-', row[2], etc.
    #conn.close()
    
    45. Sau đó, mở một cửa sổ đầu cuối, thay đổi sang thư mục
    import cx_Oracle
    
    dsn_tns = cx_Oracle.makedsn('Host Name', 'Port Number', service_name='Service Name') # if needed, place an 'r' before any parameter in order to address special characters such as '\'.
    conn = cx_Oracle.connect(user=r'User Name', password='Personal Password', dsn=dsn_tns) # if needed, place an 'r' before any parameter in order to address special characters such as '\'. For example, if your user name contains '\', you'll need to place 'r' before the user name: user=r'User Name'
    
    c = conn.cursor()
    c.execute('select * from database.table') # use triple quotes if you want to spread your query across multiple lines
    for row in c:
        print (row[0], '-', row[1]) # this only shows the first two columns. To add an additional column you'll need to add , '-', row[2], etc.
    #conn.close()
    
    46 và chạy tập lệnh
    import cx_Oracle
    
    dsn_tns = cx_Oracle.makedsn('Host Name', 'Port Number', service_name='Service Name') # if needed, place an 'r' before any parameter in order to address special characters such as '\'.
    conn = cx_Oracle.connect(user=r'User Name', password='Personal Password', dsn=dsn_tns) # if needed, place an 'r' before any parameter in order to address special characters such as '\'. For example, if your user name contains '\', you'll need to place 'r' before the user name: user=r'User Name'
    
    c = conn.cursor()
    c.execute('select * from database.table') # use triple quotes if you want to spread your query across multiple lines
    for row in c:
        print (row[0], '-', row[1]) # this only shows the first two columns. To add an additional column you'll need to add , '-', row[2], etc.
    #conn.close()
    
    47 làm người dùng hệ thống, ví dụ:

    cd samples/tutorial/sql
    sqlplus -l system/systempassword@localhost/orclpdb1 @create_user
    

    Ví dụ trên kết nối là người dùng hệ thống. Chuỗi kết nối là "localhost/orclpdb1", có nghĩa là sử dụng dịch vụ cơ sở dữ liệu "orclpdb1" chạy trên localhost (máy tính bạn đang chạy SQL*plus trên). Giá trị thay thế cho môi trường của bạn. Nếu bạn đang sử dụng cơ sở dữ liệu tự động của Oracle, hãy sử dụng người dùng quản trị thay vì hệ thống.

    Khi hướng dẫn kết thúc, tập lệnh

    import cx_Oracle
    
    dsn_tns = cx_Oracle.makedsn('Host Name', 'Port Number', service_name='Service Name') # if needed, place an 'r' before any parameter in order to address special characters such as '\'.
    conn = cx_Oracle.connect(user=r'User Name', password='Personal Password', dsn=dsn_tns) # if needed, place an 'r' before any parameter in order to address special characters such as '\'. For example, if your user name contains '\', you'll need to place 'r' before the user name: user=r'User Name'
    
    c = conn.cursor()
    c.execute('select * from database.table') # use triple quotes if you want to spread your query across multiple lines
    for row in c:
        print (row[0], '-', row[1]) # this only shows the first two columns. To add an additional column you'll need to add , '-', row[2], etc.
    #conn.close()
    
    48 trong cùng thư mục có thể được sử dụng để xóa người dùng hướng dẫn.

  • Cài đặt các bảng mẫu

    Khi bạn có người dùng cơ sở dữ liệu, thì bạn có thể tạo các bảng hướng dẫn bằng cách chạy một lệnh như thế này, sử dụng các giá trị của bạn cho tên người dùng hướng dẫn, mật khẩu và chuỗi kết nối:

    sqlplus -l pythonhol/welcome@localhost/orclpdb1 @setup_tables
    
  • Bắt đầu nhóm kết nối thường trú cơ sở dữ liệu (DRCP)

    Nếu bạn muốn thử các ví dụ DRCP trong Phần 2, hãy bắt đầu nhóm DRCP. (Nhóm đã được bắt đầu trong cơ sở dữ liệu tự trị của Oracle).

    Chạy SQL*Plus với các đặc quyền SYSDBA, ví dụ:

    sqlplus -l sys/syspassword@localhost/orclcdb as sysdba
    

    và thực hiện lệnh:

    execute dbms_connection_pool.start_pool()
    

    Lưu ý Bạn có thể cần phải làm điều này trong cơ sở dữ liệu container, không phải là cơ sở dữ liệu có thể cắm được.

  • Xem lại thông tin xác thực kết nối được sử dụng bởi các tập lệnh hướng dẫn

    Xem lại

    import cx_Oracle
    
    dsn_tns = cx_Oracle.makedsn('Host Name', 'Port Number', service_name='Service Name') # if needed, place an 'r' before any parameter in order to address special characters such as '\'.
    conn = cx_Oracle.connect(user=r'User Name', password='Personal Password', dsn=dsn_tns) # if needed, place an 'r' before any parameter in order to address special characters such as '\'. For example, if your user name contains '\', you'll need to place 'r' before the user name: user=r'User Name'
    
    c = conn.cursor()
    c.execute('select * from database.table') # use triple quotes if you want to spread your query across multiple lines
    for row in c:
        print (row[0], '-', row[1]) # this only shows the first two columns. To add an additional column you'll need to add , '-', row[2], etc.
    #conn.close()
    
    49 và
    import cx_Oracle
    
    dsn_tns = cx_Oracle.makedsn('Host Name', 'Port Number', service_name='Service Name') # if needed, place an 'r' before any parameter in order to address special characters such as '\'.
    conn = cx_Oracle.connect(user=r'User Name', password='Personal Password', dsn=dsn_tns) # if needed, place an 'r' before any parameter in order to address special characters such as '\'. For example, if your user name contains '\', you'll need to place 'r' before the user name: user=r'User Name'
    
    c = conn.cursor()
    c.execute('select * from database.table') # use triple quotes if you want to spread your query across multiple lines
    for row in c:
        print (row[0], '-', row[1]) # this only shows the first two columns. To add an additional column you'll need to add , '-', row[2], etc.
    #conn.close()
    
    50 trong thư mục
    import cx_Oracle
    
    dsn_tns = cx_Oracle.makedsn('Host Name', 'Port Number', service_name='Service Name') # if needed, place an 'r' before any parameter in order to address special characters such as '\'.
    conn = cx_Oracle.connect(user=r'User Name', password='Personal Password', dsn=dsn_tns) # if needed, place an 'r' before any parameter in order to address special characters such as '\'. For example, if your user name contains '\', you'll need to place 'r' before the user name: user=r'User Name'
    
    c = conn.cursor()
    c.execute('select * from database.table') # use triple quotes if you want to spread your query across multiple lines
    for row in c:
        print (row[0], '-', row[1]) # this only shows the first two columns. To add an additional column you'll need to add , '-', row[2], etc.
    #conn.close()
    
    51. Chúng được bao gồm trong các tệp Python và SQL khác.

    Chỉnh sửa

    import cx_Oracle
    
    dsn_tns = cx_Oracle.makedsn('Host Name', 'Port Number', service_name='Service Name') # if needed, place an 'r' before any parameter in order to address special characters such as '\'.
    conn = cx_Oracle.connect(user=r'User Name', password='Personal Password', dsn=dsn_tns) # if needed, place an 'r' before any parameter in order to address special characters such as '\'. For example, if your user name contains '\', you'll need to place 'r' before the user name: user=r'User Name'
    
    c = conn.cursor()
    c.execute('select * from database.table') # use triple quotes if you want to spread your query across multiple lines
    for row in c:
        print (row[0], '-', row[1]) # this only shows the first two columns. To add an additional column you'll need to add , '-', row[2], etc.
    #conn.close()
    
    49 và thay đổi các giá trị mặc định để phù hợp với thông tin kết nối cho môi trường của bạn. Ngoài ra, bạn có thể đặt các biến gây ra đã cho trong cửa sổ thiết bị đầu cuối của bạn. Ví dụ: tên người dùng mặc định là "Pythonhol" trừ khi biến gây ra "python_user" chứa một tên người dùng khác. Chuỗi kết nối mặc định dành cho dịch vụ cơ sở dữ liệu 'OrclPDB1' trên cùng một máy với Python. .

    user = os.environ.get("PYTHON_USER", "pythonhol")
    
    dsn = os.environ.get("PYTHON_CONNECT_STRING", "localhost/orclpdb1")
    
    pw = os.environ.get("PYTHON_PASSWORD")
    if pw is None:
        pw = getpass.getpass("Enter password for %s: " % user)
    

    Đồng thời thay đổi tên người dùng mặc định và chuỗi kết nối trong tệp cấu hình SQL*Plus

    import cx_Oracle
    
    dsn_tns = cx_Oracle.makedsn('Host Name', 'Port Number', service_name='Service Name') # if needed, place an 'r' before any parameter in order to address special characters such as '\'.
    conn = cx_Oracle.connect(user=r'User Name', password='Personal Password', dsn=dsn_tns) # if needed, place an 'r' before any parameter in order to address special characters such as '\'. For example, if your user name contains '\', you'll need to place 'r' before the user name: user=r'User Name'
    
    c = conn.cursor()
    c.execute('select * from database.table') # use triple quotes if you want to spread your query across multiple lines
    for row in c:
        print (row[0], '-', row[1]) # this only shows the first two columns. To add an additional column you'll need to add , '-', row[2], etc.
    #conn.close()
    
    50:

    -- Default database username
    def user = "pythonhol"
    
    -- Default database connection string
    def connect_string = "localhost/orclpdb1"
    
    -- Prompt for the password
    accept pw char prompt 'Enter database password for &user: ' hide
    

    Các hướng dẫn hướng dẫn có thể cần điều chỉnh, tùy thuộc vào cách bạn đã thiết lập môi trường của mình.

  • Xem lại đường dẫn thư viện máy khách tức thì

    Xem lại cài đặt đường dẫn thư viện khách hàng của Oracle trong

    import cx_Oracle
    
    dsn_tns = cx_Oracle.makedsn('Host Name', 'Port Number', service_name='Service Name') # if needed, place an 'r' before any parameter in order to address special characters such as '\'.
    conn = cx_Oracle.connect(user=r'User Name', password='Personal Password', dsn=dsn_tns) # if needed, place an 'r' before any parameter in order to address special characters such as '\'. For example, if your user name contains '\', you'll need to place 'r' before the user name: user=r'User Name'
    
    c = conn.cursor()
    c.execute('select * from database.table') # use triple quotes if you want to spread your query across multiple lines
    for row in c:
        print (row[0], '-', row[1]) # this only shows the first two columns. To add an additional column you'll need to add , '-', row[2], etc.
    #conn.close()
    
    49. Nếu CX_Oracle không thể định vị các thư viện khách hàng của Oracle, thì các ứng dụng của bạn sẽ thất bại với lỗi như "DPI-1047: Không thể xác định vị trí thư viện máy khách Oracle 64 bit".

    import cx_Oracle
    
    dsn_tns = cx_Oracle.makedsn('Host Name', 'Port Number', service_name='Service Name') # if needed, place an 'r' before any parameter in order to address special characters such as '\'.
    conn = cx_Oracle.connect(user=r'User Name', password='Personal Password', dsn=dsn_tns) # if needed, place an 'r' before any parameter in order to address special characters such as '\'. For example, if your user name contains '\', you'll need to place 'r' before the user name: user=r'User Name'
    
    c = conn.cursor()
    c.execute('select * from database.table') # use triple quotes if you want to spread your query across multiple lines
    for row in c:
        print (row[0], '-', row[1]) # this only shows the first two columns. To add an additional column you'll need to add , '-', row[2], etc.
    #conn.close()
    
    0

    Đặt

    import cx_Oracle
    
    dsn_tns = cx_Oracle.makedsn('Host Name', 'Port Number', service_name='Service Name') # if needed, place an 'r' before any parameter in order to address special characters such as '\'.
    conn = cx_Oracle.connect(user=r'User Name', password='Personal Password', dsn=dsn_tns) # if needed, place an 'r' before any parameter in order to address special characters such as '\'. For example, if your user name contains '\', you'll need to place 'r' before the user name: user=r'User Name'
    
    c = conn.cursor()
    c.execute('select * from database.table') # use triple quotes if you want to spread your query across multiple lines
    for row in c:
        print (row[0], '-', row[1]) # this only shows the first two columns. To add an additional column you'll need to add , '-', row[2], etc.
    #conn.close()
    
    55 thành
    import cx_Oracle
    
    dsn_tns = cx_Oracle.makedsn('Host Name', 'Port Number', service_name='Service Name') # if needed, place an 'r' before any parameter in order to address special characters such as '\'.
    conn = cx_Oracle.connect(user=r'User Name', password='Personal Password', dsn=dsn_tns) # if needed, place an 'r' before any parameter in order to address special characters such as '\'. For example, if your user name contains '\', you'll need to place 'r' before the user name: user=r'User Name'
    
    c = conn.cursor()
    c.execute('select * from database.table') # use triple quotes if you want to spread your query across multiple lines
    for row in c:
        print (row[0], '-', row[1]) # this only shows the first two columns. To add an additional column you'll need to add , '-', row[2], etc.
    #conn.close()
    
    56 hoặc thành một đường dẫn hợp lệ theo các ghi chú sau:

    • Nếu bạn đang ở trên macOS hoặc Windows và bạn đã cài đặt thư viện máy khách Instant Oracle vì cơ sở dữ liệu của bạn nằm trên máy từ xa, thì hãy đặt

      import cx_Oracle
      
      dsn_tns = cx_Oracle.makedsn('Host Name', 'Port Number', service_name='Service Name') # if needed, place an 'r' before any parameter in order to address special characters such as '\'.
      conn = cx_Oracle.connect(user=r'User Name', password='Personal Password', dsn=dsn_tns) # if needed, place an 'r' before any parameter in order to address special characters such as '\'. For example, if your user name contains '\', you'll need to place 'r' before the user name: user=r'User Name'
      
      c = conn.cursor()
      c.execute('select * from database.table') # use triple quotes if you want to spread your query across multiple lines
      for row in c:
          print (row[0], '-', row[1]) # this only shows the first two columns. To add an additional column you'll need to add , '-', row[2], etc.
      #conn.close()
      
      55 thành đường dẫn của thư viện máy khách tức thời.

    • Nếu bạn đang ở trên Windows và cài đặt cơ sở dữ liệu cục bộ, thì hãy nhận xét hai dòng Windows, để

      import cx_Oracle
      
      dsn_tns = cx_Oracle.makedsn('Host Name', 'Port Number', service_name='Service Name') # if needed, place an 'r' before any parameter in order to address special characters such as '\'.
      conn = cx_Oracle.connect(user=r'User Name', password='Personal Password', dsn=dsn_tns) # if needed, place an 'r' before any parameter in order to address special characters such as '\'. For example, if your user name contains '\', you'll need to place 'r' before the user name: user=r'User Name'
      
      c = conn.cursor()
      c.execute('select * from database.table') # use triple quotes if you want to spread your query across multiple lines
      for row in c:
          print (row[0], '-', row[1]) # this only shows the first two columns. To add an additional column you'll need to add , '-', row[2], etc.
      #conn.close()
      
      55 vẫn còn
      import cx_Oracle
      
      dsn_tns = cx_Oracle.makedsn('Host Name', 'Port Number', service_name='Service Name') # if needed, place an 'r' before any parameter in order to address special characters such as '\'.
      conn = cx_Oracle.connect(user=r'User Name', password='Personal Password', dsn=dsn_tns) # if needed, place an 'r' before any parameter in order to address special characters such as '\'. For example, if your user name contains '\', you'll need to place 'r' before the user name: user=r'User Name'
      
      c = conn.cursor()
      c.execute('select * from database.table') # use triple quotes if you want to spread your query across multiple lines
      for row in c:
          print (row[0], '-', row[1]) # this only shows the first two columns. To add an additional column you'll need to add , '-', row[2], etc.
      #conn.close()
      
      56.

    • Trong tất cả các trường hợp khác (bao gồm Linux với Oracle Instant Client), hãy đảm bảo rằng

      import cx_Oracle
      
      dsn_tns = cx_Oracle.makedsn('Host Name', 'Port Number', service_name='Service Name') # if needed, place an 'r' before any parameter in order to address special characters such as '\'.
      conn = cx_Oracle.connect(user=r'User Name', password='Personal Password', dsn=dsn_tns) # if needed, place an 'r' before any parameter in order to address special characters such as '\'. For example, if your user name contains '\', you'll need to place 'r' before the user name: user=r'User Name'
      
      c = conn.cursor()
      c.execute('select * from database.table') # use triple quotes if you want to spread your query across multiple lines
      for row in c:
          print (row[0], '-', row[1]) # this only shows the first two columns. To add an additional column you'll need to add , '-', row[2], etc.
      #conn.close()
      
      55 được đặt thành
      import cx_Oracle
      
      dsn_tns = cx_Oracle.makedsn('Host Name', 'Port Number', service_name='Service Name') # if needed, place an 'r' before any parameter in order to address special characters such as '\'.
      conn = cx_Oracle.connect(user=r'User Name', password='Personal Password', dsn=dsn_tns) # if needed, place an 'r' before any parameter in order to address special characters such as '\'. For example, if your user name contains '\', you'll need to place 'r' before the user name: user=r'User Name'
      
      c = conn.cursor()
      c.execute('select * from database.table') # use triple quotes if you want to spread your query across multiple lines
      for row in c:
          print (row[0], '-', row[1]) # this only shows the first two columns. To add an additional column you'll need to add , '-', row[2], etc.
      #conn.close()
      
      56. Trong những trường hợp này, bạn phải đảm bảo rằng các thư viện Oracle từ Instant Client hoặc Oracle_home của bạn nằm trong đường dẫn tìm kiếm thư viện hệ thống của bạn trước khi bạn bắt đầu Python. Trên Linux, đường dẫn có thể được cấu hình bằng LDConfig hoặc với các biến môi trường LD_L Library_Path.

1. Kết nối với Oracle

Bạn có thể kết nối từ Python với cơ sở dữ liệu cục bộ, từ xa hoặc đám mây. Liên kết tài liệu để đọc thêm: Kết nối với cơ sở dữ liệu Oracle.

  • 1.1 Tạo kết nối cơ bản

    Xem lại mã có trong

    import cx_Oracle
    
    dsn_tns = cx_Oracle.makedsn('Host Name', 'Port Number', service_name='Service Name') # if needed, place an 'r' before any parameter in order to address special characters such as '\'.
    conn = cx_Oracle.connect(user=r'User Name', password='Personal Password', dsn=dsn_tns) # if needed, place an 'r' before any parameter in order to address special characters such as '\'. For example, if your user name contains '\', you'll need to place 'r' before the user name: user=r'User Name'
    
    c = conn.cursor()
    c.execute('select * from database.table') # use triple quotes if you want to spread your query across multiple lines
    for row in c:
        print (row[0], '-', row[1]) # this only shows the first two columns. To add an additional column you'll need to add , '-', row[2], etc.
    #conn.close()
    
    62:

    import cx_Oracle
    
    dsn_tns = cx_Oracle.makedsn('Host Name', 'Port Number', service_name='Service Name') # if needed, place an 'r' before any parameter in order to address special characters such as '\'.
    conn = cx_Oracle.connect(user=r'User Name', password='Personal Password', dsn=dsn_tns) # if needed, place an 'r' before any parameter in order to address special characters such as '\'. For example, if your user name contains '\', you'll need to place 'r' before the user name: user=r'User Name'
    
    c = conn.cursor()
    c.execute('select * from database.table') # use triple quotes if you want to spread your query across multiple lines
    for row in c:
        print (row[0], '-', row[1]) # this only shows the first two columns. To add an additional column you'll need to add , '-', row[2], etc.
    #conn.close()
    
    1

    Mô -đun CX_Oracle được nhập để cung cấp API để truy cập cơ sở dữ liệu Oracle. Nhiều mô -đun bên thứ ba và bên thứ ba có thể được bao gồm trong các kịch bản Python theo cách này.

    Phương thức

    import cx_Oracle
    
    dsn_tns = cx_Oracle.makedsn('Host Name', 'Port Number', service_name='Service Name') # if needed, place an 'r' before any parameter in order to address special characters such as '\'.
    conn = cx_Oracle.connect(user=r'User Name', password='Personal Password', dsn=dsn_tns) # if needed, place an 'r' before any parameter in order to address special characters such as '\'. For example, if your user name contains '\', you'll need to place 'r' before the user name: user=r'User Name'
    
    c = conn.cursor()
    c.execute('select * from database.table') # use triple quotes if you want to spread your query across multiple lines
    for row in c:
        print (row[0], '-', row[1]) # this only shows the first two columns. To add an additional column you'll need to add , '-', row[2], etc.
    #conn.close()
    
    63 được truyền tên người dùng, mật khẩu và chuỗi kết nối mà bạn đã cấu hình trong mô -đun db_config.py. Trong trường hợp này, cú pháp chuỗi kết nối dễ dàng của Oracle được sử dụng. Nó bao gồm tên máy chủ của máy của bạn,
    import cx_Oracle
    
    dsn_tns = cx_Oracle.makedsn('Host Name', 'Port Number', service_name='Service Name') # if needed, place an 'r' before any parameter in order to address special characters such as '\'.
    conn = cx_Oracle.connect(user=r'User Name', password='Personal Password', dsn=dsn_tns) # if needed, place an 'r' before any parameter in order to address special characters such as '\'. For example, if your user name contains '\', you'll need to place 'r' before the user name: user=r'User Name'
    
    c = conn.cursor()
    c.execute('select * from database.table') # use triple quotes if you want to spread your query across multiple lines
    for row in c:
        print (row[0], '-', row[1]) # this only shows the first two columns. To add an additional column you'll need to add , '-', row[2], etc.
    #conn.close()
    
    64 và tên dịch vụ cơ sở dữ liệu
    import cx_Oracle
    
    dsn_tns = cx_Oracle.makedsn('Host Name', 'Port Number', service_name='Service Name') # if needed, place an 'r' before any parameter in order to address special characters such as '\'.
    conn = cx_Oracle.connect(user=r'User Name', password='Personal Password', dsn=dsn_tns) # if needed, place an 'r' before any parameter in order to address special characters such as '\'. For example, if your user name contains '\', you'll need to place 'r' before the user name: user=r'User Name'
    
    c = conn.cursor()
    c.execute('select * from database.table') # use triple quotes if you want to spread your query across multiple lines
    for row in c:
        print (row[0], '-', row[1]) # this only shows the first two columns. To add an additional column you'll need to add , '-', row[2], etc.
    #conn.close()
    
    65. (Trong thuật ngữ API cơ sở dữ liệu Python, tham số chuỗi kết nối được gọi là "Tên nguồn dữ liệu" hoặc "DSN".)

    Mở một thiết bị đầu cuối lệnh và thay đổi sang thư mục

    import cx_Oracle
    
    dsn_tns = cx_Oracle.makedsn('Host Name', 'Port Number', service_name='Service Name') # if needed, place an 'r' before any parameter in order to address special characters such as '\'.
    conn = cx_Oracle.connect(user=r'User Name', password='Personal Password', dsn=dsn_tns) # if needed, place an 'r' before any parameter in order to address special characters such as '\'. For example, if your user name contains '\', you'll need to place 'r' before the user name: user=r'User Name'
    
    c = conn.cursor()
    c.execute('select * from database.table') # use triple quotes if you want to spread your query across multiple lines
    for row in c:
        print (row[0], '-', row[1]) # this only shows the first two columns. To add an additional column you'll need to add , '-', row[2], etc.
    #conn.close()
    
    51:

    import cx_Oracle
    
    dsn_tns = cx_Oracle.makedsn('Host Name', 'Port Number', service_name='Service Name') # if needed, place an 'r' before any parameter in order to address special characters such as '\'.
    conn = cx_Oracle.connect(user=r'User Name', password='Personal Password', dsn=dsn_tns) # if needed, place an 'r' before any parameter in order to address special characters such as '\'. For example, if your user name contains '\', you'll need to place 'r' before the user name: user=r'User Name'
    
    c = conn.cursor()
    c.execute('select * from database.table') # use triple quotes if you want to spread your query across multiple lines
    for row in c:
        print (row[0], '-', row[1]) # this only shows the first two columns. To add an additional column you'll need to add , '-', row[2], etc.
    #conn.close()
    
    2

    Chạy tập lệnh Python:

    import cx_Oracle
    
    dsn_tns = cx_Oracle.makedsn('Host Name', 'Port Number', service_name='Service Name') # if needed, place an 'r' before any parameter in order to address special characters such as '\'.
    conn = cx_Oracle.connect(user=r'User Name', password='Personal Password', dsn=dsn_tns) # if needed, place an 'r' before any parameter in order to address special characters such as '\'. For example, if your user name contains '\', you'll need to place 'r' before the user name: user=r'User Name'
    
    c = conn.cursor()
    c.execute('select * from database.table') # use triple quotes if you want to spread your query across multiple lines
    for row in c:
        print (row[0], '-', row[1]) # this only shows the first two columns. To add an additional column you'll need to add , '-', row[2], etc.
    #conn.close()
    
    3

    Số phiên bản của cơ sở dữ liệu phải được hiển thị. Một ngoại lệ được nâng lên nếu kết nối không thành công. Điều chỉnh tên người dùng, mật khẩu hoặc tham số chuỗi kết nối thành các giá trị không hợp lệ để xem ngoại lệ.

    CX_Oracle cũng hỗ trợ "Xác thực bên ngoài", cho phép các kết nối mà không cần tên người dùng và mật khẩu được nhúng vào mã. Xác thực sau đó sẽ được thực hiện bằng cách, ví dụ, ví LDAP hoặc Oracle.

  • 1.2 thụt nhói cho biết cấu trúc mã

    Không có trình kết thúc tuyên bố hoặc từ khóa bắt đầu/kết thúc để chỉ ra các khối mã.

    Mở

    import cx_Oracle
    
    dsn_tns = cx_Oracle.makedsn('Host Name', 'Port Number', service_name='Service Name') # if needed, place an 'r' before any parameter in order to address special characters such as '\'.
    conn = cx_Oracle.connect(user=r'User Name', password='Personal Password', dsn=dsn_tns) # if needed, place an 'r' before any parameter in order to address special characters such as '\'. For example, if your user name contains '\', you'll need to place 'r' before the user name: user=r'User Name'
    
    c = conn.cursor()
    c.execute('select * from database.table') # use triple quotes if you want to spread your query across multiple lines
    for row in c:
        print (row[0], '-', row[1]) # this only shows the first two columns. To add an additional column you'll need to add , '-', row[2], etc.
    #conn.close()
    
    62 trong một biên tập viên. Thụt vào câu lệnh in với một số không gian:

    import cx_Oracle
    
    dsn_tns = cx_Oracle.makedsn('Host Name', 'Port Number', service_name='Service Name') # if needed, place an 'r' before any parameter in order to address special characters such as '\'.
    conn = cx_Oracle.connect(user=r'User Name', password='Personal Password', dsn=dsn_tns) # if needed, place an 'r' before any parameter in order to address special characters such as '\'. For example, if your user name contains '\', you'll need to place 'r' before the user name: user=r'User Name'
    
    c = conn.cursor()
    c.execute('select * from database.table') # use triple quotes if you want to spread your query across multiple lines
    for row in c:
        print (row[0], '-', row[1]) # this only shows the first two columns. To add an additional column you'll need to add , '-', row[2], etc.
    #conn.close()
    
    4

    Lưu tập lệnh và chạy lại:

    import cx_Oracle
    
    dsn_tns = cx_Oracle.makedsn('Host Name', 'Port Number', service_name='Service Name') # if needed, place an 'r' before any parameter in order to address special characters such as '\'.
    conn = cx_Oracle.connect(user=r'User Name', password='Personal Password', dsn=dsn_tns) # if needed, place an 'r' before any parameter in order to address special characters such as '\'. For example, if your user name contains '\', you'll need to place 'r' before the user name: user=r'User Name'
    
    c = conn.cursor()
    c.execute('select * from database.table') # use triple quotes if you want to spread your query across multiple lines
    for row in c:
        print (row[0], '-', row[1]) # this only shows the first two columns. To add an additional column you'll need to add , '-', row[2], etc.
    #conn.close()
    
    5

    Điều này làm tăng một ngoại lệ về thụt lề. Số lượng không gian hoặc tab phải nhất quán trong mỗi khối; Mặt khác, trình thông dịch Python sẽ gây ra một ngoại lệ hoặc thực thi mã bất ngờ.

    Python có thể không phải lúc nào cũng có thể xác định được tình cờ từ thụt lề có chủ ý. Kiểm tra thụt lề của bạn là chính xác trước khi chạy mỗi ví dụ. Đảm bảo thụt lên tất cả các khối tuyên bố như nhau. Lưu ý các tập tin mẫu sử dụng không gian, không phải tab.Note the sample files use spaces, not tabs.

  • 1.3 Thực hiện truy vấn

    Mở

    import cx_Oracle
    
    dsn_tns = cx_Oracle.makedsn('Host Name', 'Port Number', service_name='Service Name') # if needed, place an 'r' before any parameter in order to address special characters such as '\'.
    conn = cx_Oracle.connect(user=r'User Name', password='Personal Password', dsn=dsn_tns) # if needed, place an 'r' before any parameter in order to address special characters such as '\'. For example, if your user name contains '\', you'll need to place 'r' before the user name: user=r'User Name'
    
    c = conn.cursor()
    c.execute('select * from database.table') # use triple quotes if you want to spread your query across multiple lines
    for row in c:
        print (row[0], '-', row[1]) # this only shows the first two columns. To add an additional column you'll need to add , '-', row[2], etc.
    #conn.close()
    
    68 trong một biên tập viên. Nó có vẻ như:

    import cx_Oracle
    
    dsn_tns = cx_Oracle.makedsn('Host Name', 'Port Number', service_name='Service Name') # if needed, place an 'r' before any parameter in order to address special characters such as '\'.
    conn = cx_Oracle.connect(user=r'User Name', password='Personal Password', dsn=dsn_tns) # if needed, place an 'r' before any parameter in order to address special characters such as '\'. For example, if your user name contains '\', you'll need to place 'r' before the user name: user=r'User Name'
    
    c = conn.cursor()
    c.execute('select * from database.table') # use triple quotes if you want to spread your query across multiple lines
    for row in c:
        print (row[0], '-', row[1]) # this only shows the first two columns. To add an additional column you'll need to add , '-', row[2], etc.
    #conn.close()
    
    6

    Chỉnh sửa tệp và thêm mã được hiển thị in đậm bên dưới:

    import cx_Oracle
    
    dsn_tns = cx_Oracle.makedsn('Host Name', 'Port Number', service_name='Service Name') # if needed, place an 'r' before any parameter in order to address special characters such as '\'.
    conn = cx_Oracle.connect(user=r'User Name', password='Personal Password', dsn=dsn_tns) # if needed, place an 'r' before any parameter in order to address special characters such as '\'. For example, if your user name contains '\', you'll need to place 'r' before the user name: user=r'User Name'
    
    c = conn.cursor()
    c.execute('select * from database.table') # use triple quotes if you want to spread your query across multiple lines
    for row in c:
        print (row[0], '-', row[1]) # this only shows the first two columns. To add an additional column you'll need to add , '-', row[2], etc.
    #conn.close()
    
    7

    Hãy chắc chắn rằng dòng

    import cx_Oracle
    
    dsn_tns = cx_Oracle.makedsn('Host Name', 'Port Number', service_name='Service Name') # if needed, place an 'r' before any parameter in order to address special characters such as '\'.
    conn = cx_Oracle.connect(user=r'User Name', password='Personal Password', dsn=dsn_tns) # if needed, place an 'r' before any parameter in order to address special characters such as '\'. For example, if your user name contains '\', you'll need to place 'r' before the user name: user=r'User Name'
    
    c = conn.cursor()
    c.execute('select * from database.table') # use triple quotes if you want to spread your query across multiple lines
    for row in c:
        print (row[0], '-', row[1]) # this only shows the first two columns. To add an additional column you'll need to add , '-', row[2], etc.
    #conn.close()
    
    69 được thụt vào. Phòng thí nghiệm này sử dụng không gian, không phải tab.

    Mã thực thi một truy vấn và tìm nạp tất cả dữ liệu.

    Lưu tệp và chạy nó:

    import cx_Oracle
    
    dsn_tns = cx_Oracle.makedsn('Host Name', 'Port Number', service_name='Service Name') # if needed, place an 'r' before any parameter in order to address special characters such as '\'.
    conn = cx_Oracle.connect(user=r'User Name', password='Personal Password', dsn=dsn_tns) # if needed, place an 'r' before any parameter in order to address special characters such as '\'. For example, if your user name contains '\', you'll need to place 'r' before the user name: user=r'User Name'
    
    c = conn.cursor()
    c.execute('select * from database.table') # use triple quotes if you want to spread your query across multiple lines
    for row in c:
        print (row[0], '-', row[1]) # this only shows the first two columns. To add an additional column you'll need to add , '-', row[2], etc.
    #conn.close()
    
    8

    Trong mỗi lần lặp vòng lặp, một hàng mới được lưu trữ trong

    import cx_Oracle
    
    dsn_tns = cx_Oracle.makedsn('Host Name', 'Port Number', service_name='Service Name') # if needed, place an 'r' before any parameter in order to address special characters such as '\'.
    conn = cx_Oracle.connect(user=r'User Name', password='Personal Password', dsn=dsn_tns) # if needed, place an 'r' before any parameter in order to address special characters such as '\'. For example, if your user name contains '\', you'll need to place 'r' before the user name: user=r'User Name'
    
    c = conn.cursor()
    c.execute('select * from database.table') # use triple quotes if you want to spread your query across multiple lines
    for row in c:
        print (row[0], '-', row[1]) # this only shows the first two columns. To add an additional column you'll need to add , '-', row[2], etc.
    #conn.close()
    
    70 dưới dạng "tuple" python và được hiển thị.

    Dữ liệu tìm nạp được mô tả thêm trong Phần 3.

  • 1.4 Kết nối đóng

    Các kết nối và các tài nguyên khác được sử dụng bởi cx_oracle sẽ tự động được đóng ở cuối phạm vi. Đây là một phong cách lập trình phổ biến chăm sóc đúng thứ tự đóng tài nguyên.

    Tài nguyên cũng có thể được đóng một cách rõ ràng để giải phóng các tài nguyên cơ sở dữ liệu nếu chúng không còn cần thiết. Điều này được khuyến nghị mạnh mẽ trong các khối mã vẫn còn hoạt động trong một thời gian.

    Mở

    import cx_Oracle
    
    dsn_tns = cx_Oracle.makedsn('Host Name', 'Port Number', service_name='Service Name') # if needed, place an 'r' before any parameter in order to address special characters such as '\'.
    conn = cx_Oracle.connect(user=r'User Name', password='Personal Password', dsn=dsn_tns) # if needed, place an 'r' before any parameter in order to address special characters such as '\'. For example, if your user name contains '\', you'll need to place 'r' before the user name: user=r'User Name'
    
    c = conn.cursor()
    c.execute('select * from database.table') # use triple quotes if you want to spread your query across multiple lines
    for row in c:
        print (row[0], '-', row[1]) # this only shows the first two columns. To add an additional column you'll need to add , '-', row[2], etc.
    #conn.close()
    
    68 trong trình chỉnh sửa và thêm các cuộc gọi để đóng con trỏ và kết nối như:

    import cx_Oracle
    
    dsn_tns = cx_Oracle.makedsn('Host Name', 'Port Number', service_name='Service Name') # if needed, place an 'r' before any parameter in order to address special characters such as '\'.
    conn = cx_Oracle.connect(user=r'User Name', password='Personal Password', dsn=dsn_tns) # if needed, place an 'r' before any parameter in order to address special characters such as '\'. For example, if your user name contains '\', you'll need to place 'r' before the user name: user=r'User Name'
    
    c = conn.cursor()
    c.execute('select * from database.table') # use triple quotes if you want to spread your query across multiple lines
    for row in c:
        print (row[0], '-', row[1]) # this only shows the first two columns. To add an additional column you'll need to add , '-', row[2], etc.
    #conn.close()
    
    9

    Chạy tập lệnh hoàn thành mà không có lỗi:

    import cx_Oracle
    
    dsn_tns = cx_Oracle.makedsn('Host Name', 'Port Number', service_name='Service Name') # if needed, place an 'r' before any parameter in order to address special characters such as '\'.
    conn = cx_Oracle.connect(user=r'User Name', password='Personal Password', dsn=dsn_tns) # if needed, place an 'r' before any parameter in order to address special characters such as '\'. For example, if your user name contains '\', you'll need to place 'r' before the user name: user=r'User Name'
    
    c = conn.cursor()
    c.execute('select * from database.table') # use triple quotes if you want to spread your query across multiple lines
    for row in c:
        print (row[0], '-', row[1]) # this only shows the first two columns. To add an additional column you'll need to add , '-', row[2], etc.
    #conn.close()
    
    8

    Nếu bạn trao đổi thứ tự của hai cuộc gọi

    import cx_Oracle
    
    dsn_tns = cx_Oracle.makedsn('Host Name', 'Port Number', service_name='Service Name') # if needed, place an 'r' before any parameter in order to address special characters such as '\'.
    conn = cx_Oracle.connect(user=r'User Name', password='Personal Password', dsn=dsn_tns) # if needed, place an 'r' before any parameter in order to address special characters such as '\'. For example, if your user name contains '\', you'll need to place 'r' before the user name: user=r'User Name'
    
    c = conn.cursor()
    c.execute('select * from database.table') # use triple quotes if you want to spread your query across multiple lines
    for row in c:
        print (row[0], '-', row[1]) # this only shows the first two columns. To add an additional column you'll need to add , '-', row[2], etc.
    #conn.close()
    
    72, bạn sẽ thấy lỗi.

  • 1.5 phiên bản kiểm tra

    Xem lại mã có trong

    import cx_Oracle
    
    dsn_tns = cx_Oracle.makedsn('Host Name', 'Port Number', service_name='Service Name') # if needed, place an 'r' before any parameter in order to address special characters such as '\'.
    conn = cx_Oracle.connect(user=r'User Name', password='Personal Password', dsn=dsn_tns) # if needed, place an 'r' before any parameter in order to address special characters such as '\'. For example, if your user name contains '\', you'll need to place 'r' before the user name: user=r'User Name'
    
    c = conn.cursor()
    c.execute('select * from database.table') # use triple quotes if you want to spread your query across multiple lines
    for row in c:
        print (row[0], '-', row[1]) # this only shows the first two columns. To add an additional column you'll need to add , '-', row[2], etc.
    #conn.close()
    
    73:

    select sys_context('userenv','service_name') from dual
    
    1

    Chạy tập lệnh:

    select sys_context('userenv','service_name') from dual
    
    2

    Điều này cung cấp cho phiên bản của giao diện CX_Oracle.

    Chỉnh sửa tệp để in phiên bản của cơ sở dữ liệu và của các thư viện máy khách Oracle được sử dụng bởi cx_oracle:

    select sys_context('userenv','service_name') from dual
    
    3

    Khi tập lệnh được chạy, nó sẽ hiển thị:

    select sys_context('userenv','service_name') from dual
    
    4

    Lưu ý phiên bản máy khách là một tuple.

    Bất kỳ cài đặt cx_oracle nào cũng có thể kết nối với các phiên bản cơ sở dữ liệu Oracle cũ hơn và mới hơn. Bằng cách kiểm tra các số phiên bản cơ sở dữ liệu và máy khách của Oracle, ứng dụng có thể sử dụng các tính năng tốt nhất của Oracle.

2. Kết nối gộp

Việc gộp kết nối rất quan trọng đối với hiệu suất khi các ứng dụng đa luồng thường xuyên kết nối và ngắt kết nối với cơ sở dữ liệu. Tổng hợp cũng cung cấp hỗ trợ tốt nhất cho các tính năng sẵn có của Oracle. Liên kết tài liệu để đọc thêm: Kết nối gộp.

  • 2.1 Kết nối gộp

    Xem lại mã có trong

    import cx_Oracle
    
    dsn_tns = cx_Oracle.makedsn('Host Name', 'Port Number', service_name='Service Name') # if needed, place an 'r' before any parameter in order to address special characters such as '\'.
    conn = cx_Oracle.connect(user=r'User Name', password='Personal Password', dsn=dsn_tns) # if needed, place an 'r' before any parameter in order to address special characters such as '\'. For example, if your user name contains '\', you'll need to place 'r' before the user name: user=r'User Name'
    
    c = conn.cursor()
    c.execute('select * from database.table') # use triple quotes if you want to spread your query across multiple lines
    for row in c:
        print (row[0], '-', row[1]) # this only shows the first two columns. To add an additional column you'll need to add , '-', row[2], etc.
    #conn.close()
    
    74:

    select sys_context('userenv','service_name') from dual
    
    5

    Hàm

    import cx_Oracle
    
    dsn_tns = cx_Oracle.makedsn('Host Name', 'Port Number', service_name='Service Name') # if needed, place an 'r' before any parameter in order to address special characters such as '\'.
    conn = cx_Oracle.connect(user=r'User Name', password='Personal Password', dsn=dsn_tns) # if needed, place an 'r' before any parameter in order to address special characters such as '\'. For example, if your user name contains '\', you'll need to place 'r' before the user name: user=r'User Name'
    
    c = conn.cursor()
    c.execute('select * from database.table') # use triple quotes if you want to spread your query across multiple lines
    for row in c:
        print (row[0], '-', row[1]) # this only shows the first two columns. To add an additional column you'll need to add , '-', row[2], etc.
    #conn.close()
    
    75 tạo ra một nhóm kết nối Oracle cho người dùng. Các kết nối trong nhóm có thể được sử dụng bởi cx_oracle bằng cách gọi
    import cx_Oracle
    
    dsn_tns = cx_Oracle.makedsn('Host Name', 'Port Number', service_name='Service Name') # if needed, place an 'r' before any parameter in order to address special characters such as '\'.
    conn = cx_Oracle.connect(user=r'User Name', password='Personal Password', dsn=dsn_tns) # if needed, place an 'r' before any parameter in order to address special characters such as '\'. For example, if your user name contains '\', you'll need to place 'r' before the user name: user=r'User Name'
    
    c = conn.cursor()
    c.execute('select * from database.table') # use triple quotes if you want to spread your query across multiple lines
    for row in c:
        print (row[0], '-', row[1]) # this only shows the first two columns. To add an additional column you'll need to add , '-', row[2], etc.
    #conn.close()
    
    76. Kích thước nhóm ban đầu là 2 kết nối. Kích thước tối đa là 5 kết nối. Khi nhóm cần phát triển, thì 1 kết nối mới sẽ được tạo tại một thời điểm. Hồ bơi có thể thu nhỏ lại kích thước tối thiểu 2 khi các kết nối không còn được sử dụng.

    Dòng

    import cx_Oracle
    
    dsn_tns = cx_Oracle.makedsn('Host Name', 'Port Number', service_name='Service Name') # if needed, place an 'r' before any parameter in order to address special characters such as '\'.
    conn = cx_Oracle.connect(user=r'User Name', password='Personal Password', dsn=dsn_tns) # if needed, place an 'r' before any parameter in order to address special characters such as '\'. For example, if your user name contains '\', you'll need to place 'r' before the user name: user=r'User Name'
    
    c = conn.cursor()
    c.execute('select * from database.table') # use triple quotes if you want to spread your query across multiple lines
    for row in c:
        print (row[0], '-', row[1]) # this only shows the first two columns. To add an additional column you'll need to add , '-', row[2], etc.
    #conn.close()
    
    77 tạo ra một phương thức được gọi bởi mỗi luồng.

    Trong phương thức, cuộc gọi

    import cx_Oracle
    
    dsn_tns = cx_Oracle.makedsn('Host Name', 'Port Number', service_name='Service Name') # if needed, place an 'r' before any parameter in order to address special characters such as '\'.
    conn = cx_Oracle.connect(user=r'User Name', password='Personal Password', dsn=dsn_tns) # if needed, place an 'r' before any parameter in order to address special characters such as '\'. For example, if your user name contains '\', you'll need to place 'r' before the user name: user=r'User Name'
    
    c = conn.cursor()
    c.execute('select * from database.table') # use triple quotes if you want to spread your query across multiple lines
    for row in c:
        print (row[0], '-', row[1]) # this only shows the first two columns. To add an additional column you'll need to add , '-', row[2], etc.
    #conn.close()
    
    76 có một kết nối từ nhóm (miễn là ít hơn 5 đã được sử dụng). Kết nối này được sử dụng trong một vòng lặp 4 lần lặp để truy vấn chuỗi
    import cx_Oracle
    
    dsn_tns = cx_Oracle.makedsn('Host Name', 'Port Number', service_name='Service Name') # if needed, place an 'r' before any parameter in order to address special characters such as '\'.
    conn = cx_Oracle.connect(user=r'User Name', password='Personal Password', dsn=dsn_tns) # if needed, place an 'r' before any parameter in order to address special characters such as '\'. For example, if your user name contains '\', you'll need to place 'r' before the user name: user=r'User Name'
    
    c = conn.cursor()
    c.execute('select * from database.table') # use triple quotes if you want to spread your query across multiple lines
    for row in c:
        print (row[0], '-', row[1]) # this only shows the first two columns. To add an additional column you'll need to add , '-', row[2], etc.
    #conn.close()
    
    79. Vào cuối phương thức, CX_Oracle sẽ tự động đóng con trỏ và giải phóng kết nối trở lại nhóm để tái sử dụng.

    Dòng

    import cx_Oracle
    
    dsn_tns = cx_Oracle.makedsn('Host Name', 'Port Number', service_name='Service Name') # if needed, place an 'r' before any parameter in order to address special characters such as '\'.
    conn = cx_Oracle.connect(user=r'User Name', password='Personal Password', dsn=dsn_tns) # if needed, place an 'r' before any parameter in order to address special characters such as '\'. For example, if your user name contains '\', you'll need to place 'r' before the user name: user=r'User Name'
    
    c = conn.cursor()
    c.execute('select * from database.table') # use triple quotes if you want to spread your query across multiple lines
    for row in c:
        print (row[0], '-', row[1]) # this only shows the first two columns. To add an additional column you'll need to add , '-', row[2], etc.
    #conn.close()
    
    80 lấy một hàng và đặt giá trị đơn chứa trong kết quả Tuple vào biến
    import cx_Oracle
    
    dsn_tns = cx_Oracle.makedsn('Host Name', 'Port Number', service_name='Service Name') # if needed, place an 'r' before any parameter in order to address special characters such as '\'.
    conn = cx_Oracle.connect(user=r'User Name', password='Personal Password', dsn=dsn_tns) # if needed, place an 'r' before any parameter in order to address special characters such as '\'. For example, if your user name contains '\', you'll need to place 'r' before the user name: user=r'User Name'
    
    c = conn.cursor()
    c.execute('select * from database.table') # use triple quotes if you want to spread your query across multiple lines
    for row in c:
        print (row[0], '-', row[1]) # this only shows the first two columns. To add an additional column you'll need to add , '-', row[2], etc.
    #conn.close()
    
    81. Nếu không có dấu phẩy, giá trị trong
    import cx_Oracle
    
    dsn_tns = cx_Oracle.makedsn('Host Name', 'Port Number', service_name='Service Name') # if needed, place an 'r' before any parameter in order to address special characters such as '\'.
    conn = cx_Oracle.connect(user=r'User Name', password='Personal Password', dsn=dsn_tns) # if needed, place an 'r' before any parameter in order to address special characters such as '\'. For example, if your user name contains '\', you'll need to place 'r' before the user name: user=r'User Name'
    
    c = conn.cursor()
    c.execute('select * from database.table') # use triple quotes if you want to spread your query across multiple lines
    for row in c:
        print (row[0], '-', row[1]) # this only shows the first two columns. To add an additional column you'll need to add , '-', row[2], etc.
    #conn.close()
    
    81 sẽ là một tuple như "
    import cx_Oracle
    
    dsn_tns = cx_Oracle.makedsn('Host Name', 'Port Number', service_name='Service Name') # if needed, place an 'r' before any parameter in order to address special characters such as '\'.
    conn = cx_Oracle.connect(user=r'User Name', password='Personal Password', dsn=dsn_tns) # if needed, place an 'r' before any parameter in order to address special characters such as '\'. For example, if your user name contains '\', you'll need to place 'r' before the user name: user=r'User Name'
    
    c = conn.cursor()
    c.execute('select * from database.table') # use triple quotes if you want to spread your query across multiple lines
    for row in c:
        print (row[0], '-', row[1]) # this only shows the first two columns. To add an additional column you'll need to add , '-', row[2], etc.
    #conn.close()
    
    83".

    Hai luồng được tạo, mỗi luồng gọi phương thức

    import cx_Oracle
    
    dsn_tns = cx_Oracle.makedsn('Host Name', 'Port Number', service_name='Service Name') # if needed, place an 'r' before any parameter in order to address special characters such as '\'.
    conn = cx_Oracle.connect(user=r'User Name', password='Personal Password', dsn=dsn_tns) # if needed, place an 'r' before any parameter in order to address special characters such as '\'. For example, if your user name contains '\', you'll need to place 'r' before the user name: user=r'User Name'
    
    c = conn.cursor()
    c.execute('select * from database.table') # use triple quotes if you want to spread your query across multiple lines
    for row in c:
        print (row[0], '-', row[1]) # this only shows the first two columns. To add an additional column you'll need to add , '-', row[2], etc.
    #conn.close()
    
    84.

    Trong một thiết bị đầu cuối lệnh, chạy:

    select sys_context('userenv','service_name') from dual
    
    6

    Đầu ra cho thấy kết quả truy vấn xen kẽ khi mỗi luồng tìm nạp các giá trị một cách độc lập. Thứ tự xen kẽ có thể thay đổi từ chạy đến chạy.

  • 2.2 Thử nghiệm nhóm kết nối

    Xem lại

    import cx_Oracle
    
    dsn_tns = cx_Oracle.makedsn('Host Name', 'Port Number', service_name='Service Name') # if needed, place an 'r' before any parameter in order to address special characters such as '\'.
    conn = cx_Oracle.connect(user=r'User Name', password='Personal Password', dsn=dsn_tns) # if needed, place an 'r' before any parameter in order to address special characters such as '\'. For example, if your user name contains '\', you'll need to place 'r' before the user name: user=r'User Name'
    
    c = conn.cursor()
    c.execute('select * from database.table') # use triple quotes if you want to spread your query across multiple lines
    for row in c:
        print (row[0], '-', row[1]) # this only shows the first two columns. To add an additional column you'll need to add , '-', row[2], etc.
    #conn.close()
    
    85, có một vòng lặp cho số lượng luồng, mỗi lần lặp lại gọi phương thức
    import cx_Oracle
    
    dsn_tns = cx_Oracle.makedsn('Host Name', 'Port Number', service_name='Service Name') # if needed, place an 'r' before any parameter in order to address special characters such as '\'.
    conn = cx_Oracle.connect(user=r'User Name', password='Personal Password', dsn=dsn_tns) # if needed, place an 'r' before any parameter in order to address special characters such as '\'. For example, if your user name contains '\', you'll need to place 'r' before the user name: user=r'User Name'
    
    c = conn.cursor()
    c.execute('select * from database.table') # use triple quotes if you want to spread your query across multiple lines
    for row in c:
        print (row[0], '-', row[1]) # this only shows the first two columns. To add an additional column you'll need to add , '-', row[2], etc.
    #conn.close()
    
    84:

    select sys_context('userenv','service_name') from dual
    
    7

    Trong một thiết bị đầu cuối lệnh, chạy:

    select sys_context('userenv','service_name') from dual
    
    8

    Đầu ra cho thấy kết quả truy vấn xen kẽ khi mỗi luồng tìm nạp các giá trị một cách độc lập. Thứ tự xen kẽ có thể thay đổi từ chạy đến chạy.

    2.2 Thử nghiệm nhóm kết nối

    Xem lại

    import cx_Oracle
    
    dsn_tns = cx_Oracle.makedsn('Host Name', 'Port Number', service_name='Service Name') # if needed, place an 'r' before any parameter in order to address special characters such as '\'.
    conn = cx_Oracle.connect(user=r'User Name', password='Personal Password', dsn=dsn_tns) # if needed, place an 'r' before any parameter in order to address special characters such as '\'. For example, if your user name contains '\', you'll need to place 'r' before the user name: user=r'User Name'
    
    c = conn.cursor()
    c.execute('select * from database.table') # use triple quotes if you want to spread your query across multiple lines
    for row in c:
        print (row[0], '-', row[1]) # this only shows the first two columns. To add an additional column you'll need to add , '-', row[2], etc.
    #conn.close()
    
    85, có một vòng lặp cho số lượng luồng, mỗi lần lặp lại gọi phương thức
    import cx_Oracle
    
    dsn_tns = cx_Oracle.makedsn('Host Name', 'Port Number', service_name='Service Name') # if needed, place an 'r' before any parameter in order to address special characters such as '\'.
    conn = cx_Oracle.connect(user=r'User Name', password='Personal Password', dsn=dsn_tns) # if needed, place an 'r' before any parameter in order to address special characters such as '\'. For example, if your user name contains '\', you'll need to place 'r' before the user name: user=r'User Name'
    
    c = conn.cursor()
    c.execute('select * from database.table') # use triple quotes if you want to spread your query across multiple lines
    for row in c:
        print (row[0], '-', row[1]) # this only shows the first two columns. To add an additional column you'll need to add , '-', row[2], etc.
    #conn.close()
    
    84:

  • Thử nghiệm với các giá trị khác nhau của các tham số nhóm và
    import cx_Oracle
    
    dsn_tns = cx_Oracle.makedsn('Host Name', 'Port Number', service_name='Service Name') # if needed, place an 'r' before any parameter in order to address special characters such as '\'.
    conn = cx_Oracle.connect(user=r'User Name', password='Personal Password', dsn=dsn_tns) # if needed, place an 'r' before any parameter in order to address special characters such as '\'. For example, if your user name contains '\', you'll need to place 'r' before the user name: user=r'User Name'
    
    c = conn.cursor()
    c.execute('select * from database.table') # use triple quotes if you want to spread your query across multiple lines
    for row in c:
        print (row[0], '-', row[1]) # this only shows the first two columns. To add an additional column you'll need to add , '-', row[2], etc.
    #conn.close()
    
    87. Kích thước hồ bơi ban đầu lớn hơn sẽ làm cho việc tạo hồ bơi chậm hơn, nhưng các kết nối sẽ có sẵn ngay lập tức khi cần thiết.

    Hãy thử thay đổi

    import cx_Oracle
    
    dsn_tns = cx_Oracle.makedsn('Host Name', 'Port Number', service_name='Service Name') # if needed, place an 'r' before any parameter in order to address special characters such as '\'.
    conn = cx_Oracle.connect(user=r'User Name', password='Personal Password', dsn=dsn_tns) # if needed, place an 'r' before any parameter in order to address special characters such as '\'. For example, if your user name contains '\', you'll need to place 'r' before the user name: user=r'User Name'
    
    c = conn.cursor()
    c.execute('select * from database.table') # use triple quotes if you want to spread your query across multiple lines
    for row in c:
        print (row[0], '-', row[1]) # this only shows the first two columns. To add an additional column you'll need to add , '-', row[2], etc.
    #conn.close()
    
    88 thành
    import cx_Oracle
    
    dsn_tns = cx_Oracle.makedsn('Host Name', 'Port Number', service_name='Service Name') # if needed, place an 'r' before any parameter in order to address special characters such as '\'.
    conn = cx_Oracle.connect(user=r'User Name', password='Personal Password', dsn=dsn_tns) # if needed, place an 'r' before any parameter in order to address special characters such as '\'. For example, if your user name contains '\', you'll need to place 'r' before the user name: user=r'User Name'
    
    c = conn.cursor()
    c.execute('select * from database.table') # use triple quotes if you want to spread your query across multiple lines
    for row in c:
        print (row[0], '-', row[1]) # this only shows the first two columns. To add an additional column you'll need to add , '-', row[2], etc.
    #conn.close()
    
    89. Khi
    import cx_Oracle
    
    dsn_tns = cx_Oracle.makedsn('Host Name', 'Port Number', service_name='Service Name') # if needed, place an 'r' before any parameter in order to address special characters such as '\'.
    conn = cx_Oracle.connect(user=r'User Name', password='Personal Password', dsn=dsn_tns) # if needed, place an 'r' before any parameter in order to address special characters such as '\'. For example, if your user name contains '\', you'll need to place 'r' before the user name: user=r'User Name'
    
    c = conn.cursor()
    c.execute('select * from database.table') # use triple quotes if you want to spread your query across multiple lines
    for row in c:
        print (row[0], '-', row[1]) # this only shows the first two columns. To add an additional column you'll need to add , '-', row[2], etc.
    #conn.close()
    
    87 vượt quá kích thước tối đa của nhóm, cuộc gọi
    import cx_Oracle
    
    dsn_tns = cx_Oracle.makedsn('Host Name', 'Port Number', service_name='Service Name') # if needed, place an 'r' before any parameter in order to address special characters such as '\'.
    conn = cx_Oracle.connect(user=r'User Name', password='Personal Password', dsn=dsn_tns) # if needed, place an 'r' before any parameter in order to address special characters such as '\'. For example, if your user name contains '\', you'll need to place 'r' before the user name: user=r'User Name'
    
    c = conn.cursor()
    c.execute('select * from database.table') # use triple quotes if you want to spread your query across multiple lines
    for row in c:
        print (row[0], '-', row[1]) # this only shows the first two columns. To add an additional column you'll need to add , '-', row[2], etc.
    #conn.close()
    
    91 hiện sẽ tạo ra một lỗi như "ORA-24459: ocisessionget () đã hết thời gian chờ nhóm tạo kết nối mới".

    Các cấu hình nhóm trong đó

    import cx_Oracle
    
    dsn_tns = cx_Oracle.makedsn('Host Name', 'Port Number', service_name='Service Name') # if needed, place an 'r' before any parameter in order to address special characters such as '\'.
    conn = cx_Oracle.connect(user=r'User Name', password='Personal Password', dsn=dsn_tns) # if needed, place an 'r' before any parameter in order to address special characters such as '\'. For example, if your user name contains '\', you'll need to place 'r' before the user name: user=r'User Name'
    
    c = conn.cursor()
    c.execute('select * from database.table') # use triple quotes if you want to spread your query across multiple lines
    for row in c:
        print (row[0], '-', row[1]) # this only shows the first two columns. To add an additional column you'll need to add , '-', row[2], etc.
    #conn.close()
    
    92 giống như
    import cx_Oracle
    
    dsn_tns = cx_Oracle.makedsn('Host Name', 'Port Number', service_name='Service Name') # if needed, place an 'r' before any parameter in order to address special characters such as '\'.
    conn = cx_Oracle.connect(user=r'User Name', password='Personal Password', dsn=dsn_tns) # if needed, place an 'r' before any parameter in order to address special characters such as '\'. For example, if your user name contains '\', you'll need to place 'r' before the user name: user=r'User Name'
    
    c = conn.cursor()
    c.execute('select * from database.table') # use triple quotes if you want to spread your query across multiple lines
    for row in c:
        print (row[0], '-', row[1]) # this only shows the first two columns. To add an additional column you'll need to add , '-', row[2], etc.
    #conn.close()
    
    93 (và
    import cx_Oracle
    
    dsn_tns = cx_Oracle.makedsn('Host Name', 'Port Number', service_name='Service Name') # if needed, place an 'r' before any parameter in order to address special characters such as '\'.
    conn = cx_Oracle.connect(user=r'User Name', password='Personal Password', dsn=dsn_tns) # if needed, place an 'r' before any parameter in order to address special characters such as '\'. For example, if your user name contains '\', you'll need to place 'r' before the user name: user=r'User Name'
    
    c = conn.cursor()
    c.execute('select * from database.table') # use triple quotes if you want to spread your query across multiple lines
    for row in c:
        print (row[0], '-', row[1]) # this only shows the first two columns. To add an additional column you'll need to add , '-', row[2], etc.
    #conn.close()
    
    94) thường được khuyến nghị là một thực tiễn tốt nhất. Điều này tránh các cơn bão kết nối trên máy chủ cơ sở dữ liệu.

    Hướng dẫn cx_oracle python example - ví dụ về python cx_oracle

    2.3 Tạo kết nối DRCP

    Hướng dẫn cx_oracle python example - ví dụ về python cx_oracle

    Tập hợp kết nối thường trú trong cơ sở dữ liệu cho phép nhiều quy trình Python trên nhiều máy để chia sẻ một nhóm nhỏ các quy trình máy chủ cơ sở dữ liệu.

    Bên dưới bên trái là một sơ đồ không có DRCP. Mỗi kết nối độc lập ứng dụng (hoặc kết nối kết nối CX_Oracle) đều có quy trình máy chủ cơ sở dữ liệu riêng. Ứng dụng độc lập

    import cx_Oracle
    
    dsn_tns = cx_Oracle.makedsn('Host Name', 'Port Number', service_name='Service Name') # if needed, place an 'r' before any parameter in order to address special characters such as '\'.
    conn = cx_Oracle.connect(user=r'User Name', password='Personal Password', dsn=dsn_tns) # if needed, place an 'r' before any parameter in order to address special characters such as '\'. For example, if your user name contains '\', you'll need to place 'r' before the user name: user=r'User Name'
    
    c = conn.cursor()
    c.execute('select * from database.table') # use triple quotes if you want to spread your query across multiple lines
    for row in c:
        print (row[0], '-', row[1]) # this only shows the first two columns. To add an additional column you'll need to add , '-', row[2], etc.
    #conn.close()
    
    63 và các cuộc gọi đóng yêu cầu tạo và phá hủy đắt tiền của các quy trình máy chủ cơ sở dữ liệu đó. Nhóm kết nối CX_Oracle giảm các chi phí này bằng cách mở các quy trình máy chủ cơ sở dữ liệu, nhưng mọi nhóm kết nối CX_Oracle sẽ yêu cầu bộ quy trình máy chủ cơ sở dữ liệu của riêng mình, ngay cả khi họ không thực hiện công việc cơ sở dữ liệu: các xử lý máy chủ nhàn rỗi này sẽ tiêu thụ tài nguyên máy chủ cơ sở dữ liệu. Dưới đây là một sơ đồ với DRCP. Các quy trình tập lệnh và python có thể chia sẻ các máy chủ cơ sở dữ liệu từ một nhóm máy chủ được xử lý trước và trả lại chúng khi chúng không được sử dụng.

    Không có DRCP

    Với DRCP

    select sys_context('userenv','service_name') from dual
    
    9

    DRCP rất hữu ích khi máy chủ cơ sở dữ liệu không có đủ bộ nhớ để xử lý số lượng quy trình máy chủ cơ sở dữ liệu cần thiết. Nếu DRCP được bật, nó được sử dụng tốt nhất cùng với gộp kết nối của CX_Oracle. Tuy nhiên, nếu bộ nhớ máy chủ cơ sở dữ liệu đủ lớn, thì mô hình quy trình máy chủ 'chuyên dụng' mặc định thường được khuyến nghị. Điều này có thể có hoặc không có nhóm kết nối CX_Oracle, tùy thuộc vào tốc độ kết nối.

    "Độ tinh khiết" của kết nối được định nghĩa là hằng số

    select sys_context('userenv','service_name') from dual
    
    00, có nghĩa là trạng thái phiên (như định dạng ngày mặc định) có thể được giữ lại giữa các cuộc gọi kết nối, mang lại lợi ích hiệu suất. Thông tin phiên sẽ bị loại bỏ nếu một máy chủ gộp sau đó được sử dụng lại bởi một ứng dụng có tên lớp kết nối khác.

    Các ứng dụng không bao giờ nên chia sẻ thông tin phiên nên sử dụng một lớp kết nối khác và/hoặc sử dụng

    select sys_context('userenv','service_name') from dual
    
    01 để buộc tạo phiên mới. Điều này làm giảm khả năng mở rộng tổng thể nhưng ngăn chặn các ứng dụng sử dụng sai thông tin phiên.

    Chạy

    select sys_context('userenv','service_name') from dual
    
    02in một cửa sổ thiết bị đầu cuối.

    select username from dba_users
    
    0

    Đầu ra chỉ đơn giản là phiên bản của cơ sở dữ liệu.

  • 2.4 Kết nối gộp và DRCP

    DRCP hoạt động tốt với gộp kết nối của CX_oracle.

    Chỉnh sửa

    import cx_Oracle
    
    dsn_tns = cx_Oracle.makedsn('Host Name', 'Port Number', service_name='Service Name') # if needed, place an 'r' before any parameter in order to address special characters such as '\'.
    conn = cx_Oracle.connect(user=r'User Name', password='Personal Password', dsn=dsn_tns) # if needed, place an 'r' before any parameter in order to address special characters such as '\'. For example, if your user name contains '\', you'll need to place 'r' before the user name: user=r'User Name'
    
    c = conn.cursor()
    c.execute('select * from database.table') # use triple quotes if you want to spread your query across multiple lines
    for row in c:
        print (row[0], '-', row[1]) # this only shows the first two columns. To add an additional column you'll need to add , '-', row[2], etc.
    #conn.close()
    
    85, đặt lại mọi tùy chọn nhóm đã thay đổi và sửa đổi nó để sử dụng DRCP:

    select username from dba_users
    
    1

    Logic tập lệnh không cần phải thay đổi để hưởng lợi từ việc kết nối DRCP.

    Chạy tập lệnh:

    select sys_context('userenv','service_name') from dual
    
    8

    Xem lại DRCP_Query.SQL và đặt chuỗi kết nối thành cơ sở dữ liệu của bạn. Sau đó mở một cửa sổ đầu cuối mới và gọi SQL*Plus:drcp_query.sql and set the connection string to your database. Then open a new a terminal window and invoke SQL*Plus:

    select username from dba_users
    
    3

    Điều này sẽ nhắc nhở mật khẩu hệ thống và chuỗi kết nối cơ sở dữ liệu. Với cơ sở dữ liệu có thể cắm, bạn sẽ cần kết nối với cơ sở dữ liệu container. Lưu ý rằng với ADB, chế độ xem này không chứa hàng, do đó, việc chạy tập lệnh này không hữu ích.

    Đối với các cơ sở dữ liệu khác, tập lệnh hiển thị số lượng yêu cầu kết nối được thực hiện cho nhóm kể từ khi cơ sở dữ liệu được bắt đầu ("num_requests"), có bao nhiêu trong số đó đã sử dụng lại phiên của máy chủ ("num_hits") và có bao nhiêu người phải tạo phiên mới ("Num_misses"). Thông thường mục tiêu là một số lần bỏ lỡ thấp.

    Để xem cấu hình nhóm, bạn có thể truy vấn dba_cpool_info.

  • 2.5 Điều tra DRCP khác

    Để khám phá các hành vi của kết nối cx_oracle gộp và gộp DRCP, bạn có thể thử thay đổi độ tinh khiết thành

    select sys_context('userenv','service_name') from dual
    
    04 để xem hiệu ứng trên thống kê DRCP num_misses.

    Một trải nghiệm khác là bao gồm mô -đun

    select sys_context('userenv','service_name') from dual
    
    05 ở đầu tệp:

    select username from dba_users
    
    4

    và thêm các cuộc gọi vào

    select sys_context('userenv','service_name') from dual
    
    06 trong mã, ví dụ trong vòng lặp truy vấn. Sau đó nhìn vào cách các chủ đề thực thi. Sử dụng
    select sys_context('userenv','service_name') from dual
    
    07 để theo dõi hành vi của nhóm.

3. Tìm nạp dữ liệu

Thực hiện các truy vấn chọn là cách chính để lấy dữ liệu từ cơ sở dữ liệu Oracle. Liên kết tài liệu để đọc thêm: Truy vấn SQL.

  • 3.1 Một truy vấn đơn giản

    Có một số chức năng bạn có thể sử dụng để truy vấn cơ sở dữ liệu Oracle, nhưng những điều cơ bản của truy vấn luôn giống nhau:

    1. Thực hiện câu lệnh. 2. Liên kết các giá trị dữ liệu (tùy chọn). 3. Tìm nạp các kết quả từ cơ sở dữ liệu.
    2. Bind data values (optional).
    3. Fetch the results from the database.

    Xem lại mã có trong

    select sys_context('userenv','service_name') from dual
    
    08:

    select username from dba_users
    
    5

    Phương pháp

    select sys_context('userenv','service_name') from dual
    
    09 mở một con trỏ cho các câu lệnh sử dụng.

    Phương thức

    select sys_context('userenv','service_name') from dual
    
    10 phân tích cú pháp và thực thi câu lệnh.

    Vòng lặp lấy từng hàng từ con trỏ và giải nén bộ được trả lại vào các biến

    select sys_context('userenv','service_name') from dual
    
    11,
    select sys_context('userenv','service_name') from dual
    
    12,
    select sys_context('userenv','service_name') from dual
    
    13, sau đó được in.

    Chạy tập lệnh trong cửa sổ Terminal:

    select username from dba_users
    
    6

    Đầu ra là:

    select username from dba_users
    
    7
  • 3.2 Sử dụng Fetchone ()

    Khi số lượng hàng lớn, cuộc gọi

    select sys_context('userenv','service_name') from dual
    
    14 có thể sử dụng quá nhiều bộ nhớ.

    Xem lại mã có trong

    select sys_context('userenv','service_name') from dual
    
    15:

    select username from dba_users
    
    8

    Điều này sử dụng phương thức

    select sys_context('userenv','service_name') from dual
    
    16 để trả về chỉ một hàng như một bộ. Khi được gọi là nhiều lần, các hàng liên tiếp được trả lại:

    Chạy tập lệnh trong cửa sổ Terminal:

    select username from dba_users
    
    9

    Đầu ra là:

  • select username from dba_users
    
    7

    3.2 Sử dụng Fetchone ()

    Khi số lượng hàng lớn, cuộc gọi
    select sys_context('userenv','service_name') from dual
    
    14 có thể sử dụng quá nhiều bộ nhớ.

    Xem lại mã có trong

    select sys_context('userenv','service_name') from dual
    
    15:

    Chạy tập lệnh trong cửa sổ Terminal:

    cd samples/tutorial/sql
    sqlplus -l system/systempassword@localhost/orclpdb1 @create_user
    
    1

    Đầu ra là:

    select username from dba_users
    
    7

    3.2 Sử dụng Fetchone ()
  • Khi số lượng hàng lớn, cuộc gọi
    select sys_context('userenv','service_name') from dual
    
    14 có thể sử dụng quá nhiều bộ nhớ.

    Xem lại mã có trong

    select sys_context('userenv','service_name') from dual
    
    15:

    Điều này sử dụng phương thức

    select sys_context('userenv','service_name') from dual
    
    16 để trả về chỉ một hàng như một bộ. Khi được gọi là nhiều lần, các hàng liên tiếp được trả lại:

    cd samples/tutorial/sql
    sqlplus -l system/systempassword@localhost/orclpdb1 @create_user
    
    3

    Chạy tập lệnh trong cửa sổ Terminal:

    cd samples/tutorial/sql
    sqlplus -l system/systempassword@localhost/orclpdb1 @create_user
    
    4

    Đầu ra là:

    cd samples/tutorial/sql
    sqlplus -l system/systempassword@localhost/orclpdb1 @create_user
    
    5

    select username from dba_users
    
    7

  • 3.2 Sử dụng Fetchone ()

    Khi số lượng hàng lớn, cuộc gọi

    select sys_context('userenv','service_name') from dual
    
    14 có thể sử dụng quá nhiều bộ nhớ.

    Xem lại mã có trong

    select sys_context('userenv','service_name') from dual
    
    15:

    Đầu tiên, tạo một bảng với một số lượng lớn các hàng. Xem lại

    select sys_context('userenv','service_name') from dual
    
    23:

    cd samples/tutorial/sql
    sqlplus -l system/systempassword@localhost/orclpdb1 @create_user
    
    6

    Trong cửa sổ thiết bị đầu cuối chạy tập lệnh như:

    cd samples/tutorial/sql
    sqlplus -l system/systempassword@localhost/orclpdb1 @create_user
    
    7

    Xem lại mã có trong

    select sys_context('userenv','service_name') from dual
    
    24:

    cd samples/tutorial/sql
    sqlplus -l system/systempassword@localhost/orclpdb1 @create_user
    
    8

    Điều này sử dụng mô -đun 'Thời gian' để đo thời gian trôi qua của truy vấn. Các giá trị prefetchrows và mảng là 100. Điều này gây ra các lô 100 bản ghi tại một thời điểm được trả lại từ cơ sở dữ liệu cho bộ đệm trong Python. Các giá trị này có thể được điều chỉnh để giảm số lượng "chuyến đi khứ hồi" được thực hiện cho cơ sở dữ liệu, thường giảm tải mạng và giảm số lượng công tắc ngữ cảnh trên máy chủ cơ sở dữ liệu. Các phương thức

    select sys_context('userenv','service_name') from dual
    
    16,
    select sys_context('userenv','service_name') from dual
    
    18 và
    select sys_context('userenv','service_name') from dual
    
    14 sẽ được đọc từ bộ đệm trước khi yêu cầu thêm dữ liệu từ cơ sở dữ liệu.

    Trong cửa sổ thiết bị đầu cuối, chạy:

    cd samples/tutorial/sql
    sqlplus -l system/systempassword@localhost/orclpdb1 @create_user
    
    9

    Chạy lại một vài lần để xem thời gian trung bình.

    Thử nghiệm với các giá trị prefetchrows và mảng khác nhau. Ví dụ: Chỉnh sửa

    select sys_context('userenv','service_name') from dual
    
    24 và thay đổi mảng thành:

    sqlplus -l pythonhol/welcome@localhost/orclpdb1 @setup_tables
    
    0

    Chạy lại tập lệnh để so sánh hiệu suất của các cài đặt mảng khác nhau.

    Nói chung, kích thước mảng lớn hơn cải thiện hiệu suất. Tùy thuộc vào mức độ nhanh chóng của hệ thống của bạn, bạn có thể cần sử dụng các giá trị khác nhau so với các giá trị được đưa ra ở đây để thấy sự khác biệt về thời gian có ý nghĩa.

    Có một sự đánh đổi thời gian/không gian để tăng các giá trị. Các giá trị lớn hơn sẽ yêu cầu nhiều bộ nhớ hơn trong Python để đệm các bản ghi.

    Nếu bạn biết truy vấn trả về một số lượng hàng cố định, ví dụ 20 hàng, thì hãy đặt Arraysize thành 20 và Prefetchrows thành 21. Việc bổ sung một cho Prefetchrows sẽ ngăn chặn một chuyến đi khứ hồi để kiểm tra cuối cùng. Việc thực thi tuyên bố và tìm nạp sẽ mất tổng cộng một chuyến đi. Điều này giảm thiểu tải trên cơ sở dữ liệu.

    Nếu bạn biết một truy vấn chỉ trả về một vài bản ghi, hãy giảm Arraysize từ mặc định để giảm việc sử dụng bộ nhớ.

4. Dữ liệu ràng buộc

Các biến liên kết cho phép bạn thực hiện lại các câu lệnh với các giá trị dữ liệu mới mà không có chi phí phân loại lại câu lệnh. Binding cải thiện khả năng tái sử dụng mã, cải thiện khả năng mở rộng ứng dụng và có thể giảm nguy cơ tấn công tiêm SQL. Sử dụng các biến liên kết được khuyến nghị mạnh mẽ. Liên kết tài liệu để đọc thêm: Sử dụng các biến liên kết.

  • 4.1 Binding trong các truy vấn

    Xem lại mã có trong

    select sys_context('userenv','service_name') from dual
    
    29:

    sqlplus -l pythonhol/welcome@localhost/orclpdb1 @setup_tables
    
    1

    Tuyên bố chứa một biến liên kết "

    select sys_context('userenv','service_name') from dual
    
    30" trình giữ chỗ. Câu lệnh được thực thi hai lần với các giá trị khác nhau cho mệnh đề
    select sys_context('userenv','service_name') from dual
    
    31.

    Từ một cửa sổ đầu cuối, chạy:

    sqlplus -l pythonhol/welcome@localhost/orclpdb1 @setup_tables
    
    2

    Đầu ra cho thấy các chi tiết cho hai bộ phận.

    Một số lượng tùy ý các đối số được đặt tên có thể được sử dụng trong cuộc gọi

    select sys_context('userenv','service_name') from dual
    
    10. Mỗi tên đối số phải khớp với một tên biến liên kết. Ngoài ra, thay vì chuyển nhiều đối số, bạn có thể chuyển một đối số thứ hai cho
    select sys_context('userenv','service_name') from dual
    
    10 đó là một chuỗi hoặc từ điển. Ví dụ sau cho thấy các cú pháp này.

    Để liên kết NULL cơ sở dữ liệu, hãy sử dụng giá trị Python

    import cx_Oracle
    
    dsn_tns = cx_Oracle.makedsn('Host Name', 'Port Number', service_name='Service Name') # if needed, place an 'r' before any parameter in order to address special characters such as '\'.
    conn = cx_Oracle.connect(user=r'User Name', password='Personal Password', dsn=dsn_tns) # if needed, place an 'r' before any parameter in order to address special characters such as '\'. For example, if your user name contains '\', you'll need to place 'r' before the user name: user=r'User Name'
    
    c = conn.cursor()
    c.execute('select * from database.table') # use triple quotes if you want to spread your query across multiple lines
    for row in c:
        print (row[0], '-', row[1]) # this only shows the first two columns. To add an additional column you'll need to add , '-', row[2], etc.
    #conn.close()
    
    56

    CX_ORACLE sử dụng bộ đệm câu lệnh của cơ sở dữ liệu Oracle. Miễn là câu lệnh bạn chuyển sang

    select sys_context('userenv','service_name') from dual
    
    10 còn trong bộ đệm đó, bạn có thể sử dụng các giá trị liên kết khác nhau và vẫn tránh phân tích câu lệnh đầy đủ. Kích thước bộ đệm câu lệnh có thể định cấu hình cho mỗi kết nối. Để xem kích thước bộ đệm câu lệnh mặc định, chỉnh sửa
    select sys_context('userenv','service_name') from dual
    
    29 và thêm một dòng ở cuối:

    sqlplus -l pythonhol/welcome@localhost/orclpdb1 @setup_tables
    
    3

    Chạy lại tập tin.

    Trong các ứng dụng của bạn, bạn sẽ đặt kích thước bộ đệm câu lệnh cho số lượng câu lệnh duy nhất thường được thực thi.

  • 4.2 Binding trong chèn

    Xem lại mã trong

    select sys_context('userenv','service_name') from dual
    
    37 Tạo bảng để chèn dữ liệu:

    sqlplus -l pythonhol/welcome@localhost/orclpdb1 @setup_tables
    
    4

    Chạy tập lệnh như:

    sqlplus -l pythonhol/welcome@localhost/orclpdb1 @setup_tables
    
    5

    Xem lại mã có trong

    select sys_context('userenv','service_name') from dual
    
    38:

    sqlplus -l pythonhol/welcome@localhost/orclpdb1 @setup_tables
    
    6

    Mảng '

    select sys_context('userenv','service_name') from dual
    
    39' chứa dữ liệu sẽ được chèn.

    Cuộc gọi

    select sys_context('userenv','service_name') from dual
    
    40 chèn tất cả các hàng. Cuộc gọi này sử dụng "ràng buộc mảng", đây là một cách hiệu quả để chèn nhiều bản ghi.

    Phần cuối cùng của tập lệnh truy vấn kết quả trở lại và hiển thị chúng dưới dạng danh sách các bộ dữ liệu.

    Từ một cửa sổ đầu cuối, chạy:

    sqlplus -l pythonhol/welcome@localhost/orclpdb1 @setup_tables
    
    7

    sqlplus -l pythonhol/welcome@localhost/orclpdb1 @setup_tables
    
    2

  • Đầu ra cho thấy các chi tiết cho hai bộ phận.

    Một số lượng tùy ý các đối số được đặt tên có thể được sử dụng trong cuộc gọi

    select sys_context('userenv','service_name') from dual
    
    10. Mỗi tên đối số phải khớp với một tên biến liên kết. Ngoài ra, thay vì chuyển nhiều đối số, bạn có thể chuyển một đối số thứ hai cho
    select sys_context('userenv','service_name') from dual
    
    10 đó là một chuỗi hoặc từ điển. Ví dụ sau cho thấy các cú pháp này.

    Để liên kết NULL cơ sở dữ liệu, hãy sử dụng giá trị Python

    import cx_Oracle
    
    dsn_tns = cx_Oracle.makedsn('Host Name', 'Port Number', service_name='Service Name') # if needed, place an 'r' before any parameter in order to address special characters such as '\'.
    conn = cx_Oracle.connect(user=r'User Name', password='Personal Password', dsn=dsn_tns) # if needed, place an 'r' before any parameter in order to address special characters such as '\'. For example, if your user name contains '\', you'll need to place 'r' before the user name: user=r'User Name'
    
    c = conn.cursor()
    c.execute('select * from database.table') # use triple quotes if you want to spread your query across multiple lines
    for row in c:
        print (row[0], '-', row[1]) # this only shows the first two columns. To add an additional column you'll need to add , '-', row[2], etc.
    #conn.close()
    
    56

    sqlplus -l pythonhol/welcome@localhost/orclpdb1 @setup_tables
    
    8

    Từ một cửa sổ đầu cuối, chạy:

    sqlplus -l pythonhol/welcome@localhost/orclpdb1 @setup_tables
    
    7

    sqlplus -l pythonhol/welcome@localhost/orclpdb1 @setup_tables
    
    2

    Đầu ra cho thấy các chi tiết cho hai bộ phận.

    sqlplus -l sys/syspassword@localhost/orclcdb as sysdba
    
    0

    Một số lượng tùy ý các đối số được đặt tên có thể được sử dụng trong cuộc gọi

    select sys_context('userenv','service_name') from dual
    
    10. Mỗi tên đối số phải khớp với một tên biến liên kết. Ngoài ra, thay vì chuyển nhiều đối số, bạn có thể chuyển một đối số thứ hai cho
    select sys_context('userenv','service_name') from dual
    
    10 đó là một chuỗi hoặc từ điển. Ví dụ sau cho thấy các cú pháp này.

    sqlplus -l pythonhol/welcome@localhost/orclpdb1 @setup_tables
    
    7

    Để liên kết NULL cơ sở dữ liệu, hãy sử dụng giá trị Python

    import cx_Oracle
    
    dsn_tns = cx_Oracle.makedsn('Host Name', 'Port Number', service_name='Service Name') # if needed, place an 'r' before any parameter in order to address special characters such as '\'.
    conn = cx_Oracle.connect(user=r'User Name', password='Personal Password', dsn=dsn_tns) # if needed, place an 'r' before any parameter in order to address special characters such as '\'. For example, if your user name contains '\', you'll need to place 'r' before the user name: user=r'User Name'
    
    c = conn.cursor()
    c.execute('select * from database.table') # use triple quotes if you want to spread your query across multiple lines
    for row in c:
        print (row[0], '-', row[1]) # this only shows the first two columns. To add an additional column you'll need to add , '-', row[2], etc.
    #conn.close()
    
    56

    Các dữ liệu khác được chèn và được truy vấn lại.

    Vào cuối tập lệnh, cx_oracle sẽ quay lại một giao dịch không được cam kết. Nếu bạn muốn cam kết kết quả, bạn có thể sử dụng:

    sqlplus -l sys/syspassword@localhost/orclcdb as sysdba
    
    2

    Để buộc cx_oracle quay trở lại, sử dụng:

    sqlplus -l sys/syspassword@localhost/orclcdb as sysdba
    
    3
  • 4.4 BINDING Các đối tượng được đặt tên

    CX_ORACLE có thể tìm nạp và liên kết các loại đối tượng được đặt tên như các đối tượng dữ liệu không gian của Oracle (SDO).

    Trong cửa sổ đầu cuối, hãy bắt đầu SQL*Plus bằng thông tin đăng nhập và chuỗi kết nối trong phòng thí nghiệm, chẳng hạn như:

    sqlplus -l sys/syspassword@localhost/orclcdb as sysdba
    
    4

    Sử dụng lệnh mô tả SQL*Plus để xem định nghĩa SDO:

    sqlplus -l sys/syspassword@localhost/orclcdb as sysdba
    
    5

    Nó chứa các thuộc tính và phương thức khác nhau. Mô tả cấp cao nhất là:

    sqlplus -l sys/syspassword@localhost/orclcdb as sysdba
    
    6

    Xem lại mã có trong

    select sys_context('userenv','service_name') from dual
    
    43:

    sqlplus -l sys/syspassword@localhost/orclcdb as sysdba
    
    7

    Điều này sử dụng

    select sys_context('userenv','service_name') from dual
    
    44 để lấy các loại cơ sở dữ liệu của SDO và các thuộc tính đối tượng của nó. Các cuộc gọi
    select sys_context('userenv','service_name') from dual
    
    45 tạo ra các biểu diễn python của các đối tượng đó. Đối tượng Python Atributes sau đó được đặt. Các loại Varray Oracle như SDO_ELEM_INFO_ARRAY được đặt với
    select sys_context('userenv','service_name') from dual
    
    46.

    Chạy tệp:

    sqlplus -l sys/syspassword@localhost/orclcdb as sysdba
    
    8

    SDO mới được hiển thị dưới dạng đối tượng, tương tự như:

    sqlplus -l sys/syspassword@localhost/orclcdb as sysdba
    
    9

    Để hiển thị các giá trị thuộc tính, chỉnh sửa phần mã truy vấn ở cuối tệp. Thêm một phương thức mới đi qua đối tượng. Tệp bên dưới nhận xét hiện có "

    select sys_context('userenv','service_name') from dual
    
    47") sẽ trông giống như:

    execute dbms_connection_pool.start_pool()
    
    0

    Chạy lại tệp:

    sqlplus -l sys/syspassword@localhost/orclcdb as sysdba
    
    8

    Màn trình diễn này

    execute dbms_connection_pool.start_pool()
    
    2

    Để khám phá thêm, hãy thử đặt thuộc tính SDO SDO_Point, loại SDO_Point_Type loại SDO.

    Các phương pháp

    select sys_context('userenv','service_name') from dual
    
    44 và
    select sys_context('userenv','service_name') from dual
    
    45 cũng có thể được sử dụng để liên kết các bản ghi và bộ sưu tập PL/SQL.

    Trước khi quyết định sử dụng các đối tượng, hãy xem lại các mục tiêu hiệu suất của bạn vì làm việc với các giá trị vô hướng có thể nhanh hơn.

5. PL/SQL

PL/SQL là mở rộng ngôn ngữ thủ tục của Oracle cho SQL. Các quy trình và chức năng PL/SQL được lưu trữ và chạy trong cơ sở dữ liệu. Sử dụng PL/SQL cho phép tất cả các ứng dụng cơ sở dữ liệu tái sử dụng logic, bất kể ứng dụng truy cập cơ sở dữ liệu như thế nào. Nhiều hoạt động liên quan đến dữ liệu có thể được thực hiện trong PL/SQL nhanh hơn so với trích xuất dữ liệu vào một chương trình (ví dụ: Python) và sau đó xử lý nó. Liên kết tài liệu để đọc thêm: Thực hiện PL/SQL.

  • 5.1 Chức năng PL/SQL

    Xem lại

    select sys_context('userenv','service_name') from dual
    
    50 tạo chức năng được lưu trữ PL/SQL
    select sys_context('userenv','service_name') from dual
    
    51 để chèn một hàng vào một bảng mới có tên PTAB và trả về gấp đôi giá trị được chèn:ptab and return double the inserted value:

    execute dbms_connection_pool.start_pool()
    
    3

    Chạy tập lệnh bằng cách sử dụng:

    execute dbms_connection_pool.start_pool()
    
    4

    Xem lại mã có trong

    select sys_context('userenv','service_name') from dual
    
    52:

    execute dbms_connection_pool.start_pool()
    
    5

    Điều này sử dụng

    select sys_context('userenv','service_name') from dual
    
    53 để thực thi chức năng. Tham số thứ hai là loại giá trị trả về. Nó phải là một trong các loại được hỗ trợ bởi cx_oracle hoặc một trong các hằng số loại được xác định bởi cx_oracle (như cx_oracle.number). Hai tham số hàm PL/SQL được truyền dưới dạng tuple, liên kết chúng với các đối số tham số hàm.

    Từ một cửa sổ đầu cuối, chạy:

    execute dbms_connection_pool.start_pool()
    
    6

    Đầu ra là kết quả của tính toán chức năng PL/SQL.

  • 5.2 Quy trình PL/SQL

    Xem lại

    select sys_context('userenv','service_name') from dual
    
    54 tạo ra thủ tục PL/SQL
    select sys_context('userenv','service_name') from dual
    
    55 để chấp nhận hai tham số. Tham số thứ hai chứa một giá trị trả về ra.

    execute dbms_connection_pool.start_pool()
    
    7

    Chạy tập lệnh với:

    execute dbms_connection_pool.start_pool()
    
    8

    Xem lại mã có trong

    select sys_context('userenv','service_name') from dual
    
    56:

    execute dbms_connection_pool.start_pool()
    
    9

    Điều này tạo ra một biến số nguyên

    select sys_context('userenv','service_name') from dual
    
    57 để giữ giá trị được trả về bởi tham số PL/SQL ra. Số đầu vào 123 và tên biến đầu ra được liên kết với các tham số cuộc gọi thủ tục bằng cách sử dụng một tuple.

    Để gọi thủ tục PL/SQL, phương thức

    select sys_context('userenv','service_name') from dual
    
    58 được sử dụng.

    Trong cửa sổ thiết bị đầu cuối, chạy:

    user = os.environ.get("PYTHON_USER", "pythonhol")
    
    dsn = os.environ.get("PYTHON_CONNECT_STRING", "localhost/orclpdb1")
    
    pw = os.environ.get("PYTHON_PASSWORD")
    if pw is None:
        pw = getpass.getpass("Enter password for %s: " % user)
    
    0

    Phương thức

    select sys_context('userenv','service_name') from dual
    
    59 hiển thị giá trị trả về.

6. Loại xử lý

Loại xử lý cho phép các ứng dụng thay đổi dữ liệu được tìm nạp từ hoặc được gửi đến cơ sở dữ liệu. Liên kết tài liệu để đọc thêm: Thay đổi các loại dữ liệu được tìm nạp bằng trình xử lý loại đầu ra và thay đổi các loại dữ liệu liên kết bằng trình xử lý loại đầu vào.

  • 6.1 Trình xử lý loại đầu ra cơ bản

    Trình xử lý loại đầu ra cho phép các ứng dụng thay đổi cách dữ liệu được tìm nạp từ cơ sở dữ liệu. Ví dụ, các số có thể được trả về dưới dạng chuỗi hoặc các đối tượng thập phân. LOB có thể được trả về dưới dạng chuỗi hoặc byte.

    Trình xử lý loại được bật bằng cách đặt thuộc tính

    select sys_context('userenv','service_name') from dual
    
    60 trên con trỏ hoặc kết nối. Nếu đặt trên con trỏ, nó chỉ ảnh hưởng đến các truy vấn được thực hiện bởi con trỏ đó. Nếu được đặt trên kết nối, nó ảnh hưởng đến tất cả các truy vấn được thực thi trên con trỏ được tạo bởi kết nối đó.

    Xem lại mã có trong

    select sys_context('userenv','service_name') from dual
    
    61:

    user = os.environ.get("PYTHON_USER", "pythonhol")
    
    dsn = os.environ.get("PYTHON_CONNECT_STRING", "localhost/orclpdb1")
    
    pw = os.environ.get("PYTHON_PASSWORD")
    if pw is None:
        pw = getpass.getpass("Enter password for %s: " % user)
    
    1

    Trong cửa sổ thiết bị đầu cuối, chạy:

    user = os.environ.get("PYTHON_USER", "pythonhol")
    
    dsn = os.environ.get("PYTHON_CONNECT_STRING", "localhost/orclpdb1")
    
    pw = os.environ.get("PYTHON_PASSWORD")
    if pw is None:
        pw = getpass.getpass("Enter password for %s: " % user)
    
    2

    Phương thức

    select sys_context('userenv','service_name') from dual
    
    59 hiển thị giá trị trả về.

    6. Loại xử lý

    user = os.environ.get("PYTHON_USER", "pythonhol")
    
    dsn = os.environ.get("PYTHON_CONNECT_STRING", "localhost/orclpdb1")
    
    pw = os.environ.get("PYTHON_PASSWORD")
    if pw is None:
        pw = getpass.getpass("Enter password for %s: " % user)
    
    3

    Loại xử lý cho phép các ứng dụng thay đổi dữ liệu được tìm nạp từ hoặc được gửi đến cơ sở dữ liệu. Liên kết tài liệu để đọc thêm: Thay đổi các loại dữ liệu được tìm nạp bằng trình xử lý loại đầu ra và thay đổi các loại dữ liệu liên kết bằng trình xử lý loại đầu vào.

    6.1 Trình xử lý loại đầu ra cơ bản

    user = os.environ.get("PYTHON_USER", "pythonhol")
    
    dsn = os.environ.get("PYTHON_CONNECT_STRING", "localhost/orclpdb1")
    
    pw = os.environ.get("PYTHON_PASSWORD")
    if pw is None:
        pw = getpass.getpass("Enter password for %s: " % user)
    
    2

    Trình xử lý loại đầu ra cho phép các ứng dụng thay đổi cách dữ liệu được tìm nạp từ cơ sở dữ liệu. Ví dụ, các số có thể được trả về dưới dạng chuỗi hoặc các đối tượng thập phân. LOB có thể được trả về dưới dạng chuỗi hoặc byte.

  • Trình xử lý loại được bật bằng cách đặt thuộc tính
    select sys_context('userenv','service_name') from dual
    
    60 trên con trỏ hoặc kết nối. Nếu đặt trên con trỏ, nó chỉ ảnh hưởng đến các truy vấn được thực hiện bởi con trỏ đó. Nếu được đặt trên kết nối, nó ảnh hưởng đến tất cả các truy vấn được thực thi trên con trỏ được tạo bởi kết nối đó.

    Khi các số được tìm nạp từ cơ sở dữ liệu, việc chuyển đổi từ đại diện thập phân của Oracle sang định dạng nhị phân của Python có thể cần xử lý cẩn thận. Để tránh các vấn đề bất ngờ, khuyến nghị chung là thực hiện các hoạt động số trong SQL hoặc PL/SQL hoặc sử dụng mô -đun thập phân trong Python.

    Trình xử lý loại đầu ra có thể được kết hợp với các bộ chuyển đổi biến để thay đổi cách dữ liệu được tìm nạp.

    Đánh giá

    select sys_context('userenv','service_name') from dual
    
    64:

    user = os.environ.get("PYTHON_USER", "pythonhol")
    
    dsn = os.environ.get("PYTHON_CONNECT_STRING", "localhost/orclpdb1")
    
    pw = os.environ.get("PYTHON_PASSWORD")
    if pw is None:
        pw = getpass.getpass("Enter password for %s: " % user)
    
    5

    Chạy tệp:

    user = os.environ.get("PYTHON_USER", "pythonhol")
    
    dsn = os.environ.get("PYTHON_CONNECT_STRING", "localhost/orclpdb1")
    
    pw = os.environ.get("PYTHON_PASSWORD")
    if pw is None:
        pw = getpass.getpass("Enter password for %s: " % user)
    
    6

    Đầu ra giống như:

    user = os.environ.get("PYTHON_USER", "pythonhol")
    
    dsn = os.environ.get("PYTHON_CONNECT_STRING", "localhost/orclpdb1")
    
    pw = os.environ.get("PYTHON_PASSWORD")
    if pw is None:
        pw = getpass.getpass("Enter password for %s: " % user)
    
    7

    Chỉnh sửa tệp và thêm trình xử lý loại sử dụng bộ chuyển đổi thập phân Python:

    user = os.environ.get("PYTHON_USER", "pythonhol")
    
    dsn = os.environ.get("PYTHON_CONNECT_STRING", "localhost/orclpdb1")
    
    pw = os.environ.get("PYTHON_PASSWORD")
    if pw is None:
        pw = getpass.getpass("Enter password for %s: " % user)
    
    8

    Bộ chuyển đổi Python

    select sys_context('userenv','service_name') from dual
    
    65 được gọi với biểu diễn chuỗi của số Oracle. Đầu ra từ
    select sys_context('userenv','service_name') from dual
    
    65 được trả về trong bộ tuple đầu ra.

    Chạy lại tệp:

    user = os.environ.get("PYTHON_USER", "pythonhol")
    
    dsn = os.environ.get("PYTHON_CONNECT_STRING", "localhost/orclpdb1")
    
    pw = os.environ.get("PYTHON_PASSWORD")
    if pw is None:
        pw = getpass.getpass("Enter password for %s: " % user)
    
    6

    Đầu ra giống như:

    -- Default database username
    def user = "pythonhol"
    
    -- Default database connection string
    def connect_string = "localhost/orclpdb1"
    
    -- Prompt for the password
    accept pw char prompt 'Enter database password for &user: ' hide
    
    0

    Mặc dù mã thể hiện việc sử dụng OutConverter, nhưng trong trường hợp cụ thể này, biến có thể được tạo đơn giản bằng cách sử dụng mã sau để thay thế chức năng đầu ra được xác định ở trên:

    -- Default database username
    def user = "pythonhol"
    
    -- Default database connection string
    def connect_string = "localhost/orclpdb1"
    
    -- Prompt for the password
    accept pw char prompt 'Enter database password for &user: ' hide
    
    1
  • 6.3 Trình xử lý loại đầu vào

    Trình xử lý loại đầu vào cho phép các ứng dụng thay đổi cách dữ liệu bị ràng buộc với các câu lệnh hoặc để cho phép các loại mới bị ràng buộc trực tiếp mà không cần phải chuyển đổi riêng lẻ.

    Xem lại

    select sys_context('userenv','service_name') from dual
    
    67, tương tự như
    select sys_context('userenv','service_name') from dual
    
    43 cuối cùng từ phần 4.4, với việc bổ sung một lớp và bộ chuyển đổi mới (được hiển thị in đậm):

    -- Default database username
    def user = "pythonhol"
    
    -- Default database connection string
    def connect_string = "localhost/orclpdb1"
    
    -- Prompt for the password
    accept pw char prompt 'Enter database password for &user: ' hide
    
    2

    Trong tệp mới, lớp Python

    select sys_context('userenv','service_name') from dual
    
    69 được xác định, có các thuộc tính tương ứng với mỗi thuộc tính Oracle MDSYS.SDO_Geometry. Lớp
    select sys_context('userenv','service_name') from dual
    
    69 được sử dụng thấp hơn trong mã để tạo phiên bản Python:

    -- Default database username
    def user = "pythonhol"
    
    -- Default database connection string
    def connect_string = "localhost/orclpdb1"
    
    -- Prompt for the password
    accept pw char prompt 'Enter database password for &user: ' hide
    
    3

    sau đó được liên kết trực tiếp vào câu lệnh chèn như:

    -- Default database username
    def user = "pythonhol"
    
    -- Default database connection string
    def connect_string = "localhost/orclpdb1"
    
    -- Prompt for the password
    accept pw char prompt 'Enter database password for &user: ' hide
    
    4

    Ánh xạ giữa các đối tượng Python và Oracle được xử lý trong

    select sys_context('userenv','service_name') from dual
    
    71 sử dụng các phương thức cx_oracle
    select sys_context('userenv','service_name') from dual
    
    45 và
    select sys_context('userenv','service_name') from dual
    
    46 để tạo một đối tượng Oracle từ các giá trị đối tượng Python. Phương pháp
    select sys_context('userenv','service_name') from dual
    
    71 được gọi bằng trình xử lý loại đầu vào
    select sys_context('userenv','service_name') from dual
    
    75 Bất cứ khi nào một thể hiện
    select sys_context('userenv','service_name') from dual
    
    69 được chèn vào con trỏ.

    Để xác nhận hành vi, hãy chạy tệp:

    -- Default database username
    def user = "pythonhol"
    
    -- Default database connection string
    def connect_string = "localhost/orclpdb1"
    
    -- Prompt for the password
    accept pw char prompt 'Enter database password for &user: ' hide
    
    5

7. LOB

Cơ sở dữ liệu Oracle "LOB" Các đối tượng dài có thể được truyền phát bằng cách sử dụng trình định vị LOB hoặc làm việc trực tiếp dưới dạng chuỗi hoặc byte. Liên kết tài liệu để đọc thêm: Sử dụng dữ liệu Clob và Blob.

  • 7.1 Tìm nạp CLOB bằng cách sử dụng bộ định vị

    Xem lại mã có trong

    select sys_context('userenv','service_name') from dual
    
    77:

    -- Default database username
    def user = "pythonhol"
    
    -- Default database connection string
    def connect_string = "localhost/orclpdb1"
    
    -- Prompt for the password
    accept pw char prompt 'Enter database password for &user: ' hide
    
    6

    Điều này chèn một số dữ liệu chuỗi kiểm tra và sau đó tìm nạp một bản ghi vào

    select sys_context('userenv','service_name') from dual
    
    78, đây là đối tượng LOB ký tự CX_Oracle. Các phương pháp trên LOB bao gồm
    select sys_context('userenv','service_name') from dual
    
    79 và
    select sys_context('userenv','service_name') from dual
    
    80.

    Để xem đầu ra, hãy chạy tệp:

    -- Default database username
    def user = "pythonhol"
    
    -- Default database connection string
    def connect_string = "localhost/orclpdb1"
    
    -- Prompt for the password
    accept pw char prompt 'Enter database password for &user: ' hide
    
    7

    Chỉnh sửa tệp và thử nghiệm đọc các khối dữ liệu bằng cách đưa ra vị trí và độ dài ký tự bắt đầu, chẳng hạn như

    select sys_context('userenv','service_name') from dual
    
    81

  • 7.2 Lấy clob như một chuỗi

    Đối với các clobs đủ nhỏ để phù hợp với bộ nhớ ứng dụng, việc tìm nạp chúng trực tiếp nhanh hơn nhiều dưới dạng chuỗi.

    Xem lại mã có trong

    select sys_context('userenv','service_name') from dual
    
    82. Sự khác biệt từ
    select sys_context('userenv','service_name') from dual
    
    77 được hiển thị in đậm:

    -- Default database username
    def user = "pythonhol"
    
    -- Default database connection string
    def connect_string = "localhost/orclpdb1"
    
    -- Prompt for the password
    accept pw char prompt 'Enter database password for &user: ' hide
    
    8

    OutputTypeHandler khiến CX_Oracle tìm nạp Clob dưới dạng chuỗi. Có thể sử dụng các hàm chuỗi Python tiêu chuẩn như

    select sys_context('userenv','service_name') from dual
    
    84 trên kết quả.

    Đầu ra giống như đối với

    select sys_context('userenv','service_name') from dual
    
    77. Để kiểm tra, hãy chạy tệp:

    -- Default database username
    def user = "pythonhol"
    
    -- Default database connection string
    def connect_string = "localhost/orclpdb1"
    
    -- Prompt for the password
    accept pw char prompt 'Enter database password for &user: ' hide
    
    9

8. Hàm Rowfactory

Các hàm RowFactory cho phép các truy vấn trả về các đối tượng khác ngoài bộ dữ liệu. Chúng có thể được sử dụng để cung cấp tên cho các cột khác nhau hoặc để trả về các đối tượng tùy chỉnh.

  • 8.1 Rowfactory cho tên cột ánh xạ

    Xem lại mã có trong

    select sys_context('userenv','service_name') from dual
    
    86:

    import cx_Oracle
    
    dsn_tns = cx_Oracle.makedsn('Host Name', 'Port Number', service_name='Service Name') # if needed, place an 'r' before any parameter in order to address special characters such as '\'.
    conn = cx_Oracle.connect(user=r'User Name', password='Personal Password', dsn=dsn_tns) # if needed, place an 'r' before any parameter in order to address special characters such as '\'. For example, if your user name contains '\', you'll need to place 'r' before the user name: user=r'User Name'
    
    c = conn.cursor()
    c.execute('select * from database.table') # use triple quotes if you want to spread your query across multiple lines
    for row in c:
        print (row[0], '-', row[1]) # this only shows the first two columns. To add an additional column you'll need to add , '-', row[2], etc.
    #conn.close()
    
    00

    Điều này cho thấy hai phương thức truy cập kết quả đặt các mục từ hàng dữ liệu. Đầu tiên sử dụng các chỉ mục mảng như

    select sys_context('userenv','service_name') from dual
    
    87. Thứ hai sử dụng các biến mục tiêu vòng lặp lấy các giá trị của mỗi tuple.

    Chạy tệp:

    import cx_Oracle
    
    dsn_tns = cx_Oracle.makedsn('Host Name', 'Port Number', service_name='Service Name') # if needed, place an 'r' before any parameter in order to address special characters such as '\'.
    conn = cx_Oracle.connect(user=r'User Name', password='Personal Password', dsn=dsn_tns) # if needed, place an 'r' before any parameter in order to address special characters such as '\'. For example, if your user name contains '\', you'll need to place 'r' before the user name: user=r'User Name'
    
    c = conn.cursor()
    c.execute('select * from database.table') # use triple quotes if you want to spread your query across multiple lines
    for row in c:
        print (row[0], '-', row[1]) # this only shows the first two columns. To add an additional column you'll need to add , '-', row[2], etc.
    #conn.close()
    
    01

    Cả hai phương thức truy cập cho cùng một kết quả.

    Để sử dụng chức năng RowFactory, chỉnh sửa

    select sys_context('userenv','service_name') from dual
    
    86 và thêm mã này ở phía dưới:

    import cx_Oracle
    
    dsn_tns = cx_Oracle.makedsn('Host Name', 'Port Number', service_name='Service Name') # if needed, place an 'r' before any parameter in order to address special characters such as '\'.
    conn = cx_Oracle.connect(user=r'User Name', password='Personal Password', dsn=dsn_tns) # if needed, place an 'r' before any parameter in order to address special characters such as '\'. For example, if your user name contains '\', you'll need to place 'r' before the user name: user=r'User Name'
    
    c = conn.cursor()
    c.execute('select * from database.table') # use triple quotes if you want to spread your query across multiple lines
    for row in c:
        print (row[0], '-', row[1]) # this only shows the first two columns. To add an additional column you'll need to add , '-', row[2], etc.
    #conn.close()
    
    02

    Điều này sử dụng chức năng Python Factory

    select sys_context('userenv','service_name') from dual
    
    89 để tạo một lớp con tuple cho phép truy cập vào các phần tử thông qua các chỉ mục hoặc tên trường đã cho.

    Hàm

    select sys_context('userenv','service_name') from dual
    
    90 cho thấy việc sử dụng các trường Tuple có tên mới. Phong cách mã hóa này có thể giúp giảm lỗi mã hóa.

    Chạy lại tập lệnh:

    import cx_Oracle
    
    dsn_tns = cx_Oracle.makedsn('Host Name', 'Port Number', service_name='Service Name') # if needed, place an 'r' before any parameter in order to address special characters such as '\'.
    conn = cx_Oracle.connect(user=r'User Name', password='Personal Password', dsn=dsn_tns) # if needed, place an 'r' before any parameter in order to address special characters such as '\'. For example, if your user name contains '\', you'll need to place 'r' before the user name: user=r'User Name'
    
    c = conn.cursor()
    c.execute('select * from database.table') # use triple quotes if you want to spread your query across multiple lines
    for row in c:
        print (row[0], '-', row[1]) # this only shows the first two columns. To add an additional column you'll need to add , '-', row[2], etc.
    #conn.close()
    
    01

    Các kết quả đầu ra là như nhau.

9. Kết nối phân lớp và con trỏ

Phân lớp cho phép ứng dụng để tạo kết nối "móc" và tạo con trỏ. Điều này có thể được sử dụng để thay đổi hoặc ghi nhật ký các tham số thực hiện và kết nối và để mở rộng chức năng cx_oracle. Liên kết tài liệu để đọc thêm: Truy tìm các câu lệnh SQL và PL/SQL.

  • 9.1 Kết nối phân lớp

    Xem lại mã có trong

    select sys_context('userenv','service_name') from dual
    
    91:

    import cx_Oracle
    
    dsn_tns = cx_Oracle.makedsn('Host Name', 'Port Number', service_name='Service Name') # if needed, place an 'r' before any parameter in order to address special characters such as '\'.
    conn = cx_Oracle.connect(user=r'User Name', password='Personal Password', dsn=dsn_tns) # if needed, place an 'r' before any parameter in order to address special characters such as '\'. For example, if your user name contains '\', you'll need to place 'r' before the user name: user=r'User Name'
    
    c = conn.cursor()
    c.execute('select * from database.table') # use triple quotes if you want to spread your query across multiple lines
    for row in c:
        print (row[0], '-', row[1]) # this only shows the first two columns. To add an additional column you'll need to add , '-', row[2], etc.
    #conn.close()
    
    04

    Điều này tạo ra một lớp mới "myconnection" kế thừa từ lớp kết nối CX_Oracle. Phương thức

    select sys_context('userenv','service_name') from dual
    
    92 được gọi khi một thể hiện của lớp mới được tạo. Nó in một tin nhắn và gọi lớp cơ sở, truyền thông tin kết nối.

    Trong ứng dụng "Bình thường", mã ứng dụng:

    import cx_Oracle
    
    dsn_tns = cx_Oracle.makedsn('Host Name', 'Port Number', service_name='Service Name') # if needed, place an 'r' before any parameter in order to address special characters such as '\'.
    conn = cx_Oracle.connect(user=r'User Name', password='Personal Password', dsn=dsn_tns) # if needed, place an 'r' before any parameter in order to address special characters such as '\'. For example, if your user name contains '\', you'll need to place 'r' before the user name: user=r'User Name'
    
    c = conn.cursor()
    c.execute('select * from database.table') # use triple quotes if you want to spread your query across multiple lines
    for row in c:
        print (row[0], '-', row[1]) # this only shows the first two columns. To add an additional column you'll need to add , '-', row[2], etc.
    #conn.close()
    
    05

    Không cần phải cung cấp bất kỳ thông tin đăng nhập nào, vì chúng được nhúng trong lớp con tùy chỉnh. Tất cả các phương thức CX_Oracle như

    select sys_context('userenv','service_name') from dual
    
    09 đều có sẵn, như được hiển thị bởi truy vấn.

    Chạy tệp:

    import cx_Oracle
    
    dsn_tns = cx_Oracle.makedsn('Host Name', 'Port Number', service_name='Service Name') # if needed, place an 'r' before any parameter in order to address special characters such as '\'.
    conn = cx_Oracle.connect(user=r'User Name', password='Personal Password', dsn=dsn_tns) # if needed, place an 'r' before any parameter in order to address special characters such as '\'. For example, if your user name contains '\', you'll need to place 'r' before the user name: user=r'User Name'
    
    c = conn.cursor()
    c.execute('select * from database.table') # use triple quotes if you want to spread your query across multiple lines
    for row in c:
        print (row[0], '-', row[1]) # this only shows the first two columns. To add an additional column you'll need to add , '-', row[2], etc.
    #conn.close()
    
    06

    Truy vấn thực hiện thành công.

  • 9.2 con trỏ phân lớp con

    Chỉnh sửa

    select sys_context('userenv','service_name') from dual
    
    91 và mở rộng phương thức
    select sys_context('userenv','service_name') from dual
    
    09 với lớp MyCursor mới:

    import cx_Oracle
    
    dsn_tns = cx_Oracle.makedsn('Host Name', 'Port Number', service_name='Service Name') # if needed, place an 'r' before any parameter in order to address special characters such as '\'.
    conn = cx_Oracle.connect(user=r'User Name', password='Personal Password', dsn=dsn_tns) # if needed, place an 'r' before any parameter in order to address special characters such as '\'. For example, if your user name contains '\', you'll need to place 'r' before the user name: user=r'User Name'
    
    c = conn.cursor()
    c.execute('select * from database.table') # use triple quotes if you want to spread your query across multiple lines
    for row in c:
        print (row[0], '-', row[1]) # this only shows the first two columns. To add an additional column you'll need to add , '-', row[2], etc.
    #conn.close()
    
    07

    Khi ứng dụng nhận được một con trỏ từ lớp

    select sys_context('userenv','service_name') from dual
    
    96, phương thức
    select sys_context('userenv','service_name') from dual
    
    09 mới sẽ trả về một thể hiện của lớp
    select sys_context('userenv','service_name') from dual
    
    98 mới của chúng tôi.

    Mã truy vấn "ứng dụng" vẫn không thay đổi. Các phương thức

    select sys_context('userenv','service_name') from dual
    
    10 và
    select sys_context('userenv','service_name') from dual
    
    16 mới của lớp
    select sys_context('userenv','service_name') from dual
    
    98 được gọi. Họ thực hiện một số ghi nhật ký và gọi các phương thức cha mẹ để thực hiện tuyên bố thực tế.

    Để xác nhận điều này, hãy chạy lại tệp:

    import cx_Oracle
    
    dsn_tns = cx_Oracle.makedsn('Host Name', 'Port Number', service_name='Service Name') # if needed, place an 'r' before any parameter in order to address special characters such as '\'.
    conn = cx_Oracle.connect(user=r'User Name', password='Personal Password', dsn=dsn_tns) # if needed, place an 'r' before any parameter in order to address special characters such as '\'. For example, if your user name contains '\', you'll need to place 'r' before the user name: user=r'User Name'
    
    c = conn.cursor()
    c.execute('select * from database.table') # use triple quotes if you want to spread your query across multiple lines
    for row in c:
        print (row[0], '-', row[1]) # this only shows the first two columns. To add an additional column you'll need to add , '-', row[2], etc.
    #conn.close()
    
    06

10. xếp hàng nâng cao

Oracle Advanced xếp hàng (AQ) cho phép các tin nhắn được truyền giữa các ứng dụng. Liên kết tài liệu để đọc thêm: xếp hàng của Oracle Advanced (aq).

  • 10.1 Thông điệp truyền tải với Oracle Advanced xếp hàng

    Đánh giá

    select username from dba_users
    
    02:

    import cx_Oracle
    
    dsn_tns = cx_Oracle.makedsn('Host Name', 'Port Number', service_name='Service Name') # if needed, place an 'r' before any parameter in order to address special characters such as '\'.
    conn = cx_Oracle.connect(user=r'User Name', password='Personal Password', dsn=dsn_tns) # if needed, place an 'r' before any parameter in order to address special characters such as '\'. For example, if your user name contains '\', you'll need to place 'r' before the user name: user=r'User Name'
    
    c = conn.cursor()
    c.execute('select * from database.table') # use triple quotes if you want to spread your query across multiple lines
    for row in c:
        print (row[0], '-', row[1]) # this only shows the first two columns. To add an additional column you'll need to add , '-', row[2], etc.
    #conn.close()
    
    09

    Tệp này thiết lập hàng đợi nâng cao bằng gói DBMS_AQADM của Oracle. Hàng đợi được sử dụng để chuyển các đối tượng Oracle UDT_book. Tệp sử dụng các tính năng giao diện AQ được tăng cường trong CX_ORACLE 7.2.

    Chạy tệp:

    import cx_Oracle
    
    dsn_tns = cx_Oracle.makedsn('Host Name', 'Port Number', service_name='Service Name') # if needed, place an 'r' before any parameter in order to address special characters such as '\'.
    conn = cx_Oracle.connect(user=r'User Name', password='Personal Password', dsn=dsn_tns) # if needed, place an 'r' before any parameter in order to address special characters such as '\'. For example, if your user name contains '\', you'll need to place 'r' before the user name: user=r'User Name'
    
    c = conn.cursor()
    c.execute('select * from database.table') # use triple quotes if you want to spread your query across multiple lines
    for row in c:
        print (row[0], '-', row[1]) # this only shows the first two columns. To add an additional column you'll need to add , '-', row[2], etc.
    #conn.close()
    
    10

    Truy vấn thực hiện thành công.

    9.2 con trỏ phân lớp con

    Chỉnh sửa

    select sys_context('userenv','service_name') from dual
    
    91 và mở rộng phương thức
    select sys_context('userenv','service_name') from dual
    
    09 với lớp MyCursor mới:

    Khi ứng dụng nhận được một con trỏ từ lớp

    select sys_context('userenv','service_name') from dual
    
    96, phương thức
    select sys_context('userenv','service_name') from dual
    
    09 mới sẽ trả về một thể hiện của lớp
    select sys_context('userenv','service_name') from dual
    
    98 mới của chúng tôi.

    import cx_Oracle
    
    dsn_tns = cx_Oracle.makedsn('Host Name', 'Port Number', service_name='Service Name') # if needed, place an 'r' before any parameter in order to address special characters such as '\'.
    conn = cx_Oracle.connect(user=r'User Name', password='Personal Password', dsn=dsn_tns) # if needed, place an 'r' before any parameter in order to address special characters such as '\'. For example, if your user name contains '\', you'll need to place 'r' before the user name: user=r'User Name'
    
    c = conn.cursor()
    c.execute('select * from database.table') # use triple quotes if you want to spread your query across multiple lines
    for row in c:
        print (row[0], '-', row[1]) # this only shows the first two columns. To add an additional column you'll need to add , '-', row[2], etc.
    #conn.close()
    
    11

    Mã truy vấn "ứng dụng" vẫn không thay đổi. Các phương thức

    select sys_context('userenv','service_name') from dual
    
    10 và
    select sys_context('userenv','service_name') from dual
    
    16 mới của lớp
    select sys_context('userenv','service_name') from dual
    
    98 được gọi. Họ thực hiện một số ghi nhật ký và gọi các phương thức cha mẹ để thực hiện tuyên bố thực tế.

    Để xác nhận điều này, hãy chạy lại tệp:

    import cx_Oracle
    
    dsn_tns = cx_Oracle.makedsn('Host Name', 'Port Number', service_name='Service Name') # if needed, place an 'r' before any parameter in order to address special characters such as '\'.
    conn = cx_Oracle.connect(user=r'User Name', password='Personal Password', dsn=dsn_tns) # if needed, place an 'r' before any parameter in order to address special characters such as '\'. For example, if your user name contains '\', you'll need to place 'r' before the user name: user=r'User Name'
    
    c = conn.cursor()
    c.execute('select * from database.table') # use triple quotes if you want to spread your query across multiple lines
    for row in c:
        print (row[0], '-', row[1]) # this only shows the first two columns. To add an additional column you'll need to add , '-', row[2], etc.
    #conn.close()
    
    12

    10. xếp hàng nâng cao

    Oracle Advanced xếp hàng (AQ) cho phép các tin nhắn được truyền giữa các ứng dụng. Liên kết tài liệu để đọc thêm: xếp hàng của Oracle Advanced (aq).

10.1 Thông điệp truyền tải với Oracle Advanced xếp hàng

Đánh giá

select username from dba_users
02:

  • Tệp này thiết lập hàng đợi nâng cao bằng gói DBMS_AQADM của Oracle. Hàng đợi được sử dụng để chuyển các đối tượng Oracle UDT_book. Tệp sử dụng các tính năng giao diện AQ được tăng cường trong CX_ORACLE 7.2.

    Đầu ra cho thấy các thông điệp được xếp hàng và khử.

    import cx_Oracle
    
    dsn_tns = cx_Oracle.makedsn('Host Name', 'Port Number', service_name='Service Name') # if needed, place an 'r' before any parameter in order to address special characters such as '\'.
    conn = cx_Oracle.connect(user=r'User Name', password='Personal Password', dsn=dsn_tns) # if needed, place an 'r' before any parameter in order to address special characters such as '\'. For example, if your user name contains '\', you'll need to place 'r' before the user name: user=r'User Name'
    
    c = conn.cursor()
    c.execute('select * from database.table') # use triple quotes if you want to spread your query across multiple lines
    for row in c:
        print (row[0], '-', row[1]) # this only shows the first two columns. To add an additional column you'll need to add , '-', row[2], etc.
    #conn.close()
    
    13

    Để thử nghiệm, chia mã thành ba tệp: một để tạo và khởi động hàng đợi và hai tệp khác để xếp hàng và dequeue. Thử nghiệm chạy các tệp hàng đợi và dequeue đồng thời trong các cửa sổ đầu cuối riêng biệt.

    Hãy thử xóa cuộc gọi

    select username from dba_users
    
    03 trong
    select username from dba_users
    
    04. Bây giờ chạy
    select username from dba_users
    
    05 một lần và sau đó
    select username from dba_users
    
    04 nhiều lần. Các tin nhắn tương tự sẽ có sẵn mỗi khi bạn cố gắng hủy bỏ chúng.

    Thay đổi

    select username from dba_users
    
    04 để cam kết trong một giao dịch riêng biệt bằng cách thay đổi cài đặt "khả năng hiển thị":

    Điều này đưa ra hành vi tương tự như mã gốc.

    Chạy tệp:

    import cx_Oracle
    
    dsn_tns = cx_Oracle.makedsn('Host Name', 'Port Number', service_name='Service Name') # if needed, place an 'r' before any parameter in order to address special characters such as '\'.
    conn = cx_Oracle.connect(user=r'User Name', password='Personal Password', dsn=dsn_tns) # if needed, place an 'r' before any parameter in order to address special characters such as '\'. For example, if your user name contains '\', you'll need to place 'r' before the user name: user=r'User Name'
    
    c = conn.cursor()
    c.execute('select * from database.table') # use triple quotes if you want to spread your query across multiple lines
    for row in c:
        print (row[0], '-', row[1]) # this only shows the first two columns. To add an additional column you'll need to add , '-', row[2], etc.
    #conn.close()
    
    14

    Bây giờ hãy thay đổi các tùy chọn của các tin nhắn enqueued để chúng hết hạn từ hàng đợi nếu chúng không được khử trùng sau bốn giây:

  • Bây giờ chạy
    select username from dba_users
    
    05 và đợi bốn giây trước khi bạn chạy
    select username from dba_users
    
    04. Không nên có thông điệp để dequeue.

    Nếu bạn bị mắc kẹt, hãy xem trong thư mục

    select username from dba_users
    
    10 tại các tệp
    select username from dba_users
    
    04,
    select username from dba_users
    
    05 và
    select username from dba_users
    
    13.

    import cx_Oracle
    
    dsn_tns = cx_Oracle.makedsn('Host Name', 'Port Number', service_name='Service Name') # if needed, place an 'r' before any parameter in order to address special characters such as '\'.
    conn = cx_Oracle.connect(user=r'User Name', password='Personal Password', dsn=dsn_tns) # if needed, place an 'r' before any parameter in order to address special characters such as '\'. For example, if your user name contains '\', you'll need to place 'r' before the user name: user=r'User Name'
    
    c = conn.cursor()
    c.execute('select * from database.table') # use triple quotes if you want to spread your query across multiple lines
    for row in c:
        print (row[0], '-', row[1]) # this only shows the first two columns. To add an additional column you'll need to add , '-', row[2], etc.
    #conn.close()
    
    15

    11. Truy cập tài liệu Oracle đơn giản (Soda)

    Truy cập tài liệu Oracle đơn giản (SODA) là một tập hợp các API kiểu NoQuery. Tài liệu có thể được chèn, truy vấn và lấy từ cơ sở dữ liệu Oracle. Theo mặc định, tài liệu là chuỗi JSON. API soda tồn tại trong nhiều ngôn ngữ. Liên kết tài liệu để đọc thêm: Truy cập tài liệu Oracle đơn giản (SODA).

    11.1 Chèn tài liệu JSON

    Đánh giá

    select username from dba_users
    
    14:

    import cx_Oracle
    
    dsn_tns = cx_Oracle.makedsn('Host Name', 'Port Number', service_name='Service Name') # if needed, place an 'r' before any parameter in order to address special characters such as '\'.
    conn = cx_Oracle.connect(user=r'User Name', password='Personal Password', dsn=dsn_tns) # if needed, place an 'r' before any parameter in order to address special characters such as '\'. For example, if your user name contains '\', you'll need to place 'r' before the user name: user=r'User Name'
    
    c = conn.cursor()
    c.execute('select * from database.table') # use triple quotes if you want to spread your query across multiple lines
    for row in c:
        print (row[0], '-', row[1]) # this only shows the first two columns. To add an additional column you'll need to add , '-', row[2], etc.
    #conn.close()
    
    17

    select username from dba_users
    
    15 sẽ tạo một bộ sưu tập mới hoặc mở một bộ sưu tập hiện có, nếu tên đã được sử dụng. .

select username from dba_users 16 chèn nội dung của tài liệu vào cơ sở dữ liệu và trả về một đối tượng tài liệu soda. Điều này cho phép truy cập vào dữ liệu meta như khóa tài liệu. Theo mặc định, các khóa tài liệu được tự động tạo.

Phương pháp

select username from dba_users
17 được sử dụng để bắt đầu một thao tác sẽ hành động theo các tài liệu trong bộ sưu tập.

  • select username from dba_users
    
    18 là một từ điển. Bạn cũng có thể nhận được một chuỗi JSON bằng cách gọi
    select username from dba_users
    
    19.
  • Đầu ra hiển thị nội dung của tài liệu mới.
  • 11.2 Tìm kiếm tài liệu soda
  • Mở rộng
    select username from dba_users
    
    14 để chèn thêm một số tài liệu và thực hiện thao tác Find Filter:
  • Chạy lại tập lệnh:
  • import cx_Oracle
    
    dsn_tns = cx_Oracle.makedsn('Host Name', 'Port Number', service_name='Service Name') # if needed, place an 'r' before any parameter in order to address special characters such as '\'.
    conn = cx_Oracle.connect(user=r'User Name', password='Personal Password', dsn=dsn_tns) # if needed, place an 'r' before any parameter in order to address special characters such as '\'. For example, if your user name contains '\', you'll need to place 'r' before the user name: user=r'User Name'
    
    c = conn.cursor()
    c.execute('select * from database.table') # use triple quotes if you want to spread your query across multiple lines
    for row in c:
        print (row[0], '-', row[1]) # this only shows the first two columns. To add an additional column you'll need to add , '-', row[2], etc.
    #conn.close()
    
    14
  • Hoạt động tìm thấy bộ sưu tập và trả lại các tài liệu trong đó thành phố là Melbourne. Lưu ý phương thức
    select username from dba_users
    
    21 hiện đang được xem trước.
  • Soda hỗ trợ truy vấn bằng ví dụ (QBE) với một bộ toán tử rộng lớn. Mở rộng
    select username from dba_users
    
    14 với QBE để tìm các tài liệu trong đó độ tuổi nhỏ hơn 25:

Chạy tập lệnh hiển thị tên.

Bản tóm tắt

Trong hướng dẫn này, bạn đã học được cách:

Tạo kết nối

Sử dụng kết nối kết nối CX_Oracle và kết nối thường trú trong cơ sở dữ liệu

  • Thực hiện truy vấn và tìm nạp dữ liệu

    import cx_Oracle
    
    dsn_tns = cx_Oracle.makedsn('Host Name', 'Port Number', service_name='Service Name') # if needed, place an 'r' before any parameter in order to address special characters such as '\'.
    conn = cx_Oracle.connect(user=r'User Name', password='Personal Password', dsn=dsn_tns) # if needed, place an 'r' before any parameter in order to address special characters such as '\'. For example, if your user name contains '\', you'll need to place 'r' before the user name: user=r'User Name'
    
    c = conn.cursor()
    c.execute('select * from database.table') # use triple quotes if you want to spread your query across multiple lines
    for row in c:
        print (row[0], '-', row[1]) # this only shows the first two columns. To add an additional column you'll need to add , '-', row[2], etc.
    #conn.close()
    
    18
  • Ngoài ra, chạy trình thông dịch Python bằng cách thực thi lệnh

    select username from dba_users
    
    24 trong một thiết bị đầu cuối, sau đó tương tác nhập các lệnh. Sử dụng CTRL-D để thoát trở lại dấu nhắc hệ điều hành.Ctrl-D to exit back to the operating system prompt.

Khi bạn chạy các tập lệnh, Python sẽ tự động tạo các phiên bản mã byte của chúng trong một thư mục có tên

select username from dba_users
25. Chúng cải thiện hiệu suất của các tập lệnh được chạy nhiều lần. Chúng được tự động tạo lại nếu tệp nguồn thay đổi.

Vết lõm

Thắng khoảng trắng có ý nghĩa trong Python. Khi sao chép các ví dụ, sử dụng cùng một căn chỉnh cột như được hiển thị. Các mẫu trong phòng thí nghiệm này sử dụng không gian, không phải các tab.

Các bản in thụt sau đây 'xong' một lần sau khi vòng lặp hoàn thành:

import cx_Oracle

dsn_tns = cx_Oracle.makedsn('Host Name', 'Port Number', service_name='Service Name') # if needed, place an 'r' before any parameter in order to address special characters such as '\'.
conn = cx_Oracle.connect(user=r'User Name', password='Personal Password', dsn=dsn_tns) # if needed, place an 'r' before any parameter in order to address special characters such as '\'. For example, if your user name contains '\', you'll need to place 'r' before the user name: user=r'User Name'

c = conn.cursor()
c.execute('select * from database.table') # use triple quotes if you want to spread your query across multiple lines
for row in c:
    print (row[0], '-', row[1]) # this only shows the first two columns. To add an additional column you'll need to add , '-', row[2], etc.
#conn.close()
19

Nhưng thụt lề này in 'xong' trong mỗi lần lặp:

import cx_Oracle

dsn_tns = cx_Oracle.makedsn('Host Name', 'Port Number', service_name='Service Name') # if needed, place an 'r' before any parameter in order to address special characters such as '\'.
conn = cx_Oracle.connect(user=r'User Name', password='Personal Password', dsn=dsn_tns) # if needed, place an 'r' before any parameter in order to address special characters such as '\'. For example, if your user name contains '\', you'll need to place 'r' before the user name: user=r'User Name'

c = conn.cursor()
c.execute('select * from database.table') # use triple quotes if you want to spread your query across multiple lines
for row in c:
    print (row[0], '-', row[1]) # this only shows the first two columns. To add an additional column you'll need to add , '-', row[2], etc.
#conn.close()
20

Dây

Chuỗi Python có thể được đặt trong các trích dẫn đơn hoặc đôi:

import cx_Oracle

dsn_tns = cx_Oracle.makedsn('Host Name', 'Port Number', service_name='Service Name') # if needed, place an 'r' before any parameter in order to address special characters such as '\'.
conn = cx_Oracle.connect(user=r'User Name', password='Personal Password', dsn=dsn_tns) # if needed, place an 'r' before any parameter in order to address special characters such as '\'. For example, if your user name contains '\', you'll need to place 'r' before the user name: user=r'User Name'

c = conn.cursor()
c.execute('select * from database.table') # use triple quotes if you want to spread your query across multiple lines
for row in c:
    print (row[0], '-', row[1]) # this only shows the first two columns. To add an additional column you'll need to add , '-', row[2], etc.
#conn.close()
21

Các chuỗi đa dòng Sử dụng cú pháp ba quote:

import cx_Oracle

dsn_tns = cx_Oracle.makedsn('Host Name', 'Port Number', service_name='Service Name') # if needed, place an 'r' before any parameter in order to address special characters such as '\'.
conn = cx_Oracle.connect(user=r'User Name', password='Personal Password', dsn=dsn_tns) # if needed, place an 'r' before any parameter in order to address special characters such as '\'. For example, if your user name contains '\', you'll need to place 'r' before the user name: user=r'User Name'

c = conn.cursor()
c.execute('select * from database.table') # use triple quotes if you want to spread your query across multiple lines
for row in c:
    print (row[0], '-', row[1]) # this only shows the first two columns. To add an additional column you'll need to add , '-', row[2], etc.
#conn.close()
22

Biến

Các biến không cần loại được khai báo:

import cx_Oracle

dsn_tns = cx_Oracle.makedsn('Host Name', 'Port Number', service_name='Service Name') # if needed, place an 'r' before any parameter in order to address special characters such as '\'.
conn = cx_Oracle.connect(user=r'User Name', password='Personal Password', dsn=dsn_tns) # if needed, place an 'r' before any parameter in order to address special characters such as '\'. For example, if your user name contains '\', you'll need to place 'r' before the user name: user=r'User Name'

c = conn.cursor()
c.execute('select * from database.table') # use triple quotes if you want to spread your query across multiple lines
for row in c:
    print (row[0], '-', row[1]) # this only shows the first two columns. To add an additional column you'll need to add , '-', row[2], etc.
#conn.close()
23

Bình luận

Nhận xét là một dòng đơn:

import cx_Oracle

dsn_tns = cx_Oracle.makedsn('Host Name', 'Port Number', service_name='Service Name') # if needed, place an 'r' before any parameter in order to address special characters such as '\'.
conn = cx_Oracle.connect(user=r'User Name', password='Personal Password', dsn=dsn_tns) # if needed, place an 'r' before any parameter in order to address special characters such as '\'. For example, if your user name contains '\', you'll need to place 'r' before the user name: user=r'User Name'

c = conn.cursor()
c.execute('select * from database.table') # use triple quotes if you want to spread your query across multiple lines
for row in c:
    print (row[0], '-', row[1]) # this only shows the first two columns. To add an additional column you'll need to add , '-', row[2], etc.
#conn.close()
24

Chúng có thể là đa dòng bằng cách sử dụng mã thông báo ba cách để tạo một chuỗi không có gì:

import cx_Oracle

dsn_tns = cx_Oracle.makedsn('Host Name', 'Port Number', service_name='Service Name') # if needed, place an 'r' before any parameter in order to address special characters such as '\'.
conn = cx_Oracle.connect(user=r'User Name', password='Personal Password', dsn=dsn_tns) # if needed, place an 'r' before any parameter in order to address special characters such as '\'. For example, if your user name contains '\', you'll need to place 'r' before the user name: user=r'User Name'

c = conn.cursor()
c.execute('select * from database.table') # use triple quotes if you want to spread your query across multiple lines
for row in c:
    print (row[0], '-', row[1]) # this only shows the first two columns. To add an additional column you'll need to add , '-', row[2], etc.
#conn.close()
25

In

Chuỗi và biến có thể được hiển thị với hàm

select sys_context('userenv','service_name') from dual
90:

import cx_Oracle

dsn_tns = cx_Oracle.makedsn('Host Name', 'Port Number', service_name='Service Name') # if needed, place an 'r' before any parameter in order to address special characters such as '\'.
conn = cx_Oracle.connect(user=r'User Name', password='Personal Password', dsn=dsn_tns) # if needed, place an 'r' before any parameter in order to address special characters such as '\'. For example, if your user name contains '\', you'll need to place 'r' before the user name: user=r'User Name'

c = conn.cursor()
c.execute('select * from database.table') # use triple quotes if you want to spread your query across multiple lines
for row in c:
    print (row[0], '-', row[1]) # this only shows the first two columns. To add an additional column you'll need to add , '-', row[2], etc.
#conn.close()
26

Cấu trúc dữ liệu

Các mảng liên kết được gọi là 'từ điển':

import cx_Oracle

dsn_tns = cx_Oracle.makedsn('Host Name', 'Port Number', service_name='Service Name') # if needed, place an 'r' before any parameter in order to address special characters such as '\'.
conn = cx_Oracle.connect(user=r'User Name', password='Personal Password', dsn=dsn_tns) # if needed, place an 'r' before any parameter in order to address special characters such as '\'. For example, if your user name contains '\', you'll need to place 'r' before the user name: user=r'User Name'

c = conn.cursor()
c.execute('select * from database.table') # use triple quotes if you want to spread your query across multiple lines
for row in c:
    print (row[0], '-', row[1]) # this only shows the first two columns. To add an additional column you'll need to add , '-', row[2], etc.
#conn.close()
27

Các mảng được đặt hàng được gọi là 'danh sách':

import cx_Oracle

dsn_tns = cx_Oracle.makedsn('Host Name', 'Port Number', service_name='Service Name') # if needed, place an 'r' before any parameter in order to address special characters such as '\'.
conn = cx_Oracle.connect(user=r'User Name', password='Personal Password', dsn=dsn_tns) # if needed, place an 'r' before any parameter in order to address special characters such as '\'. For example, if your user name contains '\', you'll need to place 'r' before the user name: user=r'User Name'

c = conn.cursor()
c.execute('select * from database.table') # use triple quotes if you want to spread your query across multiple lines
for row in c:
    print (row[0], '-', row[1]) # this only shows the first two columns. To add an additional column you'll need to add , '-', row[2], etc.
#conn.close()
28

Danh sách có thể được truy cập thông qua các chỉ mục.

import cx_Oracle

dsn_tns = cx_Oracle.makedsn('Host Name', 'Port Number', service_name='Service Name') # if needed, place an 'r' before any parameter in order to address special characters such as '\'.
conn = cx_Oracle.connect(user=r'User Name', password='Personal Password', dsn=dsn_tns) # if needed, place an 'r' before any parameter in order to address special characters such as '\'. For example, if your user name contains '\', you'll need to place 'r' before the user name: user=r'User Name'

c = conn.cursor()
c.execute('select * from database.table') # use triple quotes if you want to spread your query across multiple lines
for row in c:
    print (row[0], '-', row[1]) # this only shows the first two columns. To add an additional column you'll need to add , '-', row[2], etc.
#conn.close()
29

Tuples giống như danh sách nhưng không thể thay đổi sau khi chúng được tạo. Chúng được tạo ra với dấu ngoặc đơn:

import cx_Oracle

dsn_tns = cx_Oracle.makedsn('Host Name', 'Port Number', service_name='Service Name') # if needed, place an 'r' before any parameter in order to address special characters such as '\'.
conn = cx_Oracle.connect(user=r'User Name', password='Personal Password', dsn=dsn_tns) # if needed, place an 'r' before any parameter in order to address special characters such as '\'. For example, if your user name contains '\', you'll need to place 'r' before the user name: user=r'User Name'

c = conn.cursor()
c.execute('select * from database.table') # use triple quotes if you want to spread your query across multiple lines
for row in c:
    print (row[0], '-', row[1]) # this only shows the first two columns. To add an additional column you'll need to add , '-', row[2], etc.
#conn.close()
30

Các giá trị riêng lẻ trong một tuple có thể được gán cho các biến như:

import cx_Oracle

dsn_tns = cx_Oracle.makedsn('Host Name', 'Port Number', service_name='Service Name') # if needed, place an 'r' before any parameter in order to address special characters such as '\'.
conn = cx_Oracle.connect(user=r'User Name', password='Personal Password', dsn=dsn_tns) # if needed, place an 'r' before any parameter in order to address special characters such as '\'. For example, if your user name contains '\', you'll need to place 'r' before the user name: user=r'User Name'

c = conn.cursor()
c.execute('select * from database.table') # use triple quotes if you want to spread your query across multiple lines
for row in c:
    print (row[0], '-', row[1]) # this only shows the first two columns. To add an additional column you'll need to add , '-', row[2], etc.
#conn.close()
31

Bây giờ biến V1 chứa 3, biến V2 chứa 7 và biến V3 chứa 10.

Giá trị trong một mục nhập duy nhất như "________ 327" có thể được gán cho một biến bằng cách đặt dấu phẩy sau tên biến như:

import cx_Oracle

dsn_tns = cx_Oracle.makedsn('Host Name', 'Port Number', service_name='Service Name') # if needed, place an 'r' before any parameter in order to address special characters such as '\'.
conn = cx_Oracle.connect(user=r'User Name', password='Personal Password', dsn=dsn_tns) # if needed, place an 'r' before any parameter in order to address special characters such as '\'. For example, if your user name contains '\', you'll need to place 'r' before the user name: user=r'User Name'

c = conn.cursor()
c.execute('select * from database.table') # use triple quotes if you want to spread your query across multiple lines
for row in c:
    print (row[0], '-', row[1]) # this only shows the first two columns. To add an additional column you'll need to add , '-', row[2], etc.
#conn.close()
32

Nếu bài tập là:

import cx_Oracle

dsn_tns = cx_Oracle.makedsn('Host Name', 'Port Number', service_name='Service Name') # if needed, place an 'r' before any parameter in order to address special characters such as '\'.
conn = cx_Oracle.connect(user=r'User Name', password='Personal Password', dsn=dsn_tns) # if needed, place an 'r' before any parameter in order to address special characters such as '\'. For example, if your user name contains '\', you'll need to place 'r' before the user name: user=r'User Name'

c = conn.cursor()
c.execute('select * from database.table') # use triple quotes if you want to spread your query across multiple lines
for row in c:
    print (row[0], '-', row[1]) # this only shows the first two columns. To add an additional column you'll need to add , '-', row[2], etc.
#conn.close()
33

Sau đó

select username from dba_users
28 sẽ chứa toàn bộ bộ tuple "
select username from dba_users
27"

Các đối tượng

Tất cả mọi thứ trong Python là một đối tượng. Ví dụ, được đưa ra trong danh sách

select username from dba_users
30 ở trên, phương thức
select username from dba_users
31 có thể được sử dụng để thêm giá trị vào danh sách.

import cx_Oracle

dsn_tns = cx_Oracle.makedsn('Host Name', 'Port Number', service_name='Service Name') # if needed, place an 'r' before any parameter in order to address special characters such as '\'.
conn = cx_Oracle.connect(user=r'User Name', password='Personal Password', dsn=dsn_tns) # if needed, place an 'r' before any parameter in order to address special characters such as '\'. For example, if your user name contains '\', you'll need to place 'r' before the user name: user=r'User Name'

c = conn.cursor()
c.execute('select * from database.table') # use triple quotes if you want to spread your query across multiple lines
for row in c:
    print (row[0], '-', row[1]) # this only shows the first two columns. To add an additional column you'll need to add , '-', row[2], etc.
#conn.close()
34

Bây giờ

select username from dba_users
30 chứa
select username from dba_users
33

Kiểm soát lưu lượng

Luồng mã có thể được kiểm soát với các bài kiểm tra và vòng lặp. Các tuyên bố ____ 334/________ 335/________ 336 trông giống như:

import cx_Oracle

dsn_tns = cx_Oracle.makedsn('Host Name', 'Port Number', service_name='Service Name') # if needed, place an 'r' before any parameter in order to address special characters such as '\'.
conn = cx_Oracle.connect(user=r'User Name', password='Personal Password', dsn=dsn_tns) # if needed, place an 'r' before any parameter in order to address special characters such as '\'. For example, if your user name contains '\', you'll need to place 'r' before the user name: user=r'User Name'

c = conn.cursor()
c.execute('select * from database.table') # use triple quotes if you want to spread your query across multiple lines
for row in c:
    print (row[0], '-', row[1]) # this only shows the first two columns. To add an additional column you'll need to add , '-', row[2], etc.
#conn.close()
35

Điều này cũng cho thấy cách các mệnh đề được phân định bằng các dấu chấm và mỗi khối phụ của mã được thụt vào.

Vòng lặp

Một vòng lặp truyền thống là:

import cx_Oracle

dsn_tns = cx_Oracle.makedsn('Host Name', 'Port Number', service_name='Service Name') # if needed, place an 'r' before any parameter in order to address special characters such as '\'.
conn = cx_Oracle.connect(user=r'User Name', password='Personal Password', dsn=dsn_tns) # if needed, place an 'r' before any parameter in order to address special characters such as '\'. For example, if your user name contains '\', you'll need to place 'r' before the user name: user=r'User Name'

c = conn.cursor()
c.execute('select * from database.table') # use triple quotes if you want to spread your query across multiple lines
for row in c:
    print (row[0], '-', row[1]) # this only shows the first two columns. To add an additional column you'll need to add , '-', row[2], etc.
#conn.close()
36

Điều này in các số từ 0 đến 9. Giá trị của

select username from dba_users
37 được tăng lên trong mỗi lần lặp.

Lệnh '

select username from dba_users
38' cũng có thể được sử dụng để lặp lại danh sách và bộ dữ liệu:

import cx_Oracle

dsn_tns = cx_Oracle.makedsn('Host Name', 'Port Number', service_name='Service Name') # if needed, place an 'r' before any parameter in order to address special characters such as '\'.
conn = cx_Oracle.connect(user=r'User Name', password='Personal Password', dsn=dsn_tns) # if needed, place an 'r' before any parameter in order to address special characters such as '\'. For example, if your user name contains '\', you'll need to place 'r' before the user name: user=r'User Name'

c = conn.cursor()
c.execute('select * from database.table') # use triple quotes if you want to spread your query across multiple lines
for row in c:
    print (row[0], '-', row[1]) # this only shows the first two columns. To add an additional column you'll need to add , '-', row[2], etc.
#conn.close()
37

Điều này đặt lần lượt

select username from dba_users
39 cho từng phần tử của danh sách
select username from dba_users
40.

Chức năng

Một hàm có thể được định nghĩa là:

import cx_Oracle

dsn_tns = cx_Oracle.makedsn('Host Name', 'Port Number', service_name='Service Name') # if needed, place an 'r' before any parameter in order to address special characters such as '\'.
conn = cx_Oracle.connect(user=r'User Name', password='Personal Password', dsn=dsn_tns) # if needed, place an 'r' before any parameter in order to address special characters such as '\'. For example, if your user name contains '\', you'll need to place 'r' before the user name: user=r'User Name'

c = conn.cursor()
c.execute('select * from database.table') # use triple quotes if you want to spread your query across multiple lines
for row in c:
    print (row[0], '-', row[1]) # this only shows the first two columns. To add an additional column you'll need to add , '-', row[2], etc.
#conn.close()
38

Các chức năng có thể hoặc không thể trả về giá trị. Chức năng này có thể được gọi bằng cách sử dụng:

import cx_Oracle

dsn_tns = cx_Oracle.makedsn('Host Name', 'Port Number', service_name='Service Name') # if needed, place an 'r' before any parameter in order to address special characters such as '\'.
conn = cx_Oracle.connect(user=r'User Name', password='Personal Password', dsn=dsn_tns) # if needed, place an 'r' before any parameter in order to address special characters such as '\'. For example, if your user name contains '\', you'll need to place 'r' before the user name: user=r'User Name'

c = conn.cursor()
c.execute('select * from database.table') # use triple quotes if you want to spread your query across multiple lines
for row in c:
    print (row[0], '-', row[1]) # this only shows the first two columns. To add an additional column you'll need to add , '-', row[2], etc.
#conn.close()
39

Các cuộc gọi chức năng phải xuất hiện sau định nghĩa chức năng của chúng.

Các chức năng cũng là đối tượng và có các thuộc tính. Thuộc tính

select username from dba_users
41 sẵn có có thể được sử dụng để tìm mô tả chức năng:

import cx_Oracle

dsn_tns = cx_Oracle.makedsn('Host Name', 'Port Number', service_name='Service Name') # if needed, place an 'r' before any parameter in order to address special characters such as '\'.
conn = cx_Oracle.connect(user=r'User Name', password='Personal Password', dsn=dsn_tns) # if needed, place an 'r' before any parameter in order to address special characters such as '\'. For example, if your user name contains '\', you'll need to place 'r' before the user name: user=r'User Name'

c = conn.cursor()
c.execute('select * from database.table') # use triple quotes if you want to spread your query across multiple lines
for row in c:
    print (row[0], '-', row[1]) # this only shows the first two columns. To add an additional column you'll need to add , '-', row[2], etc.
#conn.close()
40

Mô -đun

Files phụ có thể được bao gồm trong các tập lệnh Python với một tuyên bố nhập khẩu.

import cx_Oracle

dsn_tns = cx_Oracle.makedsn('Host Name', 'Port Number', service_name='Service Name') # if needed, place an 'r' before any parameter in order to address special characters such as '\'.
conn = cx_Oracle.connect(user=r'User Name', password='Personal Password', dsn=dsn_tns) # if needed, place an 'r' before any parameter in order to address special characters such as '\'. For example, if your user name contains '\', you'll need to place 'r' before the user name: user=r'User Name'

c = conn.cursor()
c.execute('select * from database.table') # use triple quotes if you want to spread your query across multiple lines
for row in c:
    print (row[0], '-', row[1]) # this only shows the first two columns. To add an additional column you'll need to add , '-', row[2], etc.
#conn.close()
41

Nhiều mô -đun được xác định trước tồn tại, chẳng hạn như các mô -đun HĐH và SYS.

Tài nguyên

  • Tài liệu Python
  • Tài liệu Python CX_Oracle
  • Kho lưu trữ mã nguồn Python CX_Oracle
Bản quyền © 2017, 2021, Oracle và/hoặc các chi nhánh của nó. Đã đăng ký Bản quyền