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? Show
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_oracleNế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ốiTiế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) Bước 3: Kết nối Python với Oracle bằng CX_Oracle ConnectCuố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ổ sungBạ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:
Để 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 quanHướ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_OracleCá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. Thành lập
1. Kết nối với OracleBạ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.
2. Kết nối gộpViệ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.
3. Tìm nạp dữ liệuThự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.
4. Dữ liệu ràng buộcCá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.
5. PL/SQLPL/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.
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.
7. LOBCơ 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.
8. Hàm RowfactoryCá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.
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.
10. xếp hàng nâng caoOracle 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_users02:
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_users17 đượ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.
Chạy tập lệnh hiển thị tên. Bản tóm tắtTrong hướng dẫn này, bạn đã học được cách: Tạo kết nốiSử 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
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_users25. 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õmThắ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âyChuỗ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ếnCá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ậnNhậ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 InChuỗi và biến có thể được hiển thị với hàm select sys_context('userenv','service_name') from dual90: 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ệuCá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_users28 sẽ chứa toàn bộ bộ tuple " select username from dba_users27" Các đối tượngTấ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_users30 ở trên, phương thức select username from dba_users31 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_users30 chứa select username from dba_users33 Kiểm soát lưu lượngLuồ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ặpMộ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_users37 được tăng lên trong mỗi lần lặp. Lệnh ' select username from dba_users38' 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_users39 cho từng phần tử của danh sách select username from dba_users40. Chức năngMộ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_users41 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ô -đunFiles 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
|