Tóm lược. trong hướng dẫn này, bạn sẽ học cách sử dụng Python cx_Oracle API để chèn dữ liệu vào một bảng từ Cơ sở dữ liệu Oracle
Chèn một hàng vào bảng
Đoạn mã sau minh họa cách chèn một hàng mới vào bảng
8Code language: Python [python]
username = 'OT' password = '' dsn = 'localhost/pdborcl' port = 1512 encoding = 'UTF-8'
Code language: Python [python]
import cx_Oracle import config as cfg from datetime import datetime def insert_billing[billing_date, amount, customer_id, note]: """ Insert a row to the billing_headers table :param billing_date: :param amount: :param customer_id: :param note: :return: """ # construct an insert statement that add a new row to the billing_headers table sql = ['insert into billing_headers[billing_date, amount, customer_id, note] ' 'values[:billing_date,:amount,:customer_id,:note]'] try: # establish a new connection with cx_Oracle.connect[cfg.username, cfg.password, cfg.dsn, encoding=cfg.encoding] as connection: # create a cursor with connection.cursor[] as cursor: # execute the insert statement cursor.execute[sql, [billing_date, amount, customer_id, note]] # commit work connection.commit[] except cx_Oracle.Error as error: print['Error occurred:'] print[error] if __name__ == '__main__': insert_billing[datetime.now[], 1200, 1, None]
Trong hàm
9Code language: Python [python]
username = 'OT' password = '' dsn = 'localhost/pdborcl' port = 1512 encoding = 'UTF-8'
Đầu tiên, xây dựng một câu lệnh
0 để chèn một hàng mới vào bảngCode language: Python [python]
username = 'OT' password = '' dsn = 'localhost/pdborcl' port = 1512 encoding = 'UTF-8'
8. Câu lệnhCode language: Python [python]
username = 'OT' password = '' dsn = 'localhost/pdborcl' port = 1512 encoding = 'UTF-8'
0 này sử dụng các biến liên kết được đặt tênCode language: Python [python]
username = 'OT' password = '' dsn = 'localhost/pdborcl' port = 1512 encoding = 'UTF-8'
Thứ hai, kết nối với Cơ sở dữ liệu Oracle với thông tin được cung cấp bởi mô-đun
2Code language: Python [python]
username = 'OT' password = '' dsn = 'localhost/pdborcl' port = 1512 encoding = 'UTF-8'
Nếu bạn chưa làm theo hướng dẫn trước, bạn có thể tạo mô-đun
2 bằng mã sauCode language: Python [python]
username = 'OT' password = '' dsn = 'localhost/pdborcl' port = 1512 encoding = 'UTF-8'
Code language: Python [python]
username = 'OT' password = '' dsn = 'localhost/pdborcl' port = 1512 encoding = 'UTF-8'
Thứ ba, tạo một đối tượng
4 từ đối tượngCode language: Python [python]
username = 'OT' password = '' dsn = 'localhost/pdborcl' port = 1512 encoding = 'UTF-8'
5 và thực hiện câu lệnhCode language: Python [python]
username = 'OT' password = '' dsn = 'localhost/pdborcl' port = 1512 encoding = 'UTF-8'
6Code language: Python [python]
username = 'OT' password = '' dsn = 'localhost/pdborcl' port = 1512 encoding = 'UTF-8'
Cuối cùng, thực hiện giao dịch bằng cách sử dụng phương thức
7________số 8
username = 'OT' password = '' dsn = 'localhost/pdborcl' port = 1512 encoding = 'UTF-8'
Code language: Python [python]
Lệnh gọi hàm sau chèn một hàng mới vào bảng
8Code language: Python [python]
username = 'OT' password = '' dsn = 'localhost/pdborcl' port = 1512 encoding = 'UTF-8'
Code language: Python [python]
insert_billing[datetime.now[], 1200, 1, None]
Sau khi thực thi chương trình, bạn có thể truy vấn bảng billing_headers bằng bất kỳ công cụ máy khách Oracle nào như SQL*Plus và SQL Developer
0Code language: Python [python]
username = 'OT' password = '' dsn = 'localhost/pdborcl' port = 1512 encoding = 'UTF-8'
Nó hoạt động như mong đợi
Chèn nhiều hàng vào bảng
Nếu muốn chèn nhiều hàng vào một bảng một lần, bạn có thể sử dụng phương thức
9Code language: Python [python]
username = 'OT' password = '' dsn = 'localhost/pdborcl' port = 1512 encoding = 'UTF-8'
9 hiệu quả hơn gọi phương thức
username = 'OT' password = '' dsn = 'localhost/pdborcl' port = 1512 encoding = 'UTF-8'
Code language: Python [python]
1 nhiều lần vì nó giảm tải mạng và tải cơ sở dữ liệuCode language: Python [python]
connection.commit[]
Đoạn mã sau minh họa cách chèn nhiều hàng vào bảng
8Code language: Python [python]
username = 'OT' password = '' dsn = 'localhost/pdborcl' port = 1512 encoding = 'UTF-8'
4Code language: Python [python]
username = 'OT' password = '' dsn = 'localhost/pdborcl' port = 1512 encoding = 'UTF-8'
Đây là nội dung của bảng
8 sau khi thực hiện chương trìnhCode language: Python [python]
username = 'OT' password = '' dsn = 'localhost/pdborcl' port = 1512 encoding = 'UTF-8'
0Code language: Python [python]
username = 'OT' password = '' dsn = 'localhost/pdborcl' port = 1512 encoding = 'UTF-8'
Trong hướng dẫn này, bạn đã học cách sử dụng các phương thức
4 vàCode language: Python [python]
connection.commit[]
9 để chèn một hoặc nhiều hàng vào một bảng trong Cơ sở dữ liệu Oracle.Code language: Python [python]
username = 'OT' password = '' dsn = 'localhost/pdborcl' port = 1512 encoding = 'UTF-8'