Làm cách nào để tạo một bảng trong python psycopg2?

Việc sử dụng Psycopg cơ bản là phổ biến đối với tất cả các bộ điều hợp cơ sở dữ liệu triển khai API DB 2. 0 giao thức. Đây là một phiên tương tác hiển thị một số lệnh cơ bản

>>> import psycopg2

# Connect to an existing database
>>> conn = psycopg2.connect["dbname=test user=postgres"]

# Open a cursor to perform database operations
>>> cur = conn.cursor[]

# Execute a command: this creates a new table
>>> cur.execute["CREATE TABLE test [id serial PRIMARY KEY, num integer, data varchar];"]

# Pass data to fill a query placeholders and let Psycopg perform
# the correct conversion [no more SQL injections!]
>>> cur.execute["INSERT INTO test [num, data] VALUES [%s, %s]",
..      [100, "abc'def"]]

# Query the database and obtain data as Python objects
>>> cur.execute["SELECT * FROM test;"]
>>> cur.fetchone[]
[1, 100, "abc'def"]

# Make the changes to the database persistent
>>> conn.commit[]

# Close communication with the database
>>> cur.close[]
>>> conn.close[]

Các điểm vào chính của Psycopg là

  • Hàm

    >>> cur.execute["""
    ..     INSERT INTO some_table [an_int, a_date, another_date, a_string]
    ..     VALUES [%[int]s, %[date]s, %[date]s, %[str]s];
    ..     """,
    ..     {'int': 10, 'str': "O'Reilly", 'date': datetime.date[2005, 11, 18]}]
    
    9 tạo phiên cơ sở dữ liệu mới và trả về phiên bản
    >>> cur.execute["SELECT [%s % 2] = 0 AS even", [10,]]       # WRONG
    >>> cur.execute["SELECT [%s %% 2] = 0 AS even", [10,]]      # correct
    
    0 mới

  • Lớp

    >>> cur.execute["SELECT [%s % 2] = 0 AS even", [10,]]       # WRONG
    >>> cur.execute["SELECT [%s %% 2] = 0 AS even", [10,]]      # correct
    
    0 gói gọn một phiên cơ sở dữ liệu. Nó cho phép

    • tạo các phiên bản

      >>> cur.execute["SELECT [%s % 2] = 0 AS even", [10,]]       # WRONG
      >>> cur.execute["SELECT [%s %% 2] = 0 AS even", [10,]]      # correct
      
      2 mới bằng cách sử dụng phương pháp
      >>> cur.execute["SELECT [%s % 2] = 0 AS even", [10,]]       # WRONG
      >>> cur.execute["SELECT [%s %% 2] = 0 AS even", [10,]]      # correct
      
      3 để thực thi các lệnh và truy vấn cơ sở dữ liệu,

    • chấm dứt giao dịch bằng các phương thức

      >>> cur.execute["SELECT [%s % 2] = 0 AS even", [10,]]       # WRONG
      >>> cur.execute["SELECT [%s %% 2] = 0 AS even", [10,]]      # correct
      
      4 hoặc
      >>> cur.execute["SELECT [%s % 2] = 0 AS even", [10,]]       # WRONG
      >>> cur.execute["SELECT [%s %% 2] = 0 AS even", [10,]]      # correct
      
      5

  • Lớp

    >>> cur.execute["SELECT [%s % 2] = 0 AS even", [10,]]       # WRONG
    >>> cur.execute["SELECT [%s %% 2] = 0 AS even", [10,]]      # correct
    
    2 cho phép tương tác với cơ sở dữ liệu

    • gửi lệnh đến cơ sở dữ liệu bằng các phương pháp như

      >>> cur.execute["SELECT [%s % 2] = 0 AS even", [10,]]       # WRONG
      >>> cur.execute["SELECT [%s %% 2] = 0 AS even", [10,]]      # correct
      
      7 và
      >>> cur.execute["SELECT [%s % 2] = 0 AS even", [10,]]       # WRONG
      >>> cur.execute["SELECT [%s %% 2] = 0 AS even", [10,]]      # correct
      
      8,

    • truy xuất dữ liệu từ cơ sở dữ liệu bằng cách lặp lại hoặc sử dụng các phương thức như

      >>> cur.execute["SELECT [%s % 2] = 0 AS even", [10,]]       # WRONG
      >>> cur.execute["SELECT [%s %% 2] = 0 AS even", [10,]]      # correct
      
      9,
      >>> cur.execute["""
      ..     INSERT INTO some_table [an_int, a_date, another_date, a_string]
      ..     VALUES [%[int]s, %[date]s, %[date]s, %[str]s];
      ..     """,
      ..     {'int': 10, 'str': "O'Reilly", 'date': datetime.date[2005, 11, 18]}]
      
      20,
      >>> cur.execute["""
      ..     INSERT INTO some_table [an_int, a_date, another_date, a_string]
      ..     VALUES [%[int]s, %[date]s, %[date]s, %[str]s];
      ..     """,
      ..     {'int': 10, 'str': "O'Reilly", 'date': datetime.date[2005, 11, 18]}]
      
      21.

Truyền tham số cho truy vấn SQL¶

Psycopg chuyển đổi các biến Python thành giá trị SQL bằng cách sử dụng các loại của chúng. loại Python xác định chức năng được sử dụng để chuyển đổi đối tượng thành biểu diễn chuỗi phù hợp với PostgreSQL. Nhiều loại Python tiêu chuẩn đã được điều chỉnh theo cách biểu diễn SQL chính xác

Truyền tham số cho câu lệnh SQL xảy ra trong các hàm chẳng hạn như

>>> cur.execute["""
..     INSERT INTO some_table [an_int, a_date, another_date, a_string]
..     VALUES [%[int]s, %[date]s, %[date]s, %[str]s];
..     """,
..     {'int': 10, 'str': "O'Reilly", 'date': datetime.date[2005, 11, 18]}]
22 bằng cách sử dụng trình giữ chỗ
>>> cur.execute["""
..     INSERT INTO some_table [an_int, a_date, another_date, a_string]
..     VALUES [%[int]s, %[date]s, %[date]s, %[str]s];
..     """,
..     {'int': 10, 'str': "O'Reilly", 'date': datetime.date[2005, 11, 18]}]
23 trong câu lệnh SQL và truyền một chuỗi giá trị làm đối số thứ hai của hàm. Ví dụ: lệnh gọi hàm Python

>>> cur.execute["""
..     INSERT INTO some_table [an_int, a_date, another_date, a_string]
..     VALUES [%[int]s, %[date]s, %[date]s, %[str]s];
..     """,
..     {'int': 10, 'str': "O'Reilly", 'date': datetime.date[2005, 11, 18]}]
6

được chuyển đổi thành một lệnh SQL tương tự như

>>> cur.execute["""
..     INSERT INTO some_table [an_int, a_date, another_date, a_string]
..     VALUES [%[int]s, %[date]s, %[date]s, %[str]s];
..     """,
..     {'int': 10, 'str': "O'Reilly", 'date': datetime.date[2005, 11, 18]}]
7

Các đối số được đặt tên cũng được hỗ trợ bằng cách sử dụng trình giữ chỗ

>>> cur.execute["""
..     INSERT INTO some_table [an_int, a_date, another_date, a_string]
..     VALUES [%[int]s, %[date]s, %[date]s, %[str]s];
..     """,
..     {'int': 10, 'str': "O'Reilly", 'date': datetime.date[2005, 11, 18]}]
24 trong truy vấn và chỉ định các giá trị thành ánh xạ. Sử dụng các đối số được đặt tên cho phép chỉ định các giá trị theo bất kỳ thứ tự nào và lặp lại cùng một giá trị ở một số vị trí trong truy vấn

>>> cur.execute["""
..     INSERT INTO some_table [an_int, a_date, another_date, a_string]
..     VALUES [%[int]s, %[date]s, %[date]s, %[str]s];
..     """,
..     {'int': 10, 'str': "O'Reilly", 'date': datetime.date[2005, 11, 18]}]

Không hỗ trợ sử dụng các ký tự

>>> cur.execute["""
..     INSERT INTO some_table [an_int, a_date, another_date, a_string]
..     VALUES [%[int]s, %[date]s, %[date]s, %[str]s];
..     """,
..     {'int': 10, 'str': "O'Reilly", 'date': datetime.date[2005, 11, 18]}]
25,
>>> cur.execute["""
..     INSERT INTO some_table [an_int, a_date, another_date, a_string]
..     VALUES [%[int]s, %[date]s, %[date]s, %[str]s];
..     """,
..     {'int': 10, 'str': "O'Reilly", 'date': datetime.date[2005, 11, 18]}]
26,
>>> cur.execute["""
..     INSERT INTO some_table [an_int, a_date, another_date, a_string]
..     VALUES [%[int]s, %[date]s, %[date]s, %[str]s];
..     """,
..     {'int': 10, 'str': "O'Reilly", 'date': datetime.date[2005, 11, 18]}]
27 trong tên đối số

Khi các tham số được sử dụng, để bao gồm một ký tự

>>> cur.execute["""
..     INSERT INTO some_table [an_int, a_date, another_date, a_string]
..     VALUES [%[int]s, %[date]s, %[date]s, %[str]s];
..     """,
..     {'int': 10, 'str': "O'Reilly", 'date': datetime.date[2005, 11, 18]}]
25 trong truy vấn, bạn có thể sử dụng chuỗi
>>> cur.execute["""
..     INSERT INTO some_table [an_int, a_date, another_date, a_string]
..     VALUES [%[int]s, %[date]s, %[date]s, %[str]s];
..     """,
..     {'int': 10, 'str': "O'Reilly", 'date': datetime.date[2005, 11, 18]}]
29

>>> cur.execute["SELECT [%s % 2] = 0 AS even", [10,]]       # WRONG
>>> cur.execute["SELECT [%s %% 2] = 0 AS even", [10,]]      # correct

Mặc dù cơ chế này giống với thao tác chuỗi Python thông thường, nhưng có một số khác biệt nhỏ mà bạn nên quan tâm khi truyền tham số cho truy vấn

  • Không được sử dụng toán tử chuỗi Python

    >>> cur.execute["""
    ..     INSERT INTO some_table [an_int, a_date, another_date, a_string]
    ..     VALUES [%[int]s, %[date]s, %[date]s, %[str]s];
    ..     """,
    ..     {'int': 10, 'str': "O'Reilly", 'date': datetime.date[2005, 11, 18]}]
    
    25. phương thức
    >>> cur.execute["SELECT [%s % 2] = 0 AS even", [10,]]       # WRONG
    >>> cur.execute["SELECT [%s %% 2] = 0 AS even", [10,]]      # correct
    
    7 chấp nhận một bộ hoặc từ điển các giá trị làm tham số thứ hai. Không bao giờ sử dụng
    >>> cur.execute["""
    ..     INSERT INTO some_table [an_int, a_date, another_date, a_string]
    ..     VALUES [%[int]s, %[date]s, %[date]s, %[str]s];
    ..     """,
    ..     {'int': 10, 'str': "O'Reilly", 'date': datetime.date[2005, 11, 18]}]
    
    25 hoặc
    >>> cur.execute["SELECT [%s % 2] = 0 AS even", [10,]]       # WRONG
    >>> cur.execute["SELECT [%s %% 2] = 0 AS even", [10,]]      # correct
    
    93 để hợp nhất các giá trị vào truy vấn

    >>> cur.execute["""
    ..     INSERT INTO some_table [an_int, a_date, another_date, a_string]
    ..     VALUES [%[int]s, %[date]s, %[date]s, %[str]s];
    ..     """,
    ..     {'int': 10, 'str': "O'Reilly", 'date': datetime.date[2005, 11, 18]}]
    
    2

  • Đối với liên kết biến vị trí, đối số thứ hai phải luôn là một chuỗi, ngay cả khi nó chứa một biến duy nhất [hãy nhớ rằng Python yêu cầu dấu phẩy để tạo một bộ phần tử đơn lẻ]

    >>> cur.execute["SELECT [%s % 2] = 0 AS even", [10,]]       # WRONG
    >>> cur.execute["SELECT [%s %% 2] = 0 AS even", [10,]]      # correct
    
    9

  • Trình giữ chỗ không được trích dẫn. Psycopg sẽ thêm dấu ngoặc kép khi cần thiết

    >>> cur.execute["SELECT [%s % 2] = 0 AS even", [10,]]       # WRONG
    >>> cur.execute["SELECT [%s %% 2] = 0 AS even", [10,]]      # correct
    
    2

  • Trình giữ chỗ biến phải luôn là ____123, ngay cả khi một trình giữ chỗ khác [chẳng hạn như _

    >>> cur.execute["SELECT [%s % 2] = 0 AS even", [10,]]       # WRONG
    >>> cur.execute["SELECT [%s %% 2] = 0 AS even", [10,]]      # correct
    
    95 cho số nguyên hoặc _
    >>> cur.execute["SELECT [%s % 2] = 0 AS even", [10,]]       # WRONG
    >>> cur.execute["SELECT [%s %% 2] = 0 AS even", [10,]]      # correct
    
    96 cho số thực] có thể phù hợp hơn

    >>> cur.execute["SELECT [%s % 2] = 0 AS even", [10,]]       # WRONG
    >>> cur.execute["SELECT [%s %% 2] = 0 AS even", [10,]]      # correct
    
    6

  • Chỉ các giá trị truy vấn mới được ràng buộc thông qua phương thức này. nó không nên được sử dụng để hợp nhất tên bảng hoặc trường với truy vấn [Psycopg sẽ thử trích dẫn tên bảng dưới dạng giá trị chuỗi, tạo SQL không hợp lệ]. Nếu bạn cần tạo các truy vấn SQL động [ví dụ: chọn tên bảng động], bạn có thể sử dụng các phương tiện do mô-đun

    >>> cur.execute["SELECT [%s % 2] = 0 AS even", [10,]]       # WRONG
    >>> cur.execute["SELECT [%s %% 2] = 0 AS even", [10,]]      # correct
    
    97 cung cấp

    >>> cur.execute["SELECT [%s % 2] = 0 AS even", [10,]]       # WRONG
    >>> cur.execute["SELECT [%s %% 2] = 0 AS even", [10,]]      # correct
    
    8

Vấn đề với các tham số truy vấn¶

Biểu diễn SQL của nhiều loại dữ liệu thường khác với biểu diễn chuỗi Python của chúng. Ví dụ điển hình là với dấu ngoặc đơn trong chuỗi. trong SQL, các dấu ngoặc đơn được sử dụng làm dấu phân cách ký tự chuỗi, do đó, các dấu xuất hiện bên trong chuỗi phải được thoát, trong khi trong Python, dấu ngoặc đơn có thể không được thoát nếu chuỗi được phân tách bằng dấu ngoặc kép

Do sự khác biệt, đôi khi rất tinh tế, giữa các biểu diễn kiểu dữ liệu, một cách tiếp cận ngây thơ đối với thành phần chuỗi truy vấn, chẳng hạn như sử dụng nối chuỗi Python, là một công thức cho các vấn đề khủng khiếp

>>> cur.execute["""
..     INSERT INTO some_table [an_int, a_date, another_date, a_string]
..     VALUES [%[int]s, %[date]s, %[date]s, %[str]s];
..     """,
..     {'int': 10, 'str': "O'Reilly", 'date': datetime.date[2005, 11, 18]}]
60

Nếu các biến chứa dữ liệu để gửi đến cơ sở dữ liệu đến từ một nguồn không đáng tin cậy [chẳng hạn như một biểu mẫu được xuất bản trên một trang web], kẻ tấn công có thể dễ dàng tạo ra một chuỗi không đúng định dạng, giành quyền truy cập vào dữ liệu trái phép hoặc thực hiện các hoạt động phá hoại trên cơ sở dữ liệu. Hình thức tấn công này được gọi là SQL injection và được biết đến là một trong những hình thức tấn công phổ biến nhất đối với các máy chủ cơ sở dữ liệu. Trước khi tiếp tục, vui lòng in trang này dưới dạng bản ghi nhớ và treo nó lên bàn làm việc của bạn

Psycopg có thể tự động chuyển đổi các đối tượng Python sang và từ các ký tự SQL. sử dụng tính năng này, mã của bạn sẽ mạnh mẽ và đáng tin cậy hơn. Chúng ta phải nhấn mạnh điểm này

Cảnh báo

Không bao giờ, không bao giờ, KHÔNG BAO GIỜ sử dụng phép nối chuỗi Python [

>>> cur.execute["SELECT [%s % 2] = 0 AS even", [10,]]       # WRONG
>>> cur.execute["SELECT [%s %% 2] = 0 AS even", [10,]]      # correct
93] hoặc phép nội suy tham số chuỗi [
>>> cur.execute["""
..     INSERT INTO some_table [an_int, a_date, another_date, a_string]
..     VALUES [%[int]s, %[date]s, %[date]s, %[str]s];
..     """,
..     {'int': 10, 'str': "O'Reilly", 'date': datetime.date[2005, 11, 18]}]
25] để truyền biến cho chuỗi truy vấn SQL. Thậm chí không bằng súng

Cách chính xác để truyền biến trong lệnh SQL là sử dụng đối số thứ hai của phương thức

>>> cur.execute["SELECT [%s % 2] = 0 AS even", [10,]]       # WRONG
>>> cur.execute["SELECT [%s %% 2] = 0 AS even", [10,]]      # correct
7

>>> cur.execute["""
..     INSERT INTO some_table [an_int, a_date, another_date, a_string]
..     VALUES [%[int]s, %[date]s, %[date]s, %[str]s];
..     """,
..     {'int': 10, 'str': "O'Reilly", 'date': datetime.date[2005, 11, 18]}]
61

Các giá trị chứa dấu gạch chéo ngược và LIKE¶

Không giống như trong Python, dấu gạch chéo ngược [

>>> cur.execute["SELECT [%s % 2] = 0 AS even", [10,]]       # WRONG
>>> cur.execute["SELECT [%s %% 2] = 0 AS even", [10,]]      # correct
21] không được sử dụng làm ký tự thoát ngoại trừ các mẫu được sử dụng với
>>> cur.execute["SELECT [%s % 2] = 0 AS even", [10,]]       # WRONG
>>> cur.execute["SELECT [%s %% 2] = 0 AS even", [10,]]      # correct
22 và
>>> cur.execute["SELECT [%s % 2] = 0 AS even", [10,]]       # WRONG
>>> cur.execute["SELECT [%s %% 2] = 0 AS even", [10,]]      # correct
23 khi chúng cần thiết để thoát khỏi ký tự
>>> cur.execute["""
..     INSERT INTO some_table [an_int, a_date, another_date, a_string]
..     VALUES [%[int]s, %[date]s, %[date]s, %[str]s];
..     """,
..     {'int': 10, 'str': "O'Reilly", 'date': datetime.date[2005, 11, 18]}]
25 và
>>> cur.execute["SELECT [%s % 2] = 0 AS even", [10,]]       # WRONG
>>> cur.execute["SELECT [%s %% 2] = 0 AS even", [10,]]      # correct
25

Điều này có thể dẫn đến những tình huống khó hiểu

>>> cur.execute["""
..     INSERT INTO some_table [an_int, a_date, another_date, a_string]
..     VALUES [%[int]s, %[date]s, %[date]s, %[str]s];
..     """,
..     {'int': 10, 'str': "O'Reilly", 'date': datetime.date[2005, 11, 18]}]
62

Giải pháp là chỉ định một ký tự

>>> cur.execute["SELECT [%s % 2] = 0 AS even", [10,]]       # WRONG
>>> cur.execute["SELECT [%s %% 2] = 0 AS even", [10,]]      # correct
26 của
>>> cur.execute["SELECT [%s % 2] = 0 AS even", [10,]]       # WRONG
>>> cur.execute["SELECT [%s %% 2] = 0 AS even", [10,]]      # correct
27 [chuỗi trống] trong truy vấn
>>> cur.execute["SELECT [%s % 2] = 0 AS even", [10,]]       # WRONG
>>> cur.execute["SELECT [%s %% 2] = 0 AS even", [10,]]      # correct
22 của bạn

>>> cur.execute["""
..     INSERT INTO some_table [an_int, a_date, another_date, a_string]
..     VALUES [%[int]s, %[date]s, %[date]s, %[str]s];
..     """,
..     {'int': 10, 'str': "O'Reilly", 'date': datetime.date[2005, 11, 18]}]
63

Điều chỉnh các giá trị Python thành các kiểu SQL¶

Nhiều loại Python tiêu chuẩn được điều chỉnh thành SQL và được trả về dưới dạng đối tượng Python khi truy vấn được thực thi

Bảng sau đây hiển thị ánh xạ mặc định giữa các loại Python và PostgreSQL

con trăn

PostgreSQL

Xem thêm

>>> cur.execute["SELECT [%s % 2] = 0 AS even", [10,]]       # WRONG
>>> cur.execute["SELECT [%s %% 2] = 0 AS even", [10,]]      # correct
29

>>> cur.execute["SELECT [%s % 2] = 0 AS even", [10,]]       # WRONG
>>> cur.execute["SELECT [%s %% 2] = 0 AS even", [10,]]      # correct
60

hằng số thích ứng

>>> cur.execute["SELECT [%s % 2] = 0 AS even", [10,]]       # WRONG
>>> cur.execute["SELECT [%s %% 2] = 0 AS even", [10,]]      # correct
61

>>> cur.execute["SELECT [%s % 2] = 0 AS even", [10,]]       # WRONG
>>> cur.execute["SELECT [%s %% 2] = 0 AS even", [10,]]      # correct
61

>>> cur.execute["SELECT [%s % 2] = 0 AS even", [10,]]       # WRONG
>>> cur.execute["SELECT [%s %% 2] = 0 AS even", [10,]]      # correct
63

>>> cur.execute["SELECT [%s % 2] = 0 AS even", [10,]]       # WRONG
>>> cur.execute["SELECT [%s %% 2] = 0 AS even", [10,]]      # correct
64

>>> cur.execute["SELECT [%s % 2] = 0 AS even", [10,]]       # WRONG
>>> cur.execute["SELECT [%s %% 2] = 0 AS even", [10,]]      # correct
65

số thích ứng

>>> cur.execute["SELECT [%s % 2] = 0 AS even", [10,]]       # WRONG
>>> cur.execute["SELECT [%s %% 2] = 0 AS even", [10,]]      # correct
66

>>> cur.execute["SELECT [%s % 2] = 0 AS even", [10,]]       # WRONG
>>> cur.execute["SELECT [%s %% 2] = 0 AS even", [10,]]      # correct
67

>>> cur.execute["SELECT [%s % 2] = 0 AS even", [10,]]       # WRONG
>>> cur.execute["SELECT [%s %% 2] = 0 AS even", [10,]]      # correct
68

>>> cur.execute["SELECT [%s % 2] = 0 AS even", [10,]]       # WRONG
>>> cur.execute["SELECT [%s %% 2] = 0 AS even", [10,]]      # correct
69

>>> cur.execute["SELECT [%s % 2] = 0 AS even", [10,]]       # WRONG
>>> cur.execute["SELECT [%s %% 2] = 0 AS even", [10,]]      # correct
80

>>> cur.execute["SELECT [%s % 2] = 0 AS even", [10,]]       # WRONG
>>> cur.execute["SELECT [%s %% 2] = 0 AS even", [10,]]      # correct
81

>>> cur.execute["SELECT [%s % 2] = 0 AS even", [10,]]       # WRONG
>>> cur.execute["SELECT [%s %% 2] = 0 AS even", [10,]]      # correct
82

>>> cur.execute["SELECT [%s % 2] = 0 AS even", [10,]]       # WRONG
>>> cur.execute["SELECT [%s %% 2] = 0 AS even", [10,]]      # correct
83

>>> cur.execute["SELECT [%s % 2] = 0 AS even", [10,]]       # WRONG
>>> cur.execute["SELECT [%s %% 2] = 0 AS even", [10,]]      # correct
84

>>> cur.execute["SELECT [%s % 2] = 0 AS even", [10,]]       # WRONG
>>> cur.execute["SELECT [%s %% 2] = 0 AS even", [10,]]      # correct
85

>>> cur.execute["SELECT [%s % 2] = 0 AS even", [10,]]       # WRONG
>>> cur.execute["SELECT [%s %% 2] = 0 AS even", [10,]]      # correct
86

Thích ứng chuỗi

>>> cur.execute["SELECT [%s % 2] = 0 AS even", [10,]]       # WRONG
>>> cur.execute["SELECT [%s %% 2] = 0 AS even", [10,]]      # correct
87

>>> cur.execute["SELECT [%s % 2] = 0 AS even", [10,]]       # WRONG
>>> cur.execute["SELECT [%s %% 2] = 0 AS even", [10,]]      # correct
88

>>> cur.execute["SELECT [%s % 2] = 0 AS even", [10,]]       # WRONG
>>> cur.execute["SELECT [%s %% 2] = 0 AS even", [10,]]      # correct
89

>>> cur.execute["""
..     INSERT INTO some_table [an_int, a_date, another_date, a_string]
..     VALUES [%[int]s, %[date]s, %[date]s, %[str]s];
..     """,
..     {'int': 10, 'str': "O'Reilly", 'date': datetime.date[2005, 11, 18]}]
600

giao thức đệm

>>> cur.execute["""
..     INSERT INTO some_table [an_int, a_date, another_date, a_string]
..     VALUES [%[int]s, %[date]s, %[date]s, %[str]s];
..     """,
..     {'int': 10, 'str': "O'Reilly", 'date': datetime.date[2005, 11, 18]}]
601

thích ứng nhị phân

>>> cur.execute["""
..     INSERT INTO some_table [an_int, a_date, another_date, a_string]
..     VALUES [%[int]s, %[date]s, %[date]s, %[str]s];
..     """,
..     {'int': 10, 'str': "O'Reilly", 'date': datetime.date[2005, 11, 18]}]
602

>>> cur.execute["""
..     INSERT INTO some_table [an_int, a_date, another_date, a_string]
..     VALUES [%[int]s, %[date]s, %[date]s, %[str]s];
..     """,
..     {'int': 10, 'str': "O'Reilly", 'date': datetime.date[2005, 11, 18]}]
602

Thích ứng đối tượng ngày/giờ

>>> cur.execute["""
..     INSERT INTO some_table [an_int, a_date, another_date, a_string]
..     VALUES [%[int]s, %[date]s, %[date]s, %[str]s];
..     """,
..     {'int': 10, 'str': "O'Reilly", 'date': datetime.date[2005, 11, 18]}]
604

>>> cur.execute["""
..     INSERT INTO some_table [an_int, a_date, another_date, a_string]
..     VALUES [%[int]s, %[date]s, %[date]s, %[str]s];
..     """,
..     {'int': 10, 'str': "O'Reilly", 'date': datetime.date[2005, 11, 18]}]
604

>>> cur.execute["""
..     INSERT INTO some_table [an_int, a_date, another_date, a_string]
..     VALUES [%[int]s, %[date]s, %[date]s, %[str]s];
..     """,
..     {'int': 10, 'str': "O'Reilly", 'date': datetime.date[2005, 11, 18]}]
606

>>> cur.execute["""
..     INSERT INTO some_table [an_int, a_date, another_date, a_string]
..     VALUES [%[int]s, %[date]s, %[date]s, %[str]s];
..     """,
..     {'int': 10, 'str': "O'Reilly", 'date': datetime.date[2005, 11, 18]}]
607

>>> cur.execute["""
..     INSERT INTO some_table [an_int, a_date, another_date, a_string]
..     VALUES [%[int]s, %[date]s, %[date]s, %[str]s];
..     """,
..     {'int': 10, 'str': "O'Reilly", 'date': datetime.date[2005, 11, 18]}]
608

>>> cur.execute["""
..     INSERT INTO some_table [an_int, a_date, another_date, a_string]
..     VALUES [%[int]s, %[date]s, %[date]s, %[str]s];
..     """,
..     {'int': 10, 'str': "O'Reilly", 'date': datetime.date[2005, 11, 18]}]
609

>>> cur.execute["""
..     INSERT INTO some_table [an_int, a_date, another_date, a_string]
..     VALUES [%[int]s, %[date]s, %[date]s, %[str]s];
..     """,
..     {'int': 10, 'str': "O'Reilly", 'date': datetime.date[2005, 11, 18]}]
610

>>> cur.execute["""
..     INSERT INTO some_table [an_int, a_date, another_date, a_string]
..     VALUES [%[int]s, %[date]s, %[date]s, %[str]s];
..     """,
..     {'int': 10, 'str': "O'Reilly", 'date': datetime.date[2005, 11, 18]}]
611

>>> cur.execute["""
..     INSERT INTO some_table [an_int, a_date, another_date, a_string]
..     VALUES [%[int]s, %[date]s, %[date]s, %[str]s];
..     """,
..     {'int': 10, 'str': "O'Reilly", 'date': datetime.date[2005, 11, 18]}]
612

>>> cur.execute["""
..     INSERT INTO some_table [an_int, a_date, another_date, a_string]
..     VALUES [%[int]s, %[date]s, %[date]s, %[str]s];
..     """,
..     {'int': 10, 'str': "O'Reilly", 'date': datetime.date[2005, 11, 18]}]
613

Danh sách thích ứng

>>> cur.execute["""
..     INSERT INTO some_table [an_int, a_date, another_date, a_string]
..     VALUES [%[int]s, %[date]s, %[date]s, %[str]s];
..     """,
..     {'int': 10, 'str': "O'Reilly", 'date': datetime.date[2005, 11, 18]}]
614

>>> cur.execute["""
..     INSERT INTO some_table [an_int, a_date, another_date, a_string]
..     VALUES [%[int]s, %[date]s, %[date]s, %[str]s];
..     """,
..     {'int': 10, 'str': "O'Reilly", 'date': datetime.date[2005, 11, 18]}]
615

các loại tổng hợp

cú pháp

>>> cur.execute["""
..     INSERT INTO some_table [an_int, a_date, another_date, a_string]
..     VALUES [%[int]s, %[date]s, %[date]s, %[str]s];
..     """,
..     {'int': 10, 'str': "O'Reilly", 'date': datetime.date[2005, 11, 18]}]
616

Thích ứng bộ dữ liệu

Đúc các loại composite

>>> cur.execute["""
..     INSERT INTO some_table [an_int, a_date, another_date, a_string]
..     VALUES [%[int]s, %[date]s, %[date]s, %[str]s];
..     """,
..     {'int': 10, 'str': "O'Reilly", 'date': datetime.date[2005, 11, 18]}]
617

>>> cur.execute["""
..     INSERT INTO some_table [an_int, a_date, another_date, a_string]
..     VALUES [%[int]s, %[date]s, %[date]s, %[str]s];
..     """,
..     {'int': 10, 'str': "O'Reilly", 'date': datetime.date[2005, 11, 18]}]
618

Kiểu dữ liệu Hstore

Nhà ngoại cảm

>>> cur.execute["""
..     INSERT INTO some_table [an_int, a_date, another_date, a_string]
..     VALUES [%[int]s, %[date]s, %[date]s, %[str]s];
..     """,
..     {'int': 10, 'str': "O'Reilly", 'date': datetime.date[2005, 11, 18]}]
619

>>> cur.execute["""
..     INSERT INTO some_table [an_int, a_date, another_date, a_string]
..     VALUES [%[int]s, %[date]s, %[date]s, %[str]s];
..     """,
..     {'int': 10, 'str': "O'Reilly", 'date': datetime.date[2005, 11, 18]}]
620

Loại dữ liệu phạm vi

Bất cứ thứ gì™

>>> cur.execute["""
..     INSERT INTO some_table [an_int, a_date, another_date, a_string]
..     VALUES [%[int]s, %[date]s, %[date]s, %[str]s];
..     """,
..     {'int': 10, 'str': "O'Reilly", 'date': datetime.date[2005, 11, 18]}]
621

thích ứng JSON

>>> cur.execute["""
..     INSERT INTO some_table [an_int, a_date, another_date, a_string]
..     VALUES [%[int]s, %[date]s, %[date]s, %[str]s];
..     """,
..     {'int': 10, 'str': "O'Reilly", 'date': datetime.date[2005, 11, 18]}]
622

>>> cur.execute["""
..     INSERT INTO some_table [an_int, a_date, another_date, a_string]
..     VALUES [%[int]s, %[date]s, %[date]s, %[str]s];
..     """,
..     {'int': 10, 'str': "O'Reilly", 'date': datetime.date[2005, 11, 18]}]
623

kiểu dữ liệu UUID

>>> cur.execute["""
..     INSERT INTO some_table [an_int, a_date, another_date, a_string]
..     VALUES [%[int]s, %[date]s, %[date]s, %[str]s];
..     """,
..     {'int': 10, 'str': "O'Reilly", 'date': datetime.date[2005, 11, 18]}]
624 đối tượng

>>> cur.execute["""
..     INSERT INTO some_table [an_int, a_date, another_date, a_string]
..     VALUES [%[int]s, %[date]s, %[date]s, %[str]s];
..     """,
..     {'int': 10, 'str': "O'Reilly", 'date': datetime.date[2005, 11, 18]}]
625

>>> cur.execute["""
..     INSERT INTO some_table [an_int, a_date, another_date, a_string]
..     VALUES [%[int]s, %[date]s, %[date]s, %[str]s];
..     """,
..     {'int': 10, 'str': "O'Reilly", 'date': datetime.date[2005, 11, 18]}]
626

Các loại dữ liệu mạng

Ánh xạ có thể tùy chỉnh khá tốt. xem Điều chỉnh các loại Python mới thành cú pháp SQLTruyền kiểu của các loại SQL vào các đối tượng Python. You can also find a few other specialized adapters in the

>>> cur.execute["""
..     INSERT INTO some_table [an_int, a_date, another_date, a_string]
..     VALUES [%[int]s, %[date]s, %[date]s, %[str]s];
..     """,
..     {'int': 10, 'str': "O'Reilly", 'date': datetime.date[2005, 11, 18]}]
627 module.

Thích ứng hằng số¶

Python

>>> cur.execute["SELECT [%s % 2] = 0 AS even", [10,]]       # WRONG
>>> cur.execute["SELECT [%s %% 2] = 0 AS even", [10,]]      # correct
29 và các giá trị boolean
>>> cur.execute["""
..     INSERT INTO some_table [an_int, a_date, another_date, a_string]
..     VALUES [%[int]s, %[date]s, %[date]s, %[str]s];
..     """,
..     {'int': 10, 'str': "O'Reilly", 'date': datetime.date[2005, 11, 18]}]
629 và
>>> cur.execute["""
..     INSERT INTO some_table [an_int, a_date, another_date, a_string]
..     VALUES [%[int]s, %[date]s, %[date]s, %[str]s];
..     """,
..     {'int': 10, 'str': "O'Reilly", 'date': datetime.date[2005, 11, 18]}]
630 được chuyển đổi thành các ký tự SQL thích hợp

>>> cur.execute["""
..     INSERT INTO some_table [an_int, a_date, another_date, a_string]
..     VALUES [%[int]s, %[date]s, %[date]s, %[str]s];
..     """,
..     {'int': 10, 'str': "O'Reilly", 'date': datetime.date[2005, 11, 18]}]
64

Thích ứng với số¶

Các đối tượng số Python

>>> cur.execute["SELECT [%s % 2] = 0 AS even", [10,]]       # WRONG
>>> cur.execute["SELECT [%s %% 2] = 0 AS even", [10,]]      # correct
66,
>>> cur.execute["SELECT [%s % 2] = 0 AS even", [10,]]       # WRONG
>>> cur.execute["SELECT [%s %% 2] = 0 AS even", [10,]]      # correct
67,
>>> cur.execute["SELECT [%s % 2] = 0 AS even", [10,]]       # WRONG
>>> cur.execute["SELECT [%s %% 2] = 0 AS even", [10,]]      # correct
63,
>>> cur.execute["SELECT [%s % 2] = 0 AS even", [10,]]       # WRONG
>>> cur.execute["SELECT [%s %% 2] = 0 AS even", [10,]]      # correct
81 được chuyển đổi thành biểu diễn số PostgreSQL

>>> cur.execute["""
..     INSERT INTO some_table [an_int, a_date, another_date, a_string]
..     VALUES [%[int]s, %[date]s, %[date]s, %[str]s];
..     """,
..     {'int': 10, 'str': "O'Reilly", 'date': datetime.date[2005, 11, 18]}]
65

Đọc từ cơ sở dữ liệu, các loại số nguyên được chuyển đổi thành

>>> cur.execute["SELECT [%s % 2] = 0 AS even", [10,]]       # WRONG
>>> cur.execute["SELECT [%s %% 2] = 0 AS even", [10,]]      # correct
66, các loại dấu phẩy động được chuyển đổi thành
>>> cur.execute["SELECT [%s % 2] = 0 AS even", [10,]]       # WRONG
>>> cur.execute["SELECT [%s %% 2] = 0 AS even", [10,]]      # correct
63,
>>> cur.execute["SELECT [%s % 2] = 0 AS even", [10,]]       # WRONG
>>> cur.execute["SELECT [%s %% 2] = 0 AS even", [10,]]      # correct
82/
>>> cur.execute["""
..     INSERT INTO some_table [an_int, a_date, another_date, a_string]
..     VALUES [%[int]s, %[date]s, %[date]s, %[str]s];
..     """,
..     {'int': 10, 'str': "O'Reilly", 'date': datetime.date[2005, 11, 18]}]
638 được chuyển đổi thành
>>> cur.execute["SELECT [%s % 2] = 0 AS even", [10,]]       # WRONG
>>> cur.execute["SELECT [%s %% 2] = 0 AS even", [10,]]      # correct
81

Ghi chú

Đôi khi, bạn có thể muốn nhận dữ liệu

>>> cur.execute["SELECT [%s % 2] = 0 AS even", [10,]]       # WRONG
>>> cur.execute["SELECT [%s %% 2] = 0 AS even", [10,]]      # correct
82 dưới dạng
>>> cur.execute["SELECT [%s % 2] = 0 AS even", [10,]]       # WRONG
>>> cur.execute["SELECT [%s %% 2] = 0 AS even", [10,]]      # correct
63 thay vì lý do hiệu suất hoặc dễ thao tác. bạn có thể định cấu hình bộ điều hợp để truyền số PostgreSQL sang Python float . Điều này tất nhiên có thể ngụ ý mất độ chính xác.

Xem thêm

Các loại số PostgreSQL

Thích ứng chuỗi¶

Python

>>> cur.execute["SELECT [%s % 2] = 0 AS even", [10,]]       # WRONG
>>> cur.execute["SELECT [%s %% 2] = 0 AS even", [10,]]      # correct
83 và
>>> cur.execute["SELECT [%s % 2] = 0 AS even", [10,]]       # WRONG
>>> cur.execute["SELECT [%s %% 2] = 0 AS even", [10,]]      # correct
84 được chuyển thành cú pháp chuỗi SQL. Các đối tượng
>>> cur.execute["SELECT [%s % 2] = 0 AS even", [10,]]       # WRONG
>>> cur.execute["SELECT [%s %% 2] = 0 AS even", [10,]]      # correct
84 [
>>> cur.execute["SELECT [%s % 2] = 0 AS even", [10,]]       # WRONG
>>> cur.execute["SELECT [%s %% 2] = 0 AS even", [10,]]      # correct
83 trong Python 3] được mã hóa trong kết nối
>>> cur.execute["""
..     INSERT INTO some_table [an_int, a_date, another_date, a_string]
..     VALUES [%[int]s, %[date]s, %[date]s, %[str]s];
..     """,
..     {'int': 10, 'str': "O'Reilly", 'date': datetime.date[2005, 11, 18]}]
646 trước khi gửi đến chương trình phụ trợ. cố gắng gửi một ký tự không được mã hóa hỗ trợ sẽ dẫn đến lỗi. Dữ liệu thường được nhận dưới dạng
>>> cur.execute["SELECT [%s % 2] = 0 AS even", [10,]]       # WRONG
>>> cur.execute["SELECT [%s %% 2] = 0 AS even", [10,]]      # correct
83 [i. e. nó được giải mã trên Python 3, mã hóa trái trên Python 2]. Tuy nhiên, cũng có thể nhận được
>>> cur.execute["SELECT [%s % 2] = 0 AS even", [10,]]       # WRONG
>>> cur.execute["SELECT [%s %% 2] = 0 AS even", [10,]]      # correct
84 trên Python 2. xem Xử lý Unicode .

Xử lý Unicode¶

Psycopg có thể trao đổi dữ liệu Unicode với cơ sở dữ liệu PostgreSQL. Các đối tượng

>>> cur.execute["SELECT [%s % 2] = 0 AS even", [10,]]       # WRONG
>>> cur.execute["SELECT [%s %% 2] = 0 AS even", [10,]]      # correct
84 của Python được mã hóa tự động trong mã hóa máy khách được xác định trên kết nối cơ sở dữ liệu [mã hóa PostgreSQL, có sẵn trong
>>> cur.execute["""
..     INSERT INTO some_table [an_int, a_date, another_date, a_string]
..     VALUES [%[int]s, %[date]s, %[date]s, %[str]s];
..     """,
..     {'int': 10, 'str': "O'Reilly", 'date': datetime.date[2005, 11, 18]}]
650, được dịch sang mã hóa Python bằng cách sử dụng ánh xạ
>>> cur.execute["""
..     INSERT INTO some_table [an_int, a_date, another_date, a_string]
..     VALUES [%[int]s, %[date]s, %[date]s, %[str]s];
..     """,
..     {'int': 10, 'str': "O'Reilly", 'date': datetime.date[2005, 11, 18]}]
651]

>>> cur.execute["""
..     INSERT INTO some_table [an_int, a_date, another_date, a_string]
..     VALUES [%[int]s, %[date]s, %[date]s, %[str]s];
..     """,
..     {'int': 10, 'str': "O'Reilly", 'date': datetime.date[2005, 11, 18]}]
66

Khi đọc dữ liệu từ cơ sở dữ liệu, trong Python 2, các chuỗi được trả về thường là các đối tượng 8 bit

>>> cur.execute["SELECT [%s % 2] = 0 AS even", [10,]]       # WRONG
>>> cur.execute["SELECT [%s %% 2] = 0 AS even", [10,]]      # correct
83 được mã hóa trong mã hóa máy khách cơ sở dữ liệu

>>> cur.execute["""
..     INSERT INTO some_table [an_int, a_date, another_date, a_string]
..     VALUES [%[int]s, %[date]s, %[date]s, %[str]s];
..     """,
..     {'int': 10, 'str': "O'Reilly", 'date': datetime.date[2005, 11, 18]}]
67

Trong Python 3, thay vào đó, các chuỗi được giải mã tự động trong kết nối

>>> cur.execute["""
..     INSERT INTO some_table [an_int, a_date, another_date, a_string]
..     VALUES [%[int]s, %[date]s, %[date]s, %[str]s];
..     """,
..     {'int': 10, 'str': "O'Reilly", 'date': datetime.date[2005, 11, 18]}]
646, vì đối tượng
>>> cur.execute["SELECT [%s % 2] = 0 AS even", [10,]]       # WRONG
>>> cur.execute["SELECT [%s %% 2] = 0 AS even", [10,]]      # correct
83 có thể đại diện cho các ký tự Unicode. Trong Python 2, bạn phải đăng ký máy đánh chữ để nhận các đối tượng
>>> cur.execute["SELECT [%s % 2] = 0 AS even", [10,]]       # WRONG
>>> cur.execute["SELECT [%s %% 2] = 0 AS even", [10,]]      # correct
84.

>>> cur.execute["""
..     INSERT INTO some_table [an_int, a_date, another_date, a_string]
..     VALUES [%[int]s, %[date]s, %[date]s, %[str]s];
..     """,
..     {'int': 10, 'str': "O'Reilly", 'date': datetime.date[2005, 11, 18]}]
68

Trong ví dụ trên, máy đánh chữ

>>> cur.execute["""
..     INSERT INTO some_table [an_int, a_date, another_date, a_string]
..     VALUES [%[int]s, %[date]s, %[date]s, %[str]s];
..     """,
..     {'int': 10, 'str': "O'Reilly", 'date': datetime.date[2005, 11, 18]}]
656 chỉ được đăng ký trên con trỏ. Cũng có thể đăng ký máy đánh chữ trên kết nối hoặc trên toàn cầu. xem hàm
>>> cur.execute["""
..     INSERT INTO some_table [an_int, a_date, another_date, a_string]
..     VALUES [%[int]s, %[date]s, %[date]s, %[str]s];
..     """,
..     {'int': 10, 'str': "O'Reilly", 'date': datetime.date[2005, 11, 18]}]
657 và Truyền kiểu SQL vào các đối tượng Python để biết chi tiết.

Ghi chú

Trong Python 2, nếu bạn muốn nhận thống nhất tất cả dữ liệu đầu vào cơ sở dữ liệu của mình bằng Unicode, bạn có thể đăng ký các máy đánh chữ liên quan trên toàn cầu ngay khi Psycopg được nhập

>>> cur.execute["""
..     INSERT INTO some_table [an_int, a_date, another_date, a_string]
..     VALUES [%[int]s, %[date]s, %[date]s, %[str]s];
..     """,
..     {'int': 10, 'str': "O'Reilly", 'date': datetime.date[2005, 11, 18]}]
69

và quên đi câu chuyện này

Ghi chú

Trong một số trường hợp, trên Python 3, bạn có thể muốn nhận

>>> cur.execute["""
..     INSERT INTO some_table [an_int, a_date, another_date, a_string]
..     VALUES [%[int]s, %[date]s, %[date]s, %[str]s];
..     """,
..     {'int': 10, 'str': "O'Reilly", 'date': datetime.date[2005, 11, 18]}]
600 thay vì
>>> cur.execute["SELECT [%s % 2] = 0 AS even", [10,]]       # WRONG
>>> cur.execute["SELECT [%s %% 2] = 0 AS even", [10,]]      # correct
83 mà không cần trải qua bất kỳ quá trình giải mã nào. Điều này đặc biệt xảy ra nếu dữ liệu trong cơ sở dữ liệu ở dạng mã hóa hỗn hợp. Bánh xe đẩy ________ 1660 là thứ bạn cần

>>> cur.execute["""
..     INSERT INTO some_table [an_int, a_date, another_date, a_string]
..     VALUES [%[int]s, %[date]s, %[date]s, %[str]s];
..     """,
..     {'int': 10, 'str': "O'Reilly", 'date': datetime.date[2005, 11, 18]}]
70

Thích ứng nhị phân¶

Các loại Python đại diện cho các đối tượng nhị phân được chuyển đổi thành cú pháp chuỗi nhị phân PostgreSQL, phù hợp với các trường

>>> cur.execute["""
..     INSERT INTO some_table [an_int, a_date, another_date, a_string]
..     VALUES [%[int]s, %[date]s, %[date]s, %[str]s];
..     """,
..     {'int': 10, 'str': "O'Reilly", 'date': datetime.date[2005, 11, 18]}]
601. Các loại như vậy là
>>> cur.execute["SELECT [%s % 2] = 0 AS even", [10,]]       # WRONG
>>> cur.execute["SELECT [%s %% 2] = 0 AS even", [10,]]      # correct
87 [chỉ có trong Python 2],
>>> cur.execute["SELECT [%s % 2] = 0 AS even", [10,]]       # WRONG
>>> cur.execute["SELECT [%s %% 2] = 0 AS even", [10,]]      # correct
88,
>>> cur.execute["SELECT [%s % 2] = 0 AS even", [10,]]       # WRONG
>>> cur.execute["SELECT [%s %% 2] = 0 AS even", [10,]]      # correct
89 và
>>> cur.execute["""
..     INSERT INTO some_table [an_int, a_date, another_date, a_string]
..     VALUES [%[int]s, %[date]s, %[date]s, %[str]s];
..     """,
..     {'int': 10, 'str': "O'Reilly", 'date': datetime.date[2005, 11, 18]}]
600 [chỉ có trong Python 3. tên có sẵn trong Python 2 nhưng nó chỉ là bí danh cho loại
>>> cur.execute["SELECT [%s % 2] = 0 AS even", [10,]]       # WRONG
>>> cur.execute["SELECT [%s %% 2] = 0 AS even", [10,]]      # correct
83]. Bất kỳ đối tượng nào triển khai Giao thức bộ đệm đã sửa đổi đều có thể sử dụng được dưới dạng loại nhị phân. Dữ liệu đã nhận được trả về dưới dạng
>>> cur.execute["SELECT [%s % 2] = 0 AS even", [10,]]       # WRONG
>>> cur.execute["SELECT [%s %% 2] = 0 AS even", [10,]]      # correct
87 [trong Python 2] hoặc
>>> cur.execute["SELECT [%s % 2] = 0 AS even", [10,]]       # WRONG
>>> cur.execute["SELECT [%s %% 2] = 0 AS even", [10,]]      # correct
88 [trong Python 3]

Đã thay đổi trong phiên bản 2. 4. trước đây chỉ hỗ trợ các chuỗi.

Đã thay đổi trong phiên bản 2. 4. 1. có thể phân tích cú pháp định dạng 'hex' từ 9. 0 máy chủ mà không cần dựa vào phiên bản của thư viện máy khách.

Ghi chú

Trong Python 2, nếu bạn có dữ liệu nhị phân trong đối tượng

>>> cur.execute["SELECT [%s % 2] = 0 AS even", [10,]]       # WRONG
>>> cur.execute["SELECT [%s %% 2] = 0 AS even", [10,]]      # correct
83, bạn có thể chuyển chúng vào trường
>>> cur.execute["""
..     INSERT INTO some_table [an_int, a_date, another_date, a_string]
..     VALUES [%[int]s, %[date]s, %[date]s, %[str]s];
..     """,
..     {'int': 10, 'str': "O'Reilly", 'date': datetime.date[2005, 11, 18]}]
601 bằng trình bao bọc
>>> cur.execute["""
..     INSERT INTO some_table [an_int, a_date, another_date, a_string]
..     VALUES [%[int]s, %[date]s, %[date]s, %[str]s];
..     """,
..     {'int': 10, 'str': "O'Reilly", 'date': datetime.date[2005, 11, 18]}]
671

>>> cur.execute["""
..     INSERT INTO some_table [an_int, a_date, another_date, a_string]
..     VALUES [%[int]s, %[date]s, %[date]s, %[str]s];
..     """,
..     {'int': 10, 'str': "O'Reilly", 'date': datetime.date[2005, 11, 18]}]
71

Cảnh báo

Kể từ phiên bản 9. 0 PostgreSQL theo mặc định sử dụng định dạng “hex” mới để phát ra các trường

>>> cur.execute["""
..     INSERT INTO some_table [an_int, a_date, another_date, a_string]
..     VALUES [%[int]s, %[date]s, %[date]s, %[str]s];
..     """,
..     {'int': 10, 'str': "O'Reilly", 'date': datetime.date[2005, 11, 18]}]
601. Bắt đầu từ Psycopg 2. 4. 1 định dạng được hỗ trợ chính xác. Nếu bạn sử dụng phiên bản trước, bạn sẽ cần cẩn thận hơn khi nhận bytea từ PostgreSQL. bạn phải có ít nhất libpq 9. 0 được cài đặt trên máy khách hoặc cách khác, bạn có thể đặt tham số cấu hình bytea_output thành
>>> cur.execute["""
..     INSERT INTO some_table [an_int, a_date, another_date, a_string]
..     VALUES [%[int]s, %[date]s, %[date]s, %[str]s];
..     """,
..     {'int': 10, 'str': "O'Reilly", 'date': datetime.date[2005, 11, 18]}]
673, trong tệp cấu hình máy chủ hoặc trong phiên máy khách [sử dụng truy vấn như
>>> cur.execute["""
..     INSERT INTO some_table [an_int, a_date, another_date, a_string]
..     VALUES [%[int]s, %[date]s, %[date]s, %[str]s];
..     """,
..     {'int': 10, 'str': "O'Reilly", 'date': datetime.date[2005, 11, 18]}]
674] trước khi nhận dữ liệu nhị phân

Thích ứng đối tượng Ngày/Giờ¶

Các nội dung dựng sẵn của Python

>>> cur.execute["""
..     INSERT INTO some_table [an_int, a_date, another_date, a_string]
..     VALUES [%[int]s, %[date]s, %[date]s, %[str]s];
..     """,
..     {'int': 10, 'str': "O'Reilly", 'date': datetime.date[2005, 11, 18]}]
607,
>>> cur.execute["""
..     INSERT INTO some_table [an_int, a_date, another_date, a_string]
..     VALUES [%[int]s, %[date]s, %[date]s, %[str]s];
..     """,
..     {'int': 10, 'str': "O'Reilly", 'date': datetime.date[2005, 11, 18]}]
602,
>>> cur.execute["""
..     INSERT INTO some_table [an_int, a_date, another_date, a_string]
..     VALUES [%[int]s, %[date]s, %[date]s, %[str]s];
..     """,
..     {'int': 10, 'str': "O'Reilly", 'date': datetime.date[2005, 11, 18]}]
604,
>>> cur.execute["""
..     INSERT INTO some_table [an_int, a_date, another_date, a_string]
..     VALUES [%[int]s, %[date]s, %[date]s, %[str]s];
..     """,
..     {'int': 10, 'str': "O'Reilly", 'date': datetime.date[2005, 11, 18]}]
610 được chuyển đổi thành các kiểu dữ liệu
>>> cur.execute["""
..     INSERT INTO some_table [an_int, a_date, another_date, a_string]
..     VALUES [%[int]s, %[date]s, %[date]s, %[str]s];
..     """,
..     {'int': 10, 'str': "O'Reilly", 'date': datetime.date[2005, 11, 18]}]
679,
>>> cur.execute["""
..     INSERT INTO some_table [an_int, a_date, another_date, a_string]
..     VALUES [%[int]s, %[date]s, %[date]s, %[str]s];
..     """,
..     {'int': 10, 'str': "O'Reilly", 'date': datetime.date[2005, 11, 18]}]
602,
>>> cur.execute["""
..     INSERT INTO some_table [an_int, a_date, another_date, a_string]
..     VALUES [%[int]s, %[date]s, %[date]s, %[str]s];
..     """,
..     {'int': 10, 'str': "O'Reilly", 'date': datetime.date[2005, 11, 18]}]
681,
>>> cur.execute["""
..     INSERT INTO some_table [an_int, a_date, another_date, a_string]
..     VALUES [%[int]s, %[date]s, %[date]s, %[str]s];
..     """,
..     {'int': 10, 'str': "O'Reilly", 'date': datetime.date[2005, 11, 18]}]
611 của PostgreSQL. Múi giờ cũng được hỗ trợ

>>> cur.execute["""
..     INSERT INTO some_table [an_int, a_date, another_date, a_string]
..     VALUES [%[int]s, %[date]s, %[date]s, %[str]s];
..     """,
..     {'int': 10, 'str': "O'Reilly", 'date': datetime.date[2005, 11, 18]}]
72

>>> cur.execute["""
..     INSERT INTO some_table [an_int, a_date, another_date, a_string]
..     VALUES [%[int]s, %[date]s, %[date]s, %[str]s];
..     """,
..     {'int': 10, 'str': "O'Reilly", 'date': datetime.date[2005, 11, 18]}]
73

>>> cur.execute["""
..     INSERT INTO some_table [an_int, a_date, another_date, a_string]
..     VALUES [%[int]s, %[date]s, %[date]s, %[str]s];
..     """,
..     {'int': 10, 'str': "O'Reilly", 'date': datetime.date[2005, 11, 18]}]
74

Xem thêm

Các loại ngày/giờ PostgreSQL

Xử lý múi giờ¶

Loại PostgreSQL

>>> cur.execute["""
..     INSERT INTO some_table [an_int, a_date, another_date, a_string]
..     VALUES [%[int]s, %[date]s, %[date]s, %[str]s];
..     """,
..     {'int': 10, 'str': "O'Reilly", 'date': datetime.date[2005, 11, 18]}]
683 [a. k. a.
>>> cur.execute["""
..     INSERT INTO some_table [an_int, a_date, another_date, a_string]
..     VALUES [%[int]s, %[date]s, %[date]s, %[str]s];
..     """,
..     {'int': 10, 'str': "O'Reilly", 'date': datetime.date[2005, 11, 18]}]
609] được chuyển đổi thành các đối tượng Python
>>> cur.execute["""
..     INSERT INTO some_table [an_int, a_date, another_date, a_string]
..     VALUES [%[int]s, %[date]s, %[date]s, %[str]s];
..     """,
..     {'int': 10, 'str': "O'Reilly", 'date': datetime.date[2005, 11, 18]}]
607

>>> cur.execute["""
..     INSERT INTO some_table [an_int, a_date, another_date, a_string]
..     VALUES [%[int]s, %[date]s, %[date]s, %[str]s];
..     """,
..     {'int': 10, 'str': "O'Reilly", 'date': datetime.date[2005, 11, 18]}]
75

Ghi chú

Trước Python 3. 7, mô-đun

>>> cur.execute["""
..     INSERT INTO some_table [an_int, a_date, another_date, a_string]
..     VALUES [%[int]s, %[date]s, %[date]s, %[str]s];
..     """,
..     {'int': 10, 'str': "O'Reilly", 'date': datetime.date[2005, 11, 18]}]
607 chỉ hỗ trợ các múi giờ có số phút là số nguyên. Một vài múi giờ lịch sử có giây ở phần bù UTC. các múi giờ này sẽ có độ lệch được làm tròn đến phút gần nhất, với sai số lên tới 30 giây, trên các phiên bản Python trước 3. 7

>>> cur.execute["""
..     INSERT INTO some_table [an_int, a_date, another_date, a_string]
..     VALUES [%[int]s, %[date]s, %[date]s, %[str]s];
..     """,
..     {'int': 10, 'str': "O'Reilly", 'date': datetime.date[2005, 11, 18]}]
76

Đã thay đổi trong phiên bản 2. 2. 2. các múi giờ tính theo giây được hỗ trợ [có làm tròn]. Trước đây các múi giờ như vậy đã gây ra lỗi.

Đã thay đổi trong phiên bản 2. 9. các múi giờ có giây được hỗ trợ mà không cần làm tròn.

Đã thay đổi trong phiên bản 2. 9. sử dụng

>>> cur.execute["""
..     INSERT INTO some_table [an_int, a_date, another_date, a_string]
..     VALUES [%[int]s, %[date]s, %[date]s, %[str]s];
..     """,
..     {'int': 10, 'str': "O'Reilly", 'date': datetime.date[2005, 11, 18]}]
687 làm đối tượng tzinfo mặc định thay vì
>>> cur.execute["""
..     INSERT INTO some_table [an_int, a_date, another_date, a_string]
..     VALUES [%[int]s, %[date]s, %[date]s, %[str]s];
..     """,
..     {'int': 10, 'str': "O'Reilly", 'date': datetime.date[2005, 11, 18]}]
688.

Xử lý ngày vô hạn¶

PostgreSQL có thể lưu trữ biểu diễn của ngày, dấu thời gian hoặc khoảng thời gian “vô hạn”. Ngày vô hạn không có sẵn cho Python, vì vậy các đối tượng này được ánh xạ tới

>>> cur.execute["""
..     INSERT INTO some_table [an_int, a_date, another_date, a_string]
..     VALUES [%[int]s, %[date]s, %[date]s, %[str]s];
..     """,
..     {'int': 10, 'str': "O'Reilly", 'date': datetime.date[2005, 11, 18]}]
689,
>>> cur.execute["""
..     INSERT INTO some_table [an_int, a_date, another_date, a_string]
..     VALUES [%[int]s, %[date]s, %[date]s, %[str]s];
..     """,
..     {'int': 10, 'str': "O'Reilly", 'date': datetime.date[2005, 11, 18]}]
690,
>>> cur.execute["""
..     INSERT INTO some_table [an_int, a_date, another_date, a_string]
..     VALUES [%[int]s, %[date]s, %[date]s, %[str]s];
..     """,
..     {'int': 10, 'str': "O'Reilly", 'date': datetime.date[2005, 11, 18]}]
691. Thật không may, ánh xạ không thể theo hai chiều nên những ngày này sẽ được lưu lại vào cơ sở dữ liệu với các giá trị của chúng, chẳng hạn như
>>> cur.execute["""
..     INSERT INTO some_table [an_int, a_date, another_date, a_string]
..     VALUES [%[int]s, %[date]s, %[date]s, %[str]s];
..     """,
..     {'int': 10, 'str': "O'Reilly", 'date': datetime.date[2005, 11, 18]}]
692

Chẳng hạn, có thể tạo một bộ điều hợp thay thế cho ngày tháng và các đối tượng khác để ánh xạ

>>> cur.execute["""
..     INSERT INTO some_table [an_int, a_date, another_date, a_string]
..     VALUES [%[int]s, %[date]s, %[date]s, %[str]s];
..     """,
..     {'int': 10, 'str': "O'Reilly", 'date': datetime.date[2005, 11, 18]}]
689 sang
>>> cur.execute["""
..     INSERT INTO some_table [an_int, a_date, another_date, a_string]
..     VALUES [%[int]s, %[date]s, %[date]s, %[str]s];
..     """,
..     {'int': 10, 'str': "O'Reilly", 'date': datetime.date[2005, 11, 18]}]
694

>>> cur.execute["""
..     INSERT INTO some_table [an_int, a_date, another_date, a_string]
..     VALUES [%[int]s, %[date]s, %[date]s, %[str]s];
..     """,
..     {'int': 10, 'str': "O'Reilly", 'date': datetime.date[2005, 11, 18]}]
77

Tất nhiên sẽ không thể ghi giá trị của

>>> cur.execute["""
..     INSERT INTO some_table [an_int, a_date, another_date, a_string]
..     VALUES [%[int]s, %[date]s, %[date]s, %[str]s];
..     """,
..     {'int': 10, 'str': "O'Reilly", 'date': datetime.date[2005, 11, 18]}]
689 vào cơ sở dữ liệu nữa.
>>> cur.execute["""
..     INSERT INTO some_table [an_int, a_date, another_date, a_string]
..     VALUES [%[int]s, %[date]s, %[date]s, %[str]s];
..     """,
..     {'int': 10, 'str': "O'Reilly", 'date': datetime.date[2005, 11, 18]}]
694 sẽ được lưu trữ thay thế

Xử lý thời gian¶

Các loại PostgreSQL

>>> cur.execute["""
..     INSERT INTO some_table [an_int, a_date, another_date, a_string]
..     VALUES [%[int]s, %[date]s, %[date]s, %[str]s];
..     """,
..     {'int': 10, 'str': "O'Reilly", 'date': datetime.date[2005, 11, 18]}]
604 và Python
>>> cur.execute["""
..     INSERT INTO some_table [an_int, a_date, another_date, a_string]
..     VALUES [%[int]s, %[date]s, %[date]s, %[str]s];
..     """,
..     {'int': 10, 'str': "O'Reilly", 'date': datetime.date[2005, 11, 18]}]
604 không hoàn toàn hai chiều

Trong PostgreSQL, giá trị tối đa của loại

>>> cur.execute["""
..     INSERT INTO some_table [an_int, a_date, another_date, a_string]
..     VALUES [%[int]s, %[date]s, %[date]s, %[str]s];
..     """,
..     {'int': 10, 'str': "O'Reilly", 'date': datetime.date[2005, 11, 18]}]
604 là
>>> cur.execute["""
..     INSERT INTO some_table [an_int, a_date, another_date, a_string]
..     VALUES [%[int]s, %[date]s, %[date]s, %[str]s];
..     """,
..     {'int': 10, 'str': "O'Reilly", 'date': datetime.date[2005, 11, 18]}]
700 được coi là muộn hơn 24 giờ so với giá trị tối thiểu của
>>> cur.execute["""
..     INSERT INTO some_table [an_int, a_date, another_date, a_string]
..     VALUES [%[int]s, %[date]s, %[date]s, %[str]s];
..     """,
..     {'int': 10, 'str': "O'Reilly", 'date': datetime.date[2005, 11, 18]}]
701

>>> cur.execute["""
..     INSERT INTO some_table [an_int, a_date, another_date, a_string]
..     VALUES [%[int]s, %[date]s, %[date]s, %[str]s];
..     """,
..     {'int': 10, 'str': "O'Reilly", 'date': datetime.date[2005, 11, 18]}]
78

Tuy nhiên,

>>> cur.execute["""
..     INSERT INTO some_table [an_int, a_date, another_date, a_string]
..     VALUES [%[int]s, %[date]s, %[date]s, %[str]s];
..     """,
..     {'int': 10, 'str': "O'Reilly", 'date': datetime.date[2005, 11, 18]}]
604 của Python chỉ hỗ trợ thời gian cho đến khi
>>> cur.execute["""
..     INSERT INTO some_table [an_int, a_date, another_date, a_string]
..     VALUES [%[int]s, %[date]s, %[date]s, %[str]s];
..     """,
..     {'int': 10, 'str': "O'Reilly", 'date': datetime.date[2005, 11, 18]}]
703. Lấy một giá trị của
>>> cur.execute["""
..     INSERT INTO some_table [an_int, a_date, another_date, a_string]
..     VALUES [%[int]s, %[date]s, %[date]s, %[str]s];
..     """,
..     {'int': 10, 'str': "O'Reilly", 'date': datetime.date[2005, 11, 18]}]
700 dẫn đến một
>>> cur.execute["""
..     INSERT INTO some_table [an_int, a_date, another_date, a_string]
..     VALUES [%[int]s, %[date]s, %[date]s, %[str]s];
..     """,
..     {'int': 10, 'str': "O'Reilly", 'date': datetime.date[2005, 11, 18]}]
604 của
>>> cur.execute["""
..     INSERT INTO some_table [an_int, a_date, another_date, a_string]
..     VALUES [%[int]s, %[date]s, %[date]s, %[str]s];
..     """,
..     {'int': 10, 'str': "O'Reilly", 'date': datetime.date[2005, 11, 18]}]
701

>>> cur.execute["""
..     INSERT INTO some_table [an_int, a_date, another_date, a_string]
..     VALUES [%[int]s, %[date]s, %[date]s, %[str]s];
..     """,
..     {'int': 10, 'str': "O'Reilly", 'date': datetime.date[2005, 11, 18]}]
79

Danh sách thích ứng¶

Danh sách Python được chuyển đổi thành PostgreSQL

>>> cur.execute["""
..     INSERT INTO some_table [an_int, a_date, another_date, a_string]
..     VALUES [%[int]s, %[date]s, %[date]s, %[str]s];
..     """,
..     {'int': 10, 'str': "O'Reilly", 'date': datetime.date[2005, 11, 18]}]
613s

>>> cur.execute["""
..     INSERT INTO some_table [an_int, a_date, another_date, a_string]
..     VALUES [%[int]s, %[date]s, %[date]s, %[str]s];
..     """,
..     {'int': 10, 'str': "O'Reilly", 'date': datetime.date[2005, 11, 18]}]
0

Ghi chú

Bạn có thể sử dụng danh sách Python làm đối số của toán tử

>>> cur.execute["""
..     INSERT INTO some_table [an_int, a_date, another_date, a_string]
..     VALUES [%[int]s, %[date]s, %[date]s, %[str]s];
..     """,
..     {'int': 10, 'str': "O'Reilly", 'date': datetime.date[2005, 11, 18]}]
616 bằng cách sử dụng toán tử ANY của PostgreSQL

>>> cur.execute["""
..     INSERT INTO some_table [an_int, a_date, another_date, a_string]
..     VALUES [%[int]s, %[date]s, %[date]s, %[str]s];
..     """,
..     {'int': 10, 'str': "O'Reilly", 'date': datetime.date[2005, 11, 18]}]
1

Hơn nữa,

>>> cur.execute["""
..     INSERT INTO some_table [an_int, a_date, another_date, a_string]
..     VALUES [%[int]s, %[date]s, %[date]s, %[str]s];
..     """,
..     {'int': 10, 'str': "O'Reilly", 'date': datetime.date[2005, 11, 18]}]
709 cũng có thể hoạt động với danh sách trống, trong khi đó,
>>> cur.execute["""
..     INSERT INTO some_table [an_int, a_date, another_date, a_string]
..     VALUES [%[int]s, %[date]s, %[date]s, %[str]s];
..     """,
..     {'int': 10, 'str': "O'Reilly", 'date': datetime.date[2005, 11, 18]}]
710 là lỗi cú pháp SQL

Ghi chú

Đọc lại từ PostgreSQL, các mảng được chuyển đổi thành danh sách các đối tượng Python như mong đợi, nhưng chỉ khi các mục thuộc loại đã biết. Mảng của các loại không xác định được trả về như được đại diện bởi cơ sở dữ liệu [e. g.

>>> cur.execute["""
..     INSERT INTO some_table [an_int, a_date, another_date, a_string]
..     VALUES [%[int]s, %[date]s, %[date]s, %[str]s];
..     """,
..     {'int': 10, 'str': "O'Reilly", 'date': datetime.date[2005, 11, 18]}]
711]. Nếu bạn muốn chuyển đổi các mục thành các đối tượng Python, bạn có thể dễ dàng tạo một trình đánh chữ cho mảng có kiểu không xác định .

Thích ứng bộ dữ liệu¶

Các bộ dữ liệu Python được chuyển đổi thành một cú pháp phù hợp với toán tử SQL

>>> cur.execute["""
..     INSERT INTO some_table [an_int, a_date, another_date, a_string]
..     VALUES [%[int]s, %[date]s, %[date]s, %[str]s];
..     """,
..     {'int': 10, 'str': "O'Reilly", 'date': datetime.date[2005, 11, 18]}]
616 và để biểu thị một loại hỗn hợp

>>> cur.execute["""
..     INSERT INTO some_table [an_int, a_date, another_date, a_string]
..     VALUES [%[int]s, %[date]s, %[date]s, %[str]s];
..     """,
..     {'int': 10, 'str': "O'Reilly", 'date': datetime.date[2005, 11, 18]}]
2

Ghi chú

SQL không cho phép danh sách trống trong toán tử

>>> cur.execute["""
..     INSERT INTO some_table [an_int, a_date, another_date, a_string]
..     VALUES [%[int]s, %[date]s, %[date]s, %[str]s];
..     """,
..     {'int': 10, 'str': "O'Reilly", 'date': datetime.date[2005, 11, 18]}]
616, vì vậy mã của bạn phải đề phòng các bộ dữ liệu trống. Ngoài ra, bạn có thể sử dụng danh sách Python .

Nếu bạn muốn các loại hỗn hợp PostgreSQL được chuyển đổi thành một Tuple/namedtuple trong Python, bạn có thể sử dụng hàm

>>> cur.execute["""
..     INSERT INTO some_table [an_int, a_date, another_date, a_string]
..     VALUES [%[int]s, %[date]s, %[date]s, %[str]s];
..     """,
..     {'int': 10, 'str': "O'Reilly", 'date': datetime.date[2005, 11, 18]}]
714

Mới trong phiên bản 2. 0. 6. tuple

>>> cur.execute["""
..     INSERT INTO some_table [an_int, a_date, another_date, a_string]
..     VALUES [%[int]s, %[date]s, %[date]s, %[str]s];
..     """,
..     {'int': 10, 'str': "O'Reilly", 'date': datetime.date[2005, 11, 18]}]
616 thích ứng.

Đã thay đổi trong phiên bản 2. 0. 14. bộ điều hợp bộ dữ liệu

>>> cur.execute["""
..     INSERT INTO some_table [an_int, a_date, another_date, a_string]
..     VALUES [%[int]s, %[date]s, %[date]s, %[str]s];
..     """,
..     {'int': 10, 'str': "O'Reilly", 'date': datetime.date[2005, 11, 18]}]
616 luôn hoạt động. Trong các bản phát hành trước, cần phải nhập mô-đun
>>> cur.execute["""
..     INSERT INTO some_table [an_int, a_date, another_date, a_string]
..     VALUES [%[int]s, %[date]s, %[date]s, %[str]s];
..     """,
..     {'int': 10, 'str': "O'Reilly", 'date': datetime.date[2005, 11, 18]}]
717 để đăng ký.

Đã thay đổi trong phiên bản 2. 3. ______1615 các phiên bản được điều chỉnh giống như các bộ dữ liệu thông thường và do đó có thể được sử dụng để biểu diễn các loại tổng hợp.

Kiểm soát giao dịch¶

Trong giao dịch Psycopg được xử lý bởi lớp

>>> cur.execute["SELECT [%s % 2] = 0 AS even", [10,]]       # WRONG
>>> cur.execute["SELECT [%s %% 2] = 0 AS even", [10,]]      # correct
0. Theo mặc định, lần đầu tiên một lệnh được gửi đến cơ sở dữ liệu [sử dụng một trong các
>>> cur.execute["SELECT [%s % 2] = 0 AS even", [10,]]       # WRONG
>>> cur.execute["SELECT [%s %% 2] = 0 AS even", [10,]]      # correct
2 được tạo bởi kết nối], một giao dịch mới sẽ được tạo. Các lệnh cơ sở dữ liệu sau đây sẽ được thực thi trong ngữ cảnh của cùng một giao dịch – không chỉ các lệnh được đưa ra bởi con trỏ đầu tiên, mà cả các lệnh được đưa ra bởi tất cả các con trỏ được tạo bởi cùng một kết nối. Nếu bất kỳ lệnh nào không thành công, giao dịch sẽ bị hủy bỏ và không có lệnh nào khác sẽ được thực hiện cho đến khi gọi phương thức
>>> cur.execute["SELECT [%s % 2] = 0 AS even", [10,]]       # WRONG
>>> cur.execute["SELECT [%s %% 2] = 0 AS even", [10,]]      # correct
5

Kết nối chịu trách nhiệm chấm dứt giao dịch của nó, gọi phương thức

>>> cur.execute["SELECT [%s % 2] = 0 AS even", [10,]]       # WRONG
>>> cur.execute["SELECT [%s %% 2] = 0 AS even", [10,]]      # correct
4 hoặc
>>> cur.execute["SELECT [%s % 2] = 0 AS even", [10,]]       # WRONG
>>> cur.execute["SELECT [%s %% 2] = 0 AS even", [10,]]      # correct
5. Các thay đổi đã cam kết ngay lập tức được thực hiện liên tục trong cơ sở dữ liệu. Nếu kết nối bị đóng [sử dụng phương pháp
>>> cur.execute["""
..     INSERT INTO some_table [an_int, a_date, another_date, a_string]
..     VALUES [%[int]s, %[date]s, %[date]s, %[str]s];
..     """,
..     {'int': 10, 'str': "O'Reilly", 'date': datetime.date[2005, 11, 18]}]
724] hoặc bị hủy [sử dụng
>>> cur.execute["""
..     INSERT INTO some_table [an_int, a_date, another_date, a_string]
..     VALUES [%[int]s, %[date]s, %[date]s, %[str]s];
..     """,
..     {'int': 10, 'str': "O'Reilly", 'date': datetime.date[2005, 11, 18]}]
725 hoặc bằng cách để nó nằm ngoài phạm vi] trong khi giao dịch đang diễn ra, máy chủ sẽ hủy giao dịch. Tuy nhiên làm như vậy là không nên. phần mềm trung gian như PgBouncer có thể thấy kết nối bị đóng không sạch sẽ và loại bỏ nó

Có thể thiết lập kết nối ở chế độ autocommit. theo cách này, tất cả các lệnh được thực thi sẽ được thực hiện ngay lập tức và không thể quay ngược lại. Một vài lệnh [e. g.

>>> cur.execute["""
..     INSERT INTO some_table [an_int, a_date, another_date, a_string]
..     VALUES [%[int]s, %[date]s, %[date]s, %[str]s];
..     """,
..     {'int': 10, 'str': "O'Reilly", 'date': datetime.date[2005, 11, 18]}]
726,
>>> cur.execute["""
..     INSERT INTO some_table [an_int, a_date, another_date, a_string]
..     VALUES [%[int]s, %[date]s, %[date]s, %[str]s];
..     """,
..     {'int': 10, 'str': "O'Reilly", 'date': datetime.date[2005, 11, 18]}]
727,
>>> cur.execute["""
..     INSERT INTO some_table [an_int, a_date, another_date, a_string]
..     VALUES [%[int]s, %[date]s, %[date]s, %[str]s];
..     """,
..     {'int': 10, 'str': "O'Reilly", 'date': datetime.date[2005, 11, 18]}]
728 trên các thủ tục được lưu trữ sử dụng kiểm soát giao dịch…] yêu cầu được chạy bên ngoài bất kỳ giao dịch nào. để có thể chạy các lệnh này từ Psycopg, kết nối phải ở chế độ tự động cam kết. bạn có thể sử dụng tài sản
>>> cur.execute["""
..     INSERT INTO some_table [an_int, a_date, another_date, a_string]
..     VALUES [%[int]s, %[date]s, %[date]s, %[str]s];
..     """,
..     {'int': 10, 'str': "O'Reilly", 'date': datetime.date[2005, 11, 18]}]
729

Cảnh báo

Theo mặc định, ngay cả một

>>> cur.execute["""
..     INSERT INTO some_table [an_int, a_date, another_date, a_string]
..     VALUES [%[int]s, %[date]s, %[date]s, %[str]s];
..     """,
..     {'int': 10, 'str': "O'Reilly", 'date': datetime.date[2005, 11, 18]}]
730 đơn giản cũng sẽ bắt đầu một giao dịch. trong các chương trình chạy dài, nếu không có thêm hành động nào được thực hiện, phiên sẽ vẫn ở trạng thái "không hoạt động trong giao dịch", một điều kiện không mong muốn vì một số lý do [phiên bị khóa, bảng phình to...]. Đối với các tập lệnh tồn tại lâu dài, hãy đảm bảo chấm dứt giao dịch càng sớm càng tốt hoặc sử dụng kết nối tự động xác nhận

Một số thuộc tính giao dịch khác có thể được đặt trên toàn phiên bởi

>>> cur.execute["SELECT [%s % 2] = 0 AS even", [10,]]       # WRONG
>>> cur.execute["SELECT [%s %% 2] = 0 AS even", [10,]]      # correct
0. chẳng hạn, có thể có các giao dịch chỉ đọc hoặc thay đổi mức cô lập. Xem phương pháp
>>> cur.execute["""
..     INSERT INTO some_table [an_int, a_date, another_date, a_string]
..     VALUES [%[int]s, %[date]s, %[date]s, %[str]s];
..     """,
..     {'int': 10, 'str': "O'Reilly", 'date': datetime.date[2005, 11, 18]}]
732 để biết tất cả các chi tiết

Tuyên bố
>>> cur.execute["""
..     INSERT INTO some_table [an_int, a_date, another_date, a_string]
..     VALUES [%[int]s, %[date]s, %[date]s, %[str]s];
..     """,
..     {'int': 10, 'str': "O'Reilly", 'date': datetime.date[2005, 11, 18]}]
733¶

Bắt đầu từ phiên bản 2. 5, các kết nối và con trỏ của psycopg2 là trình quản lý ngữ cảnh và có thể được sử dụng với câu lệnh

>>> cur.execute["""
..     INSERT INTO some_table [an_int, a_date, another_date, a_string]
..     VALUES [%[int]s, %[date]s, %[date]s, %[str]s];
..     """,
..     {'int': 10, 'str': "O'Reilly", 'date': datetime.date[2005, 11, 18]}]
733

>>> cur.execute["""
..     INSERT INTO some_table [an_int, a_date, another_date, a_string]
..     VALUES [%[int]s, %[date]s, %[date]s, %[str]s];
..     """,
..     {'int': 10, 'str': "O'Reilly", 'date': datetime.date[2005, 11, 18]}]
3

Khi một kết nối thoát khỏi khối

>>> cur.execute["""
..     INSERT INTO some_table [an_int, a_date, another_date, a_string]
..     VALUES [%[int]s, %[date]s, %[date]s, %[str]s];
..     """,
..     {'int': 10, 'str': "O'Reilly", 'date': datetime.date[2005, 11, 18]}]
733, nếu không có ngoại lệ nào được đưa ra bởi khối, giao dịch được thực hiện. Trong trường hợp ngoại lệ, giao dịch được khôi phục

Khi một con trỏ thoát khỏi khối

>>> cur.execute["""
..     INSERT INTO some_table [an_int, a_date, another_date, a_string]
..     VALUES [%[int]s, %[date]s, %[date]s, %[str]s];
..     """,
..     {'int': 10, 'str': "O'Reilly", 'date': datetime.date[2005, 11, 18]}]
733, nó sẽ đóng lại, giải phóng mọi tài nguyên cuối cùng được liên kết với nó. Trạng thái của giao dịch không bị ảnh hưởng

Một kết nối có thể được sử dụng trong nhiều hơn một câu lệnh

>>> cur.execute["""
..     INSERT INTO some_table [an_int, a_date, another_date, a_string]
..     VALUES [%[int]s, %[date]s, %[date]s, %[str]s];
..     """,
..     {'int': 10, 'str': "O'Reilly", 'date': datetime.date[2005, 11, 18]}]
733 và mỗi khối
>>> cur.execute["""
..     INSERT INTO some_table [an_int, a_date, another_date, a_string]
..     VALUES [%[int]s, %[date]s, %[date]s, %[str]s];
..     """,
..     {'int': 10, 'str': "O'Reilly", 'date': datetime.date[2005, 11, 18]}]
733 được bao bọc một cách hiệu quả trong một giao dịch riêng biệt

>>> cur.execute["""
..     INSERT INTO some_table [an_int, a_date, another_date, a_string]
..     VALUES [%[int]s, %[date]s, %[date]s, %[str]s];
..     """,
..     {'int': 10, 'str': "O'Reilly", 'date': datetime.date[2005, 11, 18]}]
4

Cảnh báo

Không giống như các đối tượng tệp hoặc các tài nguyên khác, việc thoát khỏi khối

>>> cur.execute["""
..     INSERT INTO some_table [an_int, a_date, another_date, a_string]
..     VALUES [%[int]s, %[date]s, %[date]s, %[str]s];
..     """,
..     {'int': 10, 'str': "O'Reilly", 'date': datetime.date[2005, 11, 18]}]
733 của kết nối không đóng kết nối mà chỉ đóng giao dịch được liên kết với nó. Nếu bạn muốn đảm bảo kết nối được đóng sau một thời điểm nhất định, bạn vẫn nên sử dụng khối thử bắt

>>> cur.execute["""
..     INSERT INTO some_table [an_int, a_date, another_date, a_string]
..     VALUES [%[int]s, %[date]s, %[date]s, %[str]s];
..     """,
..     {'int': 10, 'str': "O'Reilly", 'date': datetime.date[2005, 11, 18]}]
5

Đã thay đổi trong phiên bản 2. 9. ______1740 cũng bắt đầu giao dịch trên các kết nối tự động cam kết.

Con trỏ phía máy chủ¶

Khi một truy vấn cơ sở dữ liệu được thực thi, Psycopg

>>> cur.execute["SELECT [%s % 2] = 0 AS even", [10,]]       # WRONG
>>> cur.execute["SELECT [%s %% 2] = 0 AS even", [10,]]      # correct
2 thường tìm nạp tất cả các bản ghi do phần phụ trợ trả về, chuyển chúng sang quy trình máy khách. Nếu truy vấn trả về một lượng dữ liệu khổng lồ, thì một lượng lớn bộ nhớ tương ứng sẽ được cấp phát bởi máy khách

Nếu tập dữ liệu quá lớn để xử lý thực tế ở phía máy khách, có thể tạo một con trỏ phía máy chủ. Sử dụng loại con trỏ này, chỉ có thể chuyển đến máy khách một lượng dữ liệu được kiểm soát, do đó có thể kiểm tra một tập dữ liệu lớn mà không cần giữ nó hoàn toàn trong bộ nhớ

Con trỏ phía máy chủ được tạo trong PostgreSQL bằng lệnh

>>> cur.execute["""
..     INSERT INTO some_table [an_int, a_date, another_date, a_string]
..     VALUES [%[int]s, %[date]s, %[date]s, %[str]s];
..     """,
..     {'int': 10, 'str': "O'Reilly", 'date': datetime.date[2005, 11, 18]}]
742 và sau đó được xử lý bằng lệnh
>>> cur.execute["""
..     INSERT INTO some_table [an_int, a_date, another_date, a_string]
..     VALUES [%[int]s, %[date]s, %[date]s, %[str]s];
..     """,
..     {'int': 10, 'str': "O'Reilly", 'date': datetime.date[2005, 11, 18]}]
743,
>>> cur.execute["""
..     INSERT INTO some_table [an_int, a_date, another_date, a_string]
..     VALUES [%[int]s, %[date]s, %[date]s, %[str]s];
..     """,
..     {'int': 10, 'str': "O'Reilly", 'date': datetime.date[2005, 11, 18]}]
744 và
>>> cur.execute["""
..     INSERT INTO some_table [an_int, a_date, another_date, a_string]
..     VALUES [%[int]s, %[date]s, %[date]s, %[str]s];
..     """,
..     {'int': 10, 'str': "O'Reilly", 'date': datetime.date[2005, 11, 18]}]
745

Psycopg bọc con trỏ phía máy chủ cơ sở dữ liệu trong các con trỏ được đặt tên. Một con trỏ được đặt tên được tạo bằng phương thức

>>> cur.execute["SELECT [%s % 2] = 0 AS even", [10,]]       # WRONG
>>> cur.execute["SELECT [%s %% 2] = 0 AS even", [10,]]      # correct
3 chỉ định tham số tên. Con trỏ như vậy sẽ hoạt động gần giống như một con trỏ thông thường, cho phép người dùng di chuyển trong tập dữ liệu bằng phương pháp
>>> cur.execute["""
..     INSERT INTO some_table [an_int, a_date, another_date, a_string]
..     VALUES [%[int]s, %[date]s, %[date]s, %[str]s];
..     """,
..     {'int': 10, 'str': "O'Reilly", 'date': datetime.date[2005, 11, 18]}]
747 và đọc dữ liệu bằng phương pháp
>>> cur.execute["SELECT [%s % 2] = 0 AS even", [10,]]       # WRONG
>>> cur.execute["SELECT [%s %% 2] = 0 AS even", [10,]]      # correct
9 và
>>> cur.execute["""
..     INSERT INTO some_table [an_int, a_date, another_date, a_string]
..     VALUES [%[int]s, %[date]s, %[date]s, %[str]s];
..     """,
..     {'int': 10, 'str': "O'Reilly", 'date': datetime.date[2005, 11, 18]}]
20. Thông thường, bạn chỉ có thể cuộn về phía trước trong một con trỏ. nếu bạn cần cuộn ngược lại, bạn nên khai báo con trỏ của mình
>>> cur.execute["""
..     INSERT INTO some_table [an_int, a_date, another_date, a_string]
..     VALUES [%[int]s, %[date]s, %[date]s, %[str]s];
..     """,
..     {'int': 10, 'str': "O'Reilly", 'date': datetime.date[2005, 11, 18]}]
750

Con trỏ được đặt tên cũng có thể lặp lại giống như con trỏ thông thường. Tuy nhiên, lưu ý rằng trước Psycopg 2. 4 lần lặp đã được thực hiện để tìm nạp một bản ghi tại một thời điểm từ phần phụ trợ, dẫn đến chi phí hoạt động lớn. Thuộc tính

>>> cur.execute["""
..     INSERT INTO some_table [an_int, a_date, another_date, a_string]
..     VALUES [%[int]s, %[date]s, %[date]s, %[str]s];
..     """,
..     {'int': 10, 'str': "O'Reilly", 'date': datetime.date[2005, 11, 18]}]
751 hiện kiểm soát số lượng bản ghi được tìm nạp tại một thời điểm trong quá trình lặp lại. giá trị mặc định là 2000 cho phép tìm nạp khoảng 100KB mỗi vòng khứ hồi giả sử các bản ghi gồm 10-20 cột gồm các chuỗi và số hỗn hợp; .

Con trỏ được đặt tên thường được tạo

>>> cur.execute["""
..     INSERT INTO some_table [an_int, a_date, another_date, a_string]
..     VALUES [%[int]s, %[date]s, %[date]s, %[str]s];
..     """,
..     {'int': 10, 'str': "O'Reilly", 'date': datetime.date[2005, 11, 18]}]
752, nghĩa là chúng chỉ tồn tại miễn là giao dịch hiện tại. Cố gắng tìm nạp từ một con trỏ được đặt tên sau một
>>> cur.execute["SELECT [%s % 2] = 0 AS even", [10,]]       # WRONG
>>> cur.execute["SELECT [%s %% 2] = 0 AS even", [10,]]      # correct
4 hoặc để tạo một con trỏ được đặt tên khi kết nối ở chế độ
>>> cur.execute["""
..     INSERT INTO some_table [an_int, a_date, another_date, a_string]
..     VALUES [%[int]s, %[date]s, %[date]s, %[str]s];
..     """,
..     {'int': 10, 'str': "O'Reilly", 'date': datetime.date[2005, 11, 18]}]
729 sẽ dẫn đến một ngoại lệ. Có thể tạo con trỏ
>>> cur.execute["""
..     INSERT INTO some_table [an_int, a_date, another_date, a_string]
..     VALUES [%[int]s, %[date]s, %[date]s, %[str]s];
..     """,
..     {'int': 10, 'str': "O'Reilly", 'date': datetime.date[2005, 11, 18]}]
755 bằng cách chỉ định giá trị
>>> cur.execute["""
..     INSERT INTO some_table [an_int, a_date, another_date, a_string]
..     VALUES [%[int]s, %[date]s, %[date]s, %[str]s];
..     """,
..     {'int': 10, 'str': "O'Reilly", 'date': datetime.date[2005, 11, 18]}]
629 cho tham số
>>> cur.execute["""
..     INSERT INTO some_table [an_int, a_date, another_date, a_string]
..     VALUES [%[int]s, %[date]s, %[date]s, %[str]s];
..     """,
..     {'int': 10, 'str': "O'Reilly", 'date': datetime.date[2005, 11, 18]}]
757 thành
>>> cur.execute["SELECT [%s % 2] = 0 AS even", [10,]]       # WRONG
>>> cur.execute["SELECT [%s %% 2] = 0 AS even", [10,]]      # correct
3 hoặc bằng cách đặt thuộc tính
>>> cur.execute["""
..     INSERT INTO some_table [an_int, a_date, another_date, a_string]
..     VALUES [%[int]s, %[date]s, %[date]s, %[str]s];
..     """,
..     {'int': 10, 'str': "O'Reilly", 'date': datetime.date[2005, 11, 18]}]
757 thành
>>> cur.execute["""
..     INSERT INTO some_table [an_int, a_date, another_date, a_string]
..     VALUES [%[int]s, %[date]s, %[date]s, %[str]s];
..     """,
..     {'int': 10, 'str': "O'Reilly", 'date': datetime.date[2005, 11, 18]}]
629 trước khi gọi
>>> cur.execute["SELECT [%s % 2] = 0 AS even", [10,]]       # WRONG
>>> cur.execute["SELECT [%s %% 2] = 0 AS even", [10,]]      # correct
7 trên con trỏ. Điều cực kỳ quan trọng là luôn
>>> cur.execute["""
..     INSERT INTO some_table [an_int, a_date, another_date, a_string]
..     VALUES [%[int]s, %[date]s, %[date]s, %[str]s];
..     """,
..     {'int': 10, 'str': "O'Reilly", 'date': datetime.date[2005, 11, 18]}]
724 những con trỏ như vậy, nếu không chúng sẽ tiếp tục giữ tài nguyên phía máy chủ cho đến khi kết nối cuối cùng bị đóng. Cũng lưu ý rằng mặc dù thời gian tồn tại của con trỏ
>>> cur.execute["""
..     INSERT INTO some_table [an_int, a_date, another_date, a_string]
..     VALUES [%[int]s, %[date]s, %[date]s, %[str]s];
..     """,
..     {'int': 10, 'str': "O'Reilly", 'date': datetime.date[2005, 11, 18]}]
755 kéo dài sau
>>> cur.execute["SELECT [%s % 2] = 0 AS even", [10,]]       # WRONG
>>> cur.execute["SELECT [%s %% 2] = 0 AS even", [10,]]      # correct
4, nhưng việc gọi
>>> cur.execute["SELECT [%s % 2] = 0 AS even", [10,]]       # WRONG
>>> cur.execute["SELECT [%s %% 2] = 0 AS even", [10,]]      # correct
5 sẽ tự động đóng con trỏ

Ghi chú

Cũng có thể sử dụng con trỏ được đặt tên để sử dụng con trỏ được tạo theo một số cách khác ngoài việc sử dụng

>>> cur.execute["""
..     INSERT INTO some_table [an_int, a_date, another_date, a_string]
..     VALUES [%[int]s, %[date]s, %[date]s, %[str]s];
..     """,
..     {'int': 10, 'str': "O'Reilly", 'date': datetime.date[2005, 11, 18]}]
742 được thực hiện bởi
>>> cur.execute["SELECT [%s % 2] = 0 AS even", [10,]]       # WRONG
>>> cur.execute["SELECT [%s %% 2] = 0 AS even", [10,]]      # correct
7. Ví dụ: bạn có thể có một hàm PL/pgSQL trả về một con trỏ

>>> cur.execute["""
..     INSERT INTO some_table [an_int, a_date, another_date, a_string]
..     VALUES [%[int]s, %[date]s, %[date]s, %[str]s];
..     """,
..     {'int': 10, 'str': "O'Reilly", 'date': datetime.date[2005, 11, 18]}]
6

Bạn có thể đọc nội dung con trỏ bằng cách gọi hàm bằng con trỏ Psycopg thông thường, không được đặt tên

>>> cur.execute["""
..     INSERT INTO some_table [an_int, a_date, another_date, a_string]
..     VALUES [%[int]s, %[date]s, %[date]s, %[str]s];
..     """,
..     {'int': 10, 'str': "O'Reilly", 'date': datetime.date[2005, 11, 18]}]
7

và sau đó sử dụng một con trỏ được đặt tên trong cùng một giao dịch để “đánh cắp con trỏ”

>>> cur.execute["""
..     INSERT INTO some_table [an_int, a_date, another_date, a_string]
..     VALUES [%[int]s, %[date]s, %[date]s, %[str]s];
..     """,
..     {'int': 10, 'str': "O'Reilly", 'date': datetime.date[2005, 11, 18]}]
8

An toàn chỉ và quy trình¶

Mô-đun Psycopg và các đối tượng

>>> cur.execute["SELECT [%s % 2] = 0 AS even", [10,]]       # WRONG
>>> cur.execute["SELECT [%s %% 2] = 0 AS even", [10,]]      # correct
0 an toàn cho luồng. nhiều luồng có thể truy cập cùng một cơ sở dữ liệu bằng cách sử dụng các phiên riêng biệt và tạo
>>> cur.execute["SELECT [%s % 2] = 0 AS even", [10,]]       # WRONG
>>> cur.execute["SELECT [%s %% 2] = 0 AS even", [10,]]      # correct
0 cho mỗi luồng hoặc sử dụng cùng một kết nối và tạo các
>>> cur.execute["SELECT [%s % 2] = 0 AS even", [10,]]       # WRONG
>>> cur.execute["SELECT [%s %% 2] = 0 AS even", [10,]]      # correct
2 riêng biệt. Trong DB API 2. 0, Psycopg là luồng cấp 2 an toàn

Sự khác biệt giữa hai cách tiếp cận trên là, sử dụng các kết nối khác nhau, các lệnh sẽ được thực thi trong các phiên khác nhau và sẽ được phục vụ bởi các quy trình máy chủ khác nhau. Mặt khác, khi sử dụng nhiều con trỏ trên cùng một kết nối, tất cả các lệnh sẽ được thực thi trong cùng một phiên [và trong cùng một giao dịch nếu kết nối không ở chế độ autocommit mode], but they will be serialized.

Các quan sát trên chỉ có giá trị đối với các chủ đề thông thường. chúng không áp dụng cho các quy trình rẽ nhánh cũng như các luồng xanh. Các kết nối

>>> cur.execute["""
..     INSERT INTO some_table [an_int, a_date, another_date, a_string]
..     VALUES [%[int]s, %[date]s, %[date]s, %[str]s];
..     """,
..     {'int': 10, 'str': "O'Reilly", 'date': datetime.date[2005, 11, 18]}]
771 không nên được sử dụng bởi các quy trình rẽ nhánh, vì vậy, khi sử dụng một mô-đun như
>>> cur.execute["""
..     INSERT INTO some_table [an_int, a_date, another_date, a_string]
..     VALUES [%[int]s, %[date]s, %[date]s, %[str]s];
..     """,
..     {'int': 10, 'str': "O'Reilly", 'date': datetime.date[2005, 11, 18]}]
772 hoặc phương pháp triển khai web rẽ nhánh như FastCGI, hãy đảm bảo tạo các kết nối sau khi rẽ nhánh

Các kết nối không được chia sẻ bởi các chủ đề màu lục khác nhau. xem Hỗ trợ thư viện coroutine để biết thêm chi tiết.

Sử dụng COPY TO và COPY FROM¶

Các đối tượng Psycopg

>>> cur.execute["SELECT [%s % 2] = 0 AS even", [10,]]       # WRONG
>>> cur.execute["SELECT [%s %% 2] = 0 AS even", [10,]]      # correct
2 cung cấp giao diện cho lệnh PostgreSQL
>>> cur.execute["""
..     INSERT INTO some_table [an_int, a_date, another_date, a_string]
..     VALUES [%[int]s, %[date]s, %[date]s, %[str]s];
..     """,
..     {'int': 10, 'str': "O'Reilly", 'date': datetime.date[2005, 11, 18]}]
774 hiệu quả để di chuyển dữ liệu từ tệp sang bảng và ngược lại

Hiện tại không có sự điều chỉnh nào được cung cấp giữa các loại Python và PostgreSQL trên

>>> cur.execute["""
..     INSERT INTO some_table [an_int, a_date, another_date, a_string]
..     VALUES [%[int]s, %[date]s, %[date]s, %[str]s];
..     """,
..     {'int': 10, 'str': "O'Reilly", 'date': datetime.date[2005, 11, 18]}]
774. tệp có thể là bất kỳ đối tượng giống tệp Python nào nhưng định dạng của nó phải ở định dạng được chấp nhận bởi lệnh COPY của PostgreSQL [định dạng dữ liệu, ký tự thoát, v.v.]

Các phương pháp tiếp xúc là

>>> cur.execute["""
..     INSERT INTO some_table [an_int, a_date, another_date, a_string]
..     VALUES [%[int]s, %[date]s, %[date]s, %[str]s];
..     """,
..     {'int': 10, 'str': "O'Reilly", 'date': datetime.date[2005, 11, 18]}]
776

Đọc dữ liệu từ một đối tượng giống như tệp nối chúng vào bảng cơ sở dữ liệu [cú pháp ___1777]. Tệp nguồn phải cung cấp cả phương pháp

>>> cur.execute["""
..     INSERT INTO some_table [an_int, a_date, another_date, a_string]
..     VALUES [%[int]s, %[date]s, %[date]s, %[str]s];
..     """,
..     {'int': 10, 'str': "O'Reilly", 'date': datetime.date[2005, 11, 18]}]
778 và
>>> cur.execute["""
..     INSERT INTO some_table [an_int, a_date, another_date, a_string]
..     VALUES [%[int]s, %[date]s, %[date]s, %[str]s];
..     """,
..     {'int': 10, 'str': "O'Reilly", 'date': datetime.date[2005, 11, 18]}]
779

>>> cur.execute["""
..     INSERT INTO some_table [an_int, a_date, another_date, a_string]
..     VALUES [%[int]s, %[date]s, %[date]s, %[str]s];
..     """,
..     {'int': 10, 'str': "O'Reilly", 'date': datetime.date[2005, 11, 18]}]
780

Ghi nội dung của bảng vào một đối tượng giống như tệp [cú pháp ____1781]. Tệp đích phải có phương thức

>>> cur.execute["""
..     INSERT INTO some_table [an_int, a_date, another_date, a_string]
..     VALUES [%[int]s, %[date]s, %[date]s, %[str]s];
..     """,
..     {'int': 10, 'str': "O'Reilly", 'date': datetime.date[2005, 11, 18]}]
782

>>> cur.execute["""
..     INSERT INTO some_table [an_int, a_date, another_date, a_string]
..     VALUES [%[int]s, %[date]s, %[date]s, %[str]s];
..     """,
..     {'int': 10, 'str': "O'Reilly", 'date': datetime.date[2005, 11, 18]}]
783

Cho phép xử lý các trường hợp cụ thể hơn và sử dụng tất cả các tính năng

>>> cur.execute["""
..     INSERT INTO some_table [an_int, a_date, another_date, a_string]
..     VALUES [%[int]s, %[date]s, %[date]s, %[str]s];
..     """,
..     {'int': 10, 'str': "O'Reilly", 'date': datetime.date[2005, 11, 18]}]
774 có sẵn trong PostgreSQL

Vui lòng tham khảo tài liệu về các phương pháp đơn lẻ để biết chi tiết và ví dụ

Truy cập vào các đối tượng lớn PostgreSQL¶

PostgreSQL cung cấp hỗ trợ cho các đối tượng lớn, cung cấp quyền truy cập kiểu luồng vào dữ liệu người dùng được lưu trữ trong cấu trúc đối tượng lớn đặc biệt. Chúng rất hữu ích với các giá trị dữ liệu quá lớn để có thể thao tác một cách thuận tiện nói chung

Psycopg cho phép truy cập vào đối tượng lớn bằng cách sử dụng lớp

>>> cur.execute["""
..     INSERT INTO some_table [an_int, a_date, another_date, a_string]
..     VALUES [%[int]s, %[date]s, %[date]s, %[str]s];
..     """,
..     {'int': 10, 'str': "O'Reilly", 'date': datetime.date[2005, 11, 18]}]
785. Các đối tượng được tạo bằng cách sử dụng phương thức nhà máy
>>> cur.execute["""
..     INSERT INTO some_table [an_int, a_date, another_date, a_string]
..     VALUES [%[int]s, %[date]s, %[date]s, %[str]s];
..     """,
..     {'int': 10, 'str': "O'Reilly", 'date': datetime.date[2005, 11, 18]}]
786. Dữ liệu có thể được truy xuất dưới dạng byte hoặc dưới dạng chuỗi Unicode

Psycopg đối tượng lớn hỗ trợ nhập/xuất hiệu quả với các tệp hệ thống tệp bằng cách sử dụng các hàm libpq

>>> cur.execute["""
..     INSERT INTO some_table [an_int, a_date, another_date, a_string]
..     VALUES [%[int]s, %[date]s, %[date]s, %[str]s];
..     """,
..     {'int': 10, 'str': "O'Reilly", 'date': datetime.date[2005, 11, 18]}]
787 và
>>> cur.execute["""
..     INSERT INTO some_table [an_int, a_date, another_date, a_string]
..     VALUES [%[int]s, %[date]s, %[date]s, %[str]s];
..     """,
..     {'int': 10, 'str': "O'Reilly", 'date': datetime.date[2005, 11, 18]}]
788

Đã thay đổi trong phiên bản 2. 6. đã thêm hỗ trợ cho các đối tượng lớn hơn 2GB. Lưu ý rằng hỗ trợ chỉ được bật nếu tất cả các điều kiện sau được xác minh.

  • bản dựng Python là 64 bit;

  • tiện ích mở rộng được xây dựng dựa trên ít nhất libpq 9. 3;

  • phiên bản máy chủ ít nhất là PostgreSQL 9. 3 [

    >>> cur.execute["""
    ..     INSERT INTO some_table [an_int, a_date, another_date, a_string]
    ..     VALUES [%[int]s, %[date]s, %[date]s, %[str]s];
    ..     """,
    ..     {'int': 10, 'str': "O'Reilly", 'date': datetime.date[2005, 11, 18]}]
    
    789 phải >=
    >>> cur.execute["""
    ..     INSERT INTO some_table [an_int, a_date, another_date, a_string]
    ..     VALUES [%[int]s, %[date]s, %[date]s, %[str]s];
    ..     """,
    ..     {'int': 10, 'str': "O'Reilly", 'date': datetime.date[2005, 11, 18]}]
    
    790]

Nếu Psycopg được xây dựng với hỗ trợ các đối tượng lớn 64 bit [i. e. hai điều kiện đầu tiên ở trên được xác minh], hằng số

>>> cur.execute["""
..     INSERT INTO some_table [an_int, a_date, another_date, a_string]
..     VALUES [%[int]s, %[date]s, %[date]s, %[str]s];
..     """,
..     {'int': 10, 'str': "O'Reilly", 'date': datetime.date[2005, 11, 18]}]
791 sẽ chứa cờ
>>> cur.execute["""
..     INSERT INTO some_table [an_int, a_date, another_date, a_string]
..     VALUES [%[int]s, %[date]s, %[date]s, %[str]s];
..     """,
..     {'int': 10, 'str': "O'Reilly", 'date': datetime.date[2005, 11, 18]}]
792. Nếu bất kỳ điều kiện nào không được đáp ứng, một số phương thức ________ 1785 sẽ không thành công nếu các đối số vượt quá 2GB

Hỗ trợ giao thức Cam kết hai pha¶

Mới trong phiên bản 2. 3

Psycopg hiển thị các tính năng cam kết hai giai đoạn có sẵn kể từ PostgreSQL 8. 1 triển khai tiện ích mở rộng cam kết hai giai đoạn do API DB đề xuất 2. 0

API cơ sở dữ liệu 2. 0 của cam kết hai pha được lấy cảm hứng từ đặc tả XA, theo đó ID giao dịch được hình thành từ ba thành phần

  • ID định dạng [số nguyên 32 bit không âm]

  • ID giao dịch toàn cầu [chuỗi không dài hơn 64 byte]

  • một vòng loại nhánh [chuỗi không dài hơn 64 byte]

Đối với một giao dịch toàn cầu cụ thể, hai thành phần đầu tiên sẽ giống nhau đối với tất cả các tài nguyên. Mỗi tài nguyên sẽ được chỉ định một vòng loại chi nhánh khác nhau

Theo API DB 2. 0, ID giao dịch được tạo bằng phương pháp

>>> cur.execute["""
..     INSERT INTO some_table [an_int, a_date, another_date, a_string]
..     VALUES [%[int]s, %[date]s, %[date]s, %[str]s];
..     """,
..     {'int': 10, 'str': "O'Reilly", 'date': datetime.date[2005, 11, 18]}]
794. Sau khi bạn có id giao dịch, giao dịch phân phối có thể được bắt đầu bằng
>>> cur.execute["""
..     INSERT INTO some_table [an_int, a_date, another_date, a_string]
..     VALUES [%[int]s, %[date]s, %[date]s, %[str]s];
..     """,
..     {'int': 10, 'str': "O'Reilly", 'date': datetime.date[2005, 11, 18]}]
795, được chuẩn bị bằng cách sử dụng
>>> cur.execute["""
..     INSERT INTO some_table [an_int, a_date, another_date, a_string]
..     VALUES [%[int]s, %[date]s, %[date]s, %[str]s];
..     """,
..     {'int': 10, 'str': "O'Reilly", 'date': datetime.date[2005, 11, 18]}]
796 và hoàn thành bằng cách sử dụng
>>> cur.execute["""
..     INSERT INTO some_table [an_int, a_date, another_date, a_string]
..     VALUES [%[int]s, %[date]s, %[date]s, %[str]s];
..     """,
..     {'int': 10, 'str': "O'Reilly", 'date': datetime.date[2005, 11, 18]}]
797 hoặc
>>> cur.execute["""
..     INSERT INTO some_table [an_int, a_date, another_date, a_string]
..     VALUES [%[int]s, %[date]s, %[date]s, %[str]s];
..     """,
..     {'int': 10, 'str': "O'Reilly", 'date': datetime.date[2005, 11, 18]}]
798. ID giao dịch cũng có thể được truy xuất từ ​​cơ sở dữ liệu bằng cách sử dụng
>>> cur.execute["""
..     INSERT INTO some_table [an_int, a_date, another_date, a_string]
..     VALUES [%[int]s, %[date]s, %[date]s, %[str]s];
..     """,
..     {'int': 10, 'str': "O'Reilly", 'date': datetime.date[2005, 11, 18]}]
799 và hoàn thành bằng cách sử dụng
>>> cur.execute["""
..     INSERT INTO some_table [an_int, a_date, another_date, a_string]
..     VALUES [%[int]s, %[date]s, %[date]s, %[str]s];
..     """,
..     {'int': 10, 'str': "O'Reilly", 'date': datetime.date[2005, 11, 18]}]
797 và
>>> cur.execute["""
..     INSERT INTO some_table [an_int, a_date, another_date, a_string]
..     VALUES [%[int]s, %[date]s, %[date]s, %[str]s];
..     """,
..     {'int': 10, 'str': "O'Reilly", 'date': datetime.date[2005, 11, 18]}]
798 ở trên

Mặc dù vậy, PostgreSQL không tuân theo tiêu chuẩn XA và ID cho giao dịch được chuẩn bị PostgreSQL có thể là bất kỳ chuỗi nào dài tối đa 200 ký tự. Các đối tượng

>>> cur.execute["""
..     INSERT INTO some_table [an_int, a_date, another_date, a_string]
..     VALUES [%[int]s, %[date]s, %[date]s, %[str]s];
..     """,
..     {'int': 10, 'str': "O'Reilly", 'date': datetime.date[2005, 11, 18]}]
02 của Psycopg có thể đại diện cho cả ID giao dịch kiểu XA [chẳng hạn như ID được tạo bởi phương thức
>>> cur.execute["""
..     INSERT INTO some_table [an_int, a_date, another_date, a_string]
..     VALUES [%[int]s, %[date]s, %[date]s, %[str]s];
..     """,
..     {'int': 10, 'str': "O'Reilly", 'date': datetime.date[2005, 11, 18]}]
03] và ID giao dịch PostgreSQL được xác định bởi một chuỗi chưa phân tích cú pháp

Định dạng trong đó các Xids được chuyển đổi thành các chuỗi được chuyển đến cơ sở dữ liệu giống như định dạng được sử dụng bởi trình điều khiển JDBC PostgreSQL. điều này sẽ cho phép tương tác giữa các công cụ được viết bằng Python và Java. Ví dụ: một công cụ khôi phục được viết bằng Python sẽ có thể nhận ra các thành phần của giao dịch do chương trình Java tạo ra

Bài Viết Liên Quan

Chủ Đề