Tóm lược. trong hướng dẫn này, bạn sẽ học cách chọn dữ liệu từ Cơ sở dữ liệu Oracle bằng các phương thức
9,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 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]
1Code 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]
Để 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
2
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ứ hai, tạo đối tượng
3 từ đối tượng Kết nối bằ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]
4
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ứ ba, thực thi một câu lệnh SQL để chọn dữ liệu từ một hoặc nhiều bảng bằng phương thức
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á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
3 vàimport cx_Oracle import config sql = 'select customer_id, name ' \ 'from customers ' \ 'order by name' try: with cx_Oracle.connect[ config.username, config.password, config.dsn, encoding=config.encoding] as connection: with connection.cursor[] as cursor: cursor.execute[sql] while True: row = cursor.fetchone[] if row is None: break print[row] except cx_Oracle.Error as error: print[error]
5 bằ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]
3 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]
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 sauCode 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' 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]
Truy vấn dữ liệu bằng phương thức import cx_Oracle
import config
sql = 'select customer_id, name ' \
'from customers ' \
'order by name'
try:
with cx_Oracle.connect[
config.username,
config.password,
config.dsn,
encoding=config.encoding] as connection:
with connection.cursor[] as cursor:
cursor.execute[sql]
while True:
row = cursor.fetchone[]
if row is None:
break
print[row]
except cx_Oracle.Error as error:
print[error]
Code language: Python [python]
1
import cx_Oracle
import config
sql = 'select customer_id, name ' \
'from customers ' \
'order by name'
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ảng
import cx_Oracle import config sql = 'select customer_id, name ' \ 'from customers ' \ 'order by name' batch_size = 20 try: with cx_Oracle.connect[ config.username, config.password, config.dsn, encoding=config.encoding] as connection: with connection.cursor[] as cursor: # execute the SQL statement cursor.execute[sql] while True: # fetch rows rows = cursor.fetchmany[batch_size] if not rows: break # display rows for row in rows: print[row] except cx_Oracle.Error as error: print[error]
Code language: Python [python]
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ếtCode 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 thức import cx_Oracle
import config
sql = 'select customer_id, name ' \
'from customers ' \
'order by name'
try:
with cx_Oracle.connect[
config.username,
config.password,
config.dsn,
encoding=config.encoding] as connection:
with connection.cursor[] as cursor:
cursor.execute[sql]
while True:
row = cursor.fetchone[]
if row is None:
break
print[row]
except cx_Oracle.Error as error:
print[error]
Code language: Python [python]
2
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ức
import cx_Oracle import config sql = 'select customer_id, name ' \ 'from customers ' \ 'order by name' try: with cx_Oracle.connect[ config.username, config.password, config.dsn, encoding=config.encoding] as connection: with connection.cursor[] as cursor: cursor.execute[sql] while True: row = cursor.fetchone[] if row is None: break print[row] except cx_Oracle.Error as error: print[error]
Code language: Python [python]
2. Kích thước lô mặc định là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________số 8Code 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 ít và có thể vừa với bộ nhớ, bạn có thể sử dụng phương thức
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]
8Code 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]
Trong hướng dẫn này, bạn đã học cách sử dụng các phương thức
9,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 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]
1 của đối tượngCode 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 để lấy dữ liệu từ Cơ sở dữ liệu OracleCode 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]