Nếu bạn chưa làm 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. Truy xuất thông tin kết nối
Tiếp theo, lấy thông tin kết nối. Bạn có thể làm điều đó bằng cách định vị tnsnames của bạn. ora trên máy tính của bạn [e. g. , gõ tnsnames. ora trong thanh tìm kiếm của Windows]
Bây giờ, hãy mở tnsnames của bạn. ora và tìm kiếm kết nối mong muốn của bạn
Nó sẽ giống như thông tin bên dưới [được tô sáng bằng 3 yếu tố màu mà bạn thường cần tìm trước khi có thể thiết lập kết nối giữa Python và cơ sở dữ liệu Oracle của mình]
HỆ THỐNG_OCON =
[TẢ =
[ADDRESS = [PROTOCOL = TCP][HOST = Tên máy chủ ][PORT = Cổng]]
[LOAD_BALANCE = CÓ]
[CONNECT_DATA =
[Máy chủ = CHUYÊN DỤNG]
[SERVICE_NAME = Tên dịch vụ ]
[FAILOVER_MODE =
[LOẠI = CHỌN]
[PHƯƠNG PHÁP = CƠ BẢN]
[THỬ LẠI = 180]
[TRÌ HOÃN = 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 bằng 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 khác để truy xuất thông tin cần thiết nhằm hỗ trợ 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 để lấy 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 vừa xem cách kết nối Python với Oracle bằng cx_Oracle connect. Khi bạn đã thiết lập kết nối như vậy, bạn có thể bắt đầu sử dụng SQL trong Python để 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
Tóm lược. trong hướng dẫn này, bạn sẽ học cách chọn dữ liệu từ Cơ sở dữ liệu Oracle bằng cách sử dụng các phương thức fetchone[]
, fetchmany[]
và fetchall[]
Để chọn dữ liệu từ Cơ sở dữ liệu Oracle trong chương trình Python, bạn làm theo các bước sau
- Đầu tiên, thiết lập kết nối tới Cơ sở dữ liệu Oracle bằng phương thức
cx_Oracle.connect[]
- Thứ hai, tạo đối tượng
Cursor
từ đối tượng Kết nối bằng phương thứcConnection.cursor[]
- Thứ ba, thực hiện một câu lệnh SQL để chọn dữ liệu từ một hoặc nhiều bảng bằng cách sử dụng phương thức
0
Code language: Python [python]import cx_Oracle import config sql = 'select customer_id, name ' \ 'from customers ' \ 'order by name' try: with cx_Oracle.connect[ config.username, config.password, config.dsn, encoding=config.encoding] as connection: with connection.cursor[] as cursor: cursor.execute[sql] while True: row = cursor.fetchone[] if row is None: break print[row] except cx_Oracle.Error as error: print[error]
- Thứ tư, tìm nạp các hàng bằng cách sử dụng các phương thức
1,
Code language: Python [python]import cx_Oracle import config sql = 'select customer_id, name ' \ 'from customers ' \ 'order by name' try: with cx_Oracle.connect[ config.username, config.password, config.dsn, encoding=config.encoding] as connection: with connection.cursor[] as cursor: cursor.execute[sql] while True: row = cursor.fetchone[] if row is None: break print[row] except cx_Oracle.Error as error: print[error]
2 và
Code language: Python [python]import cx_Oracle import config sql = 'select customer_id, name ' \ 'from customers ' \ 'order by name' try: with cx_Oracle.connect[ config.username, config.password, config.dsn, encoding=config.encoding] as connection: with connection.cursor[] as cursor: cursor.execute[sql] while True: row = cursor.fetchone[] if row is None: break print[row] except cx_Oracle.Error as error: print[error]
3
Code language: Python [python]import cx_Oracle import config sql = 'select customer_id, name ' \ 'from customers ' \ 'order by name' try: with cx_Oracle.connect[ config.username, config.password, config.dsn, encoding=config.encoding] as connection: with connection.cursor[] as cursor: cursor.execute[sql] while True: row = cursor.fetchone[] if row is None: break print[row] except cx_Oracle.Error as error: print[error]
- Cuối cùng, giải phóng các đối tượng
Cursor
và
5 bằng cách sử dụng phương thức
Code language: Python [python]import cx_Oracle import config sql = 'select customer_id, name ' \ 'from customers ' \ 'order by name' try: with cx_Oracle.connect[ config.username, config.password, config.dsn, encoding=config.encoding] as connection: with connection.cursor[] as cursor: cursor.execute[sql] while True: row = cursor.fetchone[] if row is None: break print[row] except cx_Oracle.Error as error: print[error]
6 và
Code language: Python [python]import cx_Oracle import config sql = 'select customer_id, name ' \ 'from customers ' \ 'order by name' try: with cx_Oracle.connect[ config.username, config.password, config.dsn, encoding=config.encoding] as connection: with connection.cursor[] as cursor: cursor.execute[sql] while True: row = cursor.fetchone[] if row is None: break print[row] except cx_Oracle.Error as error: print[error]
7. Nếu bạn muốn tự động giải phóng
Code language: Python [python]import cx_Oracle import config sql = 'select customer_id, name ' \ 'from customers ' \ 'order by name' try: with cx_Oracle.connect[ config.username, config.password, config.dsn, encoding=config.encoding] as connection: with connection.cursor[] as cursor: cursor.execute[sql] while True: row = cursor.fetchone[] if row is None: break print[row] except cx_Oracle.Error as error: print[error]
Cursor
và
5, bạn có thể sử dụng khối
Code language: Python [python]import cx_Oracle import config sql = 'select customer_id, name ' \ 'from customers ' \ 'order by name' try: with cx_Oracle.connect[ config.username, config.password, config.dsn, encoding=config.encoding] as connection: with connection.cursor[] as cursor: cursor.execute[sql] while True: row = cursor.fetchone[] if row is None: break print[row] except cx_Oracle.Error as error: print[error]
0
Code language: Python [python]import cx_Oracle import config sql = 'select customer_id, name ' \ 'from customers ' \ 'order by name' batch_size = 20 try: with cx_Oracle.connect[ config.username, config.password, config.dsn, encoding=config.encoding] as connection: with connection.cursor[] as cursor: # execute the SQL statement cursor.execute[sql] while True: # fetch rows rows = cursor.fetchmany[batch_size] if not rows: break # display rows for row in rows: print[row] except cx_Oracle.Error as error: print[error]
Chúng tôi sẽ sử dụng bảng
1 từ cơ sở dữ liệu mẫuCode language: Python [python]
import cx_Oracle import config sql = 'select customer_id, name ' \ 'from customers ' \ 'order by name' batch_size = 20 try: with cx_Oracle.connect[ config.username, config.password, config.dsn, encoding=config.encoding] as connection: with connection.cursor[] as cursor: # execute the SQL statement cursor.execute[sql] while True: # fetch rows rows = cursor.fetchmany[batch_size] if not rows: break # display rows for row in rows: print[row] except cx_Oracle.Error as error: print[error]
và mô-đun
2 sau đâyCode language: Python [python]
import cx_Oracle import config sql = 'select customer_id, name ' \ 'from customers ' \ 'order by name' batch_size = 20 try: with cx_Oracle.connect[ config.username, config.password, config.dsn, encoding=config.encoding] as connection: with connection.cursor[] as cursor: # execute the SQL statement cursor.execute[sql] while True: # fetch rows rows = cursor.fetchmany[batch_size] if not rows: break # display rows for row in rows: print[row] except cx_Oracle.Error as error: print[error]
Code language: Python [python]
username = 'OT' password = '' dsn = 'localhost/pdborcl' port = 1512 encoding = 'UTF-8'
Truy vấn dữ liệu bằng phương pháp import cx_Oracle
import config
sql = 'select customer_id, name ' \
'from customers ' \
'order by name'
try:
with cx_Oracle.connect[
config.username,
config.password,
config.dsn,
encoding=config.encoding] as connection:
with connection.cursor[] as cursor:
cursor.execute[sql]
while True:
row = cursor.fetchone[]
if row is None:
break
print[row]
except cx_Oracle.Error as error:
print[error]
Code language: Python [python]
1
import cx_Oracle
import config
sql = 'select customer_id, name ' \
'from customers ' \
'order by name'
try:
with cx_Oracle.connect[
config.username,
config.password,
config.dsn,
encoding=config.encoding] as connection:
with connection.cursor[] as cursor:
cursor.execute[sql]
while True:
row = cursor.fetchone[]
if row is None:
break
print[row]
except cx_Oracle.Error as error:
print[error]
4 sau đây minh họa cách chọn dữ liệu từ bảngCode language: Python [python]
import cx_Oracle import config sql = 'select customer_id, name ' \ 'from customers ' \ 'order by name' batch_size = 20 try: with cx_Oracle.connect[ config.username, config.password, config.dsn, encoding=config.encoding] as connection: with connection.cursor[] as cursor: # execute the SQL statement cursor.execute[sql] while True: # fetch rows rows = cursor.fetchmany[batch_size] if not rows: break # display rows for row in rows: print[row] except cx_Oracle.Error as error: print[error]
1Code language: Python [python]
import cx_Oracle import config sql = 'select customer_id, name ' \ 'from customers ' \ 'order by name' batch_size = 20 try: with cx_Oracle.connect[ config.username, config.password, config.dsn, encoding=config.encoding] as connection: with connection.cursor[] as cursor: # execute the SQL statement cursor.execute[sql] while True: # fetch rows rows = cursor.fetchmany[batch_size] if not rows: break # display rows for row in rows: print[row] except cx_Oracle.Error as error: print[error]
Code language: Python [python]
import cx_Oracle import config sql = 'select customer_id, name ' \ 'from customers ' \ 'order by name' try: with cx_Oracle.connect[ config.username, config.password, config.dsn, encoding=config.encoding] as connection: with connection.cursor[] as cursor: cursor.execute[sql] while True: row = cursor.fetchone[] if row is None: break print[row] except cx_Oracle.Error as error: print[error]
Mặc dù
1 trả về một hàng tại một thời điểm, nhưng nó luôn truy xuất dữ liệu từ Cơ sở dữ liệu Oracle theo lô với kích thước lô mặc định làCode language: Python [python]
import cx_Oracle import config sql = 'select customer_id, name ' \ 'from customers ' \ 'order by name' try: with cx_Oracle.connect[ config.username, config.password, config.dsn, encoding=config.encoding] as connection: with connection.cursor[] as cursor: cursor.execute[sql] while True: row = cursor.fetchone[] if row is None: break print[row] except cx_Oracle.Error as error: print[error]
7Code language: Python [python]
import cx_Oracle import config sql = 'select customer_id, name ' \ 'from customers ' \ 'order by name' batch_size = 20 try: with cx_Oracle.connect[ config.username, config.password, config.dsn, encoding=config.encoding] as connection: with connection.cursor[] as cursor: # execute the SQL statement cursor.execute[sql] while True: # fetch rows rows = cursor.fetchmany[batch_size] if not rows: break # display rows for row in rows: print[row] except cx_Oracle.Error as error: print[error]
Để cải thiện hiệu suất, bạn có thể điều chỉnh giá trị của
7 trước khi gọi phương thứcCode language: Python [python]
import cx_Oracle import config sql = 'select customer_id, name ' \ 'from customers ' \ 'order by name' batch_size = 20 try: with cx_Oracle.connect[ config.username, config.password, config.dsn, encoding=config.encoding] as connection: with connection.cursor[] as cursor: # execute the SQL statement cursor.execute[sql] while True: # fetch rows rows = cursor.fetchmany[batch_size] if not rows: break # display rows for row in rows: print[row] except cx_Oracle.Error as error: print[error]
0Code language: Python [python]
import cx_Oracle import config sql = 'select customer_id, name ' \ 'from customers ' \ 'order by name' try: with cx_Oracle.connect[ config.username, config.password, config.dsn, encoding=config.encoding] as connection: with connection.cursor[] as cursor: cursor.execute[sql] while True: row = cursor.fetchone[] if row is None: break print[row] except cx_Oracle.Error as error: print[error]
Lưu ý rằng việc tăng giá trị của
7 giúp giảm số lần truy cập cơ sở dữ liệu. Tuy nhiên, nó làm tăng dung lượng bộ nhớ cần thiết
import cx_Oracle import config sql = 'select customer_id, name ' \ 'from customers ' \ 'order by name' batch_size = 20 try: with cx_Oracle.connect[ config.username, config.password, config.dsn, encoding=config.encoding] as connection: with connection.cursor[] as cursor: # execute the SQL statement cursor.execute[sql] while True: # fetch rows rows = cursor.fetchmany[batch_size] if not rows: break # display rows for row in rows: print[row] except cx_Oracle.Error as error: print[error]
Code language: Python [python]
Truy vấn dữ liệu bằng phương pháp import cx_Oracle
import config
sql = 'select customer_id, name ' \
'from customers ' \
'order by name'
try:
with cx_Oracle.connect[
config.username,
config.password,
config.dsn,
encoding=config.encoding] as connection:
with connection.cursor[] as cursor:
cursor.execute[sql]
while True:
row = cursor.fetchone[]
if row is None:
break
print[row]
except cx_Oracle.Error as error:
print[error]
Code language: Python [python]
2
import cx_Oracle
import config
sql = 'select customer_id, name ' \
'from customers ' \
'order by name'
try:
with cx_Oracle.connect[
config.username,
config.password,
config.dsn,
encoding=config.encoding] as connection:
with connection.cursor[] as cursor:
cursor.execute[sql]
while True:
row = cursor.fetchone[]
if row is None:
break
print[row]
except cx_Oracle.Error as error:
print[error]
Nếu bạn muốn xử lý các hàng theo lô, bạn có thể sử dụng phương thức
2. Trong trường hợp này, bạn chuyển kích thước lô cho phương thứcCode language: Python [python]
import cx_Oracle import config sql = 'select customer_id, name ' \ 'from customers ' \ 'order by name' try: with cx_Oracle.connect[ config.username, config.password, config.dsn, encoding=config.encoding] as connection: with connection.cursor[] as cursor: cursor.execute[sql] while True: row = cursor.fetchone[] if row is None: break print[row] except cx_Oracle.Error as error: print[error]
2. Kích thước lô mặc định làCode language: Python [python]
import cx_Oracle import config sql = 'select customer_id, name ' \ 'from customers ' \ 'order by name' try: with cx_Oracle.connect[ config.username, config.password, config.dsn, encoding=config.encoding] as connection: with connection.cursor[] as cursor: cursor.execute[sql] while True: row = cursor.fetchone[] if row is None: break print[row] except cx_Oracle.Error as error: print[error]
7Code language: Python [python]
import cx_Oracle import config sql = 'select customer_id, name ' \ 'from customers ' \ 'order by name' batch_size = 20 try: with cx_Oracle.connect[ config.username, config.password, config.dsn, encoding=config.encoding] as connection: with connection.cursor[] as cursor: # execute the SQL statement cursor.execute[sql] while True: # fetch rows rows = cursor.fetchmany[batch_size] if not rows: break # display rows for row in rows: print[row] except cx_Oracle.Error as error: print[error]
Code language: Python [python]
import cx_Oracle import config sql = 'select customer_id, name ' \ 'from customers ' \ 'order by name' batch_size = 20 try: with cx_Oracle.connect[ config.username, config.password, config.dsn, encoding=config.encoding] as connection: with connection.cursor[] as cursor: # execute the SQL statement cursor.execute[sql] while True: # fetch rows rows = cursor.fetchmany[batch_size] if not rows: break # display rows for row in rows: print[row] except cx_Oracle.Error as error: print[error]
Truy vấn dữ liệu bằng phương pháp import cx_Oracle
import config
sql = 'select customer_id, name ' \
'from customers ' \
'order by name'
try:
with cx_Oracle.connect[
config.username,
config.password,
config.dsn,
encoding=config.encoding] as connection:
with connection.cursor[] as cursor:
cursor.execute[sql]
while True:
row = cursor.fetchone[]
if row is None:
break
print[row]
except cx_Oracle.Error as error:
print[error]
Code language: Python [python]
3
import cx_Oracle
import config
sql = 'select customer_id, name ' \
'from customers ' \
'order by name'
try:
with cx_Oracle.connect[
config.username,
config.password,
config.dsn,
encoding=config.encoding] as connection:
with connection.cursor[] as cursor:
cursor.execute[sql]
while True:
row = cursor.fetchone[]
if row is None:
break
print[row]
except cx_Oracle.Error as error:
print[error]
Nếu số lượng hàng nhỏ và có thể vừa với bộ nhớ, bạn có thể sử dụng phương pháp
3Code language: Python [python]
import cx_Oracle import config sql = 'select customer_id, name ' \ 'from customers ' \ 'order by name' try: with cx_Oracle.connect[ config.username, config.password, config.dsn, encoding=config.encoding] as connection: with connection.cursor[] as cursor: cursor.execute[sql] while True: row = cursor.fetchone[] if row is None: break print[row] except cx_Oracle.Error as error: print[error]
Code language: Python [python]
import cx_Oracle import config sql = 'select customer_id, name ' \ 'from customers ' \ 'order by name' try: # connect to the Oracle Database with cx_Oracle.connect[ config.username, config.password, config.dsn, encoding=config.encoding] as connection: with connection.cursor[] as cursor: # execute the SQL statement cursor.execute[sql] # fetch all rows rows = cursor.fetchall[] if rows: for row in rows: print[row] except cx_Oracle.Error as error: print[error]
Trong hướng dẫn này, bạn đã học cách sử dụng các phương thức fetchone[]
, fetchmany[]
và fetchall[]
của đối tượng Cursor
để lấy dữ liệu từ Cơ sở dữ liệu Oracle