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ức Connection.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
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)
whileTrue:
row = cursor.fetchone()
if row isNone:
break
print(row)
except cx_Oracle.Error as error:
print(error)
Code language:Python(python)
0
Thứ tư, tìm nạp các hàng bằng cách sử dụng các phương thức
import cx_Oracle
import config
sql = 'select customer_id, name ' \
'from customers ' \
'order by name'try:
with cx_Oracle.connect(
config.username,
config.password,
config.dsn,
encoding=config.encoding) as connection:
with connection.cursor() as cursor:
cursor.execute(sql)
whileTrue:
row = cursor.fetchone()
if row isNone:
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)
whileTrue:
row = cursor.fetchone()
if row isNone:
break
print(row)
except cx_Oracle.Error as error:
print(error)
Code language:Python(python)
2 và
import cx_Oracle
import config
sql = 'select customer_id, name ' \
'from customers ' \
'order by name'try:
with cx_Oracle.connect(
config.username,
config.password,
config.dsn,
encoding=config.encoding) as connection:
with connection.cursor() as cursor:
cursor.execute(sql)
whileTrue:
row = cursor.fetchone()
if row isNone:
break
print(row)
except cx_Oracle.Error as error:
print(error)
Code language:Python(python)
3
Cuối cùng, giải phóng các đối tượng Cursor 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)
whileTrue:
row = cursor.fetchone()
if row isNone:
break
print(row)
except cx_Oracle.Error as error:
print(error)
Code language:Python(python)
5 bằng cách sử dụng phương thức
import cx_Oracle
import config
sql = 'select customer_id, name ' \
'from customers ' \
'order by name'try:
with cx_Oracle.connect(
config.username,
config.password,
config.dsn,
encoding=config.encoding) as connection:
with connection.cursor() as cursor:
cursor.execute(sql)
whileTrue:
row = cursor.fetchone()
if row isNone:
break
print(row)
except cx_Oracle.Error as error:
print(error)
Code language:Python(python)
6 và
import cx_Oracle
import config
sql = 'select customer_id, name ' \
'from customers ' \
'order by name'try:
with cx_Oracle.connect(
config.username,
config.password,
config.dsn,
encoding=config.encoding) as connection:
with connection.cursor() as cursor:
cursor.execute(sql)
whileTrue:
row = cursor.fetchone()
if row isNone:
break
print(row)
except cx_Oracle.Error as error:
print(error)
Code language:Python(python)
7. Nếu bạn muốn tự động giải phóng Cursor 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)
whileTrue:
row = cursor.fetchone()
if row isNone:
break
print(row)
except cx_Oracle.Error as error:
print(error)
Code language:Python(python)
5, bạn có thể sử dụng khối
import cx_Oracle
import config
sql = 'select customer_id, name ' \
'from customers ' \
'order by name'
batch_size = 20try:
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)
whileTrue:
# fetch rows
rows = cursor.fetchmany(batch_size)
ifnot rows:
break# display rowsfor row in rows:
print(row)
except cx_Oracle.Error as error:
print(error)
Code language:Python(python)
0
Chúng tôi sẽ sử dụng bảng
import cx_Oracle
import config
sql = 'select customer_id, name ' \
'from customers ' \
'order by name'
batch_size = 20try:
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)
whileTrue:
# fetch rows
rows = cursor.fetchmany(batch_size)
ifnot rows:
break# display rowsfor row in rows:
print(row)
except cx_Oracle.Error as error:
print(error)
Code language:Python(python)
1 từ cơ sở dữ liệu mẫu
và mô-đun
import cx_Oracle
import config
sql = 'select customer_id, name ' \
'from customers ' \
'order by name'
batch_size = 20try:
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)
whileTrue:
# fetch rows
rows = cursor.fetchmany(batch_size)
ifnot rows:
break# display rowsfor 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)1
import cx_Oracle
import config
sql = 'select customer_id, name ' \
'from customers ' \
'order by name'
batch_size = 20try:
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)
whileTrue:
# fetch rows
rows = cursor.fetchmany(batch_size)
ifnot rows:
break# display rowsfor row in rows:
print(row)
except cx_Oracle.Error as error:
print(error)
Code language:Python(python)
4 sau đây minh họa cách chọn dữ liệu từ bảng
import cx_Oracle
import config
sql = 'select customer_id, name ' \
'from customers ' \
'order by name'
batch_size = 20try:
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)
whileTrue:
# fetch rows
rows = cursor.fetchmany(batch_size)
ifnot rows:
break# display rowsfor row in rows:
print(row)
except cx_Oracle.Error as error:
print(error)
Code language:Python(python)
1
import cx_Oracle
import config
sql = 'select customer_id, name ' \
'from customers ' \
'order by name'try:
with cx_Oracle.connect(
config.username,
config.password,
config.dsn,
encoding=config.encoding) as connection:
with connection.cursor() as cursor:
cursor.execute(sql)
whileTrue:
row = cursor.fetchone()
if row isNone:
break
print(row)
except cx_Oracle.Error as error:
print(error)
Code language:Python(python)
Mặc dù
import cx_Oracle
import config
sql = 'select customer_id, name ' \
'from customers ' \
'order by name'try:
with cx_Oracle.connect(
config.username,
config.password,
config.dsn,
encoding=config.encoding) as connection:
with connection.cursor() as cursor:
cursor.execute(sql)
whileTrue:
row = cursor.fetchone()
if row isNone:
break
print(row)
except cx_Oracle.Error as error:
print(error)
Code language:Python(python)
1 trả về một hàng tại một thời điểm, nhưng nó luôn truy xuất dữ liệu từ Cơ sở dữ liệu Oracle theo lô với kích thước lô mặc định là
import cx_Oracle
import config
sql = 'select customer_id, name ' \
'from customers ' \
'order by name'
batch_size = 20try:
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)
whileTrue:
# fetch rows
rows = cursor.fetchmany(batch_size)
ifnot rows:
break# display rowsfor row in rows:
print(row)
except cx_Oracle.Error as error:
print(error)
Code language:Python(python)
7
Để cải thiện hiệu suất, bạn có thể điều chỉnh giá trị của
import cx_Oracle
import config
sql = 'select customer_id, name ' \
'from customers ' \
'order by name'
batch_size = 20try:
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)
whileTrue:
# fetch rows
rows = cursor.fetchmany(batch_size)
ifnot rows:
break# display rowsfor row in rows:
print(row)
except cx_Oracle.Error as error:
print(error)
Code language:Python(python)
7 trước khi gọi phương thức
import cx_Oracle
import config
sql = 'select customer_id, name ' \
'from customers ' \
'order by name'try:
with cx_Oracle.connect(
config.username,
config.password,
config.dsn,
encoding=config.encoding) as connection:
with connection.cursor() as cursor:
cursor.execute(sql)
whileTrue:
row = cursor.fetchone()
if row isNone:
break
print(row)
except cx_Oracle.Error as error:
print(error)
Code language:Python(python)
0
Lưu ý rằng việc tăng giá trị của
import cx_Oracle
import config
sql = 'select customer_id, name ' \
'from customers ' \
'order by name'
batch_size = 20try:
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)
whileTrue:
# fetch rows
rows = cursor.fetchmany(batch_size)
ifnot rows:
break# display rowsfor row in rows:
print(row)
except cx_Oracle.Error as error:
print(error)
Code language:Python(python)
7 giúp giảm số lần truy cập cơ sở dữ liệu. Tuy nhiên, nó làm tăng dung lượng bộ nhớ cần thiết
Truy vấn dữ liệu bằng phương 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
Nếu bạn muốn xử lý các hàng theo lô, bạn có thể sử dụng phương thức
import cx_Oracle
import config
sql = 'select customer_id, name ' \
'from customers ' \
'order by name'try:
with cx_Oracle.connect(
config.username,
config.password,
config.dsn,
encoding=config.encoding) as connection:
with connection.cursor() as cursor:
cursor.execute(sql)
whileTrue:
row = cursor.fetchone()
if row isNone:
break
print(row)
except cx_Oracle.Error as error:
print(error)
Code language:Python(python)
2. Trong trường hợp này, bạn chuyển kích thước lô cho phương thức
import cx_Oracle
import config
sql = 'select customer_id, name ' \
'from customers ' \
'order by name'try:
with cx_Oracle.connect(
config.username,
config.password,
config.dsn,
encoding=config.encoding) as connection:
with connection.cursor() as cursor:
cursor.execute(sql)
whileTrue:
row = cursor.fetchone()
if row isNone:
break
print(row)
except cx_Oracle.Error as error:
print(error)
Code language:Python(python)
2. Kích thước lô mặc định là
import cx_Oracle
import config
sql = 'select customer_id, name ' \
'from customers ' \
'order by name'
batch_size = 20try:
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)
whileTrue:
# fetch rows
rows = cursor.fetchmany(batch_size)
ifnot rows:
break# display rowsfor row in rows:
print(row)
except cx_Oracle.Error as error:
print(error)
Code language:Python(python)
7
import cx_Oracle
import config
sql = 'select customer_id, name ' \
'from customers ' \
'order by name'
batch_size = 20try:
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)
whileTrue:
# fetch rows
rows = cursor.fetchmany(batch_size)
ifnot rows:
break# display rowsfor 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)3
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
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)
whileTrue:
row = cursor.fetchone()
if row isNone:
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:
# connect to the Oracle Databasewith 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)
Code language:Python(python)
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
Làm cách nào để lấy dữ liệu từ Cơ sở dữ liệu Oracle bằng Python?
Trăn. Đọc dữ liệu từ cơ sở dữ liệu Oracle .
Cài đặt gói. Nếu bạn chưa cài đặt gói trong môi trường Python của mình, hãy cài đặt gói đó bằng lệnh sau. cài đặt pip oracledb --upgrade. .
Kết nối với cơ sở dữ liệu Oracle. .
Đọc dữ liệu dưới dạng pandas DataFrame. .
Kết nối với máy khách gốc của Oracle. .
Người giới thiệu
Làm cách nào để lấy dữ liệu từ Cơ sở dữ liệu Oracle?
Bước 1. Kết nối cơ sở dữ liệu của bạn. Kết nối với cơ sở dữ liệu của bạn trong Oracle SQL Developer bằng cách sử dụng thông tin đăng nhập chính xác của cơ sở dữ liệu của bạn. .
Bước 2. Chạy truy vấn của bạn. .
Bước 3. Xuất dữ liệu của bạn. .
Bước 4. Chọn định dạng mong muốn của bạn. .
Bước 5. Sử dụng dữ liệu của bạn ở định dạng mong muốn
Làm cách nào để lấy dữ liệu từ cơ sở dữ liệu bằng Python?
Các bước sử dụng fetchall() trong Mysql bằng Python. nhập trình kết nối MySQL. Bây giờ, hãy tạo kết nối với trình kết nối MySQL bằng phương thức connect(). Tiếp theo, tạo một đối tượng con trỏ bằng phương thức con trỏ (). Bây giờ hãy tạo và thực hiện truy vấn bằng cách sử dụng câu lệnh “SELECT *” với phương thức exec() để lấy dữ liệu
Chúng ta có thể kết nối Cơ sở dữ liệu Oracle bằng Python không?
Các chương trình Python gọi hàm cx_Oracle. Cx_Oracle tự động tải nội bộ các thư viện Máy khách Oracle để truy cập Cơ sở dữ liệu Oracle . Cơ sở dữ liệu có thể trên cùng một máy với Python hoặc có thể ở xa. Nếu cơ sở dữ liệu là cục bộ, có thể sử dụng các thư viện máy khách từ cài đặt phần mềm Cơ sở dữ liệu Oracle.