Lấy tên bảng sqlite3 python

SQLite là thư viện C cung cấp cơ sở dữ liệu dựa trên đĩa nhẹ, không yêu cầu quy trình máy chủ riêng biệt và cho phép truy cập cơ sở dữ liệu bằng một biến thể không chuẩn của ngôn ngữ truy vấn SQL. Một số ứng dụng có thể sử dụng SQLite để lưu trữ dữ liệu nội bộ. Cũng có thể tạo nguyên mẫu một ứng dụng bằng SQLite và sau đó chuyển mã sang cơ sở dữ liệu lớn hơn như PostgreSQL hoặc Oracle

Mô-đun

cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
0 được viết bởi Gerhard Häring. Nó cung cấp một giao diện SQL tương thích với DB-API 2. 0 được mô tả bởi PEP 249 và yêu cầu SQLite 3. 7. 15 hoặc mới hơn

Tài liệu này bao gồm bốn phần chính

  • Hướng dẫn hướng dẫn cách sử dụng mô-đun

    cur.execute["""
        INSERT INTO movie VALUES
            ['Monty Python and the Holy Grail', 1975, 8.2],
            ['And Now for Something Completely Different', 1971, 7.5]
    """]
    
    0.

  • Tham khảo mô tả các lớp và chức năng mà mô-đun này định nghĩa.

  • Hướng dẫn cách thực hiện nêu chi tiết cách xử lý các tác vụ cụ thể.

  • Giải thích cung cấp thông tin cơ bản chuyên sâu về kiểm soát giao dịch.

Xem thêm

https. //www. sqlite. tổ chức

Trang web SQLite;

https. //www. w3schools. com/sql/

Hướng dẫn, tài liệu tham khảo và ví dụ để học cú pháp SQL

PEP 249 - Đặc tả API cơ sở dữ liệu 2. 0

PEP được viết bởi Marc-André Lemburg

Hướng dẫn¶

Trong hướng dẫn này, bạn sẽ tạo cơ sở dữ liệu phim Monty Python bằng cách sử dụng chức năng

cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
0 cơ bản. Nó giả định một sự hiểu biết cơ bản về các khái niệm cơ sở dữ liệu, bao gồm con trỏ và giao dịch

Đầu tiên, chúng ta cần tạo một cơ sở dữ liệu mới và mở một kết nối cơ sở dữ liệu để cho phép

cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
0 làm việc với nó. Gọi
cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
5 để tạo kết nối đến cơ sở dữ liệu
cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
6 trong thư mục làm việc hiện tại, tạo ngầm nếu chưa tồn tại

import sqlite3
con = sqlite3.connect["tutorial.db"]

Đối tượng

cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
7 được trả về
cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
8 đại diện cho kết nối với cơ sở dữ liệu trên đĩa

Để thực thi các câu lệnh SQL và tìm nạp kết quả từ các truy vấn SQL, chúng ta sẽ cần sử dụng một con trỏ cơ sở dữ liệu. Gọi

cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
9 để tạo
cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
00

cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
1

Bây giờ chúng ta đã có kết nối cơ sở dữ liệu và con trỏ, chúng ta có thể tạo bảng cơ sở dữ liệu

cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
01 với các cột cho tiêu đề, năm phát hành và điểm đánh giá. Để đơn giản, chúng ta chỉ cần sử dụng tên cột trong khai báo bảng – nhờ tính năng gõ linh hoạt của SQLite, việc chỉ định loại dữ liệu là tùy chọn. Thực hiện câu lệnh
cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
02 bằng cách gọi
cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
03

cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
5

Chúng tôi có thể xác minh rằng bảng mới đã được tạo bằng cách truy vấn bảng

cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
04 được tích hợp sẵn trong SQLite, hiện sẽ chứa một mục nhập cho định nghĩa bảng
cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
01 [xem Bảng lược đồ để biết chi tiết]. Thực hiện truy vấn đó bằng cách gọi
cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
03, gán kết quả cho
cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
07 và gọi
cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
08 để lấy hàng kết quả

cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
1

Chúng ta có thể thấy rằng bảng đã được tạo, khi truy vấn trả về một

cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
09 chứa tên của bảng. Nếu chúng tôi truy vấn
cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
04 cho một bảng không tồn tại
cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
41, thì
cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
08 sẽ trả về
cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
43

cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
7

Bây giờ, hãy thêm hai hàng dữ liệu được cung cấp dưới dạng ký tự SQL bằng cách thực hiện câu lệnh

cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
44, một lần nữa bằng cách gọi
cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
03

cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]

Câu lệnh

cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
44 ngầm mở một giao dịch, giao dịch này cần được cam kết trước khi các thay đổi được lưu vào cơ sở dữ liệu [xem Kiểm soát giao dịch để biết chi tiết]. Gọi
cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
47 trên đối tượng kết nối để thực hiện giao dịch.

cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
0

Chúng tôi có thể xác minh rằng dữ liệu đã được chèn chính xác bằng cách thực hiện truy vấn

cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
48. Sử dụng
cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
03 hiện đã quen thuộc để gán kết quả cho
cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
07 và gọi
cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
71 để trả về tất cả các hàng kết quả

cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
4

Kết quả là một

cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
72 trong số hai
cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
09, mỗi cái một hàng, mỗi cái chứa giá trị
cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
74 của hàng đó

Bây giờ, chèn thêm ba hàng bằng cách gọi

cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
75

cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
7

Lưu ý rằng trình giữ chỗ

cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
76 được sử dụng để liên kết
cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
77 với truy vấn. Luôn sử dụng trình giữ chỗ thay vì định dạng chuỗi để liên kết các giá trị Python với câu lệnh SQL, nhằm tránh các cuộc tấn công SQL injection [xem Cách thức . for more details].

Chúng tôi có thể xác minh rằng các hàng mới đã được chèn bằng cách thực hiện truy vấn

cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
48, lần này lặp lại kết quả của truy vấn

cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
7

Mỗi hàng là một

cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
09 hai mục của
cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
70, khớp với các cột được chọn trong truy vấn

Cuối cùng, xác minh rằng cơ sở dữ liệu đã được ghi vào đĩa bằng cách gọi

cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
71 để đóng kết nối hiện có, mở một kết nối mới, tạo con trỏ mới, sau đó truy vấn cơ sở dữ liệu

cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
10

Bây giờ bạn đã tạo cơ sở dữ liệu SQLite bằng mô-đun

cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
0, đã chèn dữ liệu và truy xuất giá trị từ cơ sở dữ liệu đó theo nhiều cách

Xem thêm

  • Hướng dẫn cách thực hiện để đọc thêm.

    • Cách sử dụng trình giữ chỗ để liên kết các giá trị trong truy vấn SQL

    • Cách điều chỉnh các loại Python tùy chỉnh thành các giá trị SQLite

    • Cách chuyển đổi các giá trị SQLite thành các loại Python tùy chỉnh

    • Cách sử dụng trình quản lý bối cảnh kết nối

    • Cách tạo và sử dụng row factory

  • Giải thích để biết thông tin cơ bản chuyên sâu về kiểm soát giao dịch.

Thẩm quyền giải quyết¶

Chức năng mô-đun¶

sqlite3. kết nối[cơ sở dữ liệu , thời gian chờ . 0=5.0 , detect_types=0 . Kết nối isolation_level='DEFERRED', check_same_thread=True, factory=sqlite3.Connection , cached_statements=128, uri=False]

Mở kết nối tới cơ sở dữ liệu SQLite

Thông số
  • cơ sở dữ liệu [ đối tượng dạng đường dẫn ] – Đường dẫn đến tệp cơ sở dữ liệu sẽ được mở. Vượt qua

    cur.execute["""
        INSERT INTO movie VALUES
            ['Monty Python and the Holy Grail', 1975, 8.2],
            ['And Now for Something Completely Different', 1971, 7.5]
    """]
    
    73 để mở kết nối tới cơ sở dữ liệu trong RAM thay vì trên đĩa.

  • thời gian chờ [float] – Kết nối sẽ đợi bao nhiêu giây trước khi đưa ra một ngoại lệ, nếu cơ sở dữ liệu bị khóa bởi một kết nối khác. Nếu một kết nối khác mở một giao dịch để sửa đổi cơ sở dữ liệu, nó sẽ bị khóa cho đến khi giao dịch đó được thực hiện. Mặc định năm giây

  • detect_types [int] – Kiểm soát xem và bằng cách nào các loại dữ liệu không được SQLite hỗ trợ được tra cứu để chuyển đổi thành các loại Python, bằng cách sử dụng . Đặt nó thành bất kỳ kết hợp nào [sử dụng

    cur.execute["""
        INSERT INTO movie VALUES
            ['Monty Python and the Holy Grail', 1975, 8.2],
            ['And Now for Something Completely Different', 1971, 7.5]
    """]
    
    75, bitwise hoặc] của
    cur.execute["""
        INSERT INTO movie VALUES
            ['Monty Python and the Holy Grail', 1975, 8.2],
            ['And Now for Something Completely Different', 1971, 7.5]
    """]
    
    76 và
    cur.execute["""
        INSERT INTO movie VALUES
            ['Monty Python and the Holy Grail', 1975, 8.2],
            ['And Now for Something Completely Different', 1971, 7.5]
    """]
    
    77 để kích hoạt tính năng này. Tên cột được ưu tiên hơn các loại đã khai báo nếu cả hai cờ được đặt. Không thể phát hiện các loại cho các trường đã tạo [ví dụ:
    cur.execute["""
        INSERT INTO movie VALUES
            ['Monty Python and the Holy Grail', 1975, 8.2],
            ['And Now for Something Completely Different', 1971, 7.5]
    """]
    
    78], ngay cả khi tham số detect_types được đặt; . Theo mặc định [
    cur.execute["""
        INSERT INTO movie VALUES
            ['Monty Python and the Holy Grail', 1975, 8.2],
            ['And Now for Something Completely Different', 1971, 7.5]
    """]
    
    100], tính năng phát hiện loại bị tắt.

  • isolation_level [str. Không có] –

    cur.execute["""
        INSERT INTO movie VALUES
            ['Monty Python and the Holy Grail', 1975, 8.2],
            ['And Now for Something Completely Different', 1971, 7.5]
    """]
    
    101 của kết nối, kiểm soát liệu các giao dịch có được mở hoàn toàn hay không và bằng cách nào. Có thể là
    cur.execute["""
        INSERT INTO movie VALUES
            ['Monty Python and the Holy Grail', 1975, 8.2],
            ['And Now for Something Completely Different', 1971, 7.5]
    """]
    
    102 [mặc định],
    cur.execute["""
        INSERT INTO movie VALUES
            ['Monty Python and the Holy Grail', 1975, 8.2],
            ['And Now for Something Completely Different', 1971, 7.5]
    """]
    
    103 hoặc
    cur.execute["""
        INSERT INTO movie VALUES
            ['Monty Python and the Holy Grail', 1975, 8.2],
            ['And Now for Something Completely Different', 1971, 7.5]
    """]
    
    104; . Xem Kiểm soát giao dịch để biết thêm.

  • check_same_thread [bool] – Nếu

    cur.execute["""
        INSERT INTO movie VALUES
            ['Monty Python and the Holy Grail', 1975, 8.2],
            ['And Now for Something Completely Different', 1971, 7.5]
    """]
    
    106 [mặc định], chỉ thread tạo mới có thể sử dụng kết nối. Nếu
    cur.execute["""
        INSERT INTO movie VALUES
            ['Monty Python and the Holy Grail', 1975, 8.2],
            ['And Now for Something Completely Different', 1971, 7.5]
    """]
    
    107, kết nối có thể được chia sẻ trên nhiều luồng;

  • nhà máy [Kết nối] – Một lớp con tùy chỉnh của

    cur.execute["""
        INSERT INTO movie VALUES
            ['Monty Python and the Holy Grail', 1975, 8.2],
            ['And Now for Something Completely Different', 1971, 7.5]
    """]
    
    7 để tạo kết nối với, nếu không phải là lớp
    cur.execute["""
        INSERT INTO movie VALUES
            ['Monty Python and the Holy Grail', 1975, 8.2],
            ['And Now for Something Completely Different', 1971, 7.5]
    """]
    
    7 mặc định

  • cached_statements [int] – Số câu lệnh mà

    cur.execute["""
        INSERT INTO movie VALUES
            ['Monty Python and the Holy Grail', 1975, 8.2],
            ['And Now for Something Completely Different', 1971, 7.5]
    """]
    
    0 sẽ lưu vào bộ nhớ đệm nội bộ cho kết nối này, để tránh phân tích cú pháp chi phí. Theo mặc định, 128 câu lệnh

  • uri [bool] – Nếu được đặt thành

    cur.execute["""
        INSERT INTO movie VALUES
            ['Monty Python and the Holy Grail', 1975, 8.2],
            ['And Now for Something Completely Different', 1971, 7.5]
    """]
    
    106, cơ sở dữ liệu được hiểu là URI với đường dẫn tệp và chuỗi truy vấn tùy chọn. Phần lược đồ phải là
    cur.execute["""
        INSERT INTO movie VALUES
            ['Monty Python and the Holy Grail', 1975, 8.2],
            ['And Now for Something Completely Different', 1971, 7.5]
    """]
    
    112 và đường dẫn có thể là tương đối hoặc tuyệt đối. Chuỗi truy vấn cho phép chuyển tham số sang SQLite, cho phép Cách làm việc với URI SQLite khác nhau.

loại trả lại

Sự liên quan

Tăng sự kiện kiểm tra

cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
113 với đối số
cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
114.

Tăng sự kiện kiểm tra

cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
115 với đối số
cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
116.

Mới trong phiên bản 3. 4. Tham số uri.

Đã thay đổi trong phiên bản 3. 7. cơ sở dữ liệu giờ đây cũng có thể là một đối tượng dạng đường dẫn , không chỉ là một chuỗi.

Mới trong phiên bản 3. 10. Sự kiện kiểm toán

cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
115.

sqlite3. complete_statement[câu lệnh]

Trả lại

cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
106 nếu câu lệnh chuỗi dường như chứa một hoặc nhiều câu lệnh SQL hoàn chỉnh. Không có xác minh cú pháp hoặc phân tích cú pháp dưới bất kỳ hình thức nào được thực hiện, ngoài việc kiểm tra để đảm bảo rằng không có chuỗi ký tự không được đóng và câu lệnh được kết thúc bằng dấu chấm phẩy

Ví dụ

cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
11

Chức năng này có thể hữu ích trong quá trình nhập dòng lệnh để xác định xem văn bản đã nhập có phải là một câu lệnh SQL hoàn chỉnh hay không hoặc nếu cần nhập thêm trước khi gọi

cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
119

sqlite3. enable_callback_tracebacks[flag , /]

Bật hoặc tắt theo dõi cuộc gọi lại. Theo mặc định, bạn sẽ không nhận được bất kỳ dấu vết nào trong các chức năng do người dùng xác định, tổng hợp, trình chuyển đổi, lệnh gọi lại của người ủy quyền, v.v. Nếu bạn muốn gỡ lỗi chúng, bạn có thể gọi chức năng này với cờ được đặt thành

cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
106. Sau đó, bạn sẽ nhận được dấu vết từ các cuộc gọi lại trên
cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
121. Sử dụng
cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
107 để tắt lại tính năng này

Đăng ký một

cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
123 để có trải nghiệm gỡ lỗi được cải thiện

cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
12

sqlite3. register_adapter[loại , bộ chuyển đổi, /]

Đăng ký bộ điều hợp có thể gọi được để điều chỉnh loại Python thành loại SQLite. Bộ điều hợp được gọi với một đối tượng Python thuộc loại làm đối số duy nhất của nó và phải trả về giá trị của loại mà SQLite vốn hiểu .

sqlite3. register_converter[typename , converter, /]

Đăng ký trình chuyển đổi có thể gọi được để chuyển đổi các đối tượng SQLite thuộc loại tên thành một đối tượng Python thuộc một loại cụ thể. Trình chuyển đổi được gọi cho tất cả các giá trị SQLite của kiểu tên; . Tham khảo tham số detect_types của

cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
125 để biết thông tin về cách hoạt động của tính năng phát hiện loại

Ghi chú. tên loại và tên của loại trong truy vấn của bạn được đối sánh không phân biệt chữ hoa chữ thường

Hằng số mô-đun¶

sqlite3. PARSE_COLNAMES

Chuyển giá trị cờ này cho tham số detect_types của

cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
125 để tra cứu hàm chuyển đổi bằng cách sử dụng tên loại, được phân tích cú pháp từ tên cột truy vấn, làm khóa từ điển chuyển đổi. Tên loại phải được đặt trong dấu ngoặc vuông [______1127]

cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
13

Cờ này có thể được kết hợp với

cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
76 bằng cách sử dụng toán tử
cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
75 [theo bit hoặc]

sqlite3. PARSE_DECLTYPES

Chuyển giá trị cờ này cho tham số detect_types của

cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
125 để tra cứu hàm chuyển đổi bằng cách sử dụng các loại đã khai báo cho mỗi cột. Các loại được khai báo khi bảng cơ sở dữ liệu được tạo.
cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
0 sẽ tra cứu hàm chuyển đổi bằng cách sử dụng từ đầu tiên của loại được khai báo làm khóa từ điển chuyển đổi. Ví dụ

cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
14

Cờ này có thể được kết hợp với

cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
77 bằng cách sử dụng toán tử
cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
75 [theo bit hoặc]

sqlite3. SQLITE_OKsqlite3. SQLITE_DENYsqlite3. SQLITE_IGNORE

Các cờ sẽ được trả về bởi khả năng gọi lại ủy quyền_callback được chuyển đến

cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
134, để cho biết liệu

  • Truy cập được cho phép [

    cur.execute["""
        INSERT INTO movie VALUES
            ['Monty Python and the Holy Grail', 1975, 8.2],
            ['And Now for Something Completely Different', 1971, 7.5]
    """]
    
    135],

  • Câu lệnh SQL sẽ bị hủy bỏ khi có lỗi [

    cur.execute["""
        INSERT INTO movie VALUES
            ['Monty Python and the Holy Grail', 1975, 8.2],
            ['And Now for Something Completely Different', 1971, 7.5]
    """]
    
    136]

  • Cột phải được coi là giá trị

    cur.execute["""
        INSERT INTO movie VALUES
            ['Monty Python and the Holy Grail', 1975, 8.2],
            ['And Now for Something Completely Different', 1971, 7.5]
    """]
    
    137 [
    cur.execute["""
        INSERT INTO movie VALUES
            ['Monty Python and the Holy Grail', 1975, 8.2],
            ['And Now for Something Completely Different', 1971, 7.5]
    """]
    
    138]

sqlite3. apilevel

Hằng số chuỗi cho biết mức DB-API được hỗ trợ. Yêu cầu bởi DB-API. Mã hóa cứng thành

cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
139

sqlite3. paramstyle

Hằng số chuỗi cho biết loại định dạng đánh dấu tham số mà mô-đun

cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
0 mong đợi. Yêu cầu bởi DB-API. Mã hóa cứng thành
cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
141

Ghi chú

Kiểu tham số DB-API

cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
142 cũng được hỗ trợ

sqlite3. sqlite_version

Số phiên bản của thư viện SQLite thời gian chạy dưới dạng

cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
143

sqlite3. sqlite_version_info

Số phiên bản của thư viện SQLite thời gian chạy là

cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
09 của
cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
145

sqlite3. an toàn luồng

Hằng số nguyên theo yêu cầu của DB-API 2. 0, cho biết mức độ an toàn của luồng mà mô-đun

cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
0 hỗ trợ. Thuộc tính này được đặt dựa trên chế độ phân luồng mặc định mà thư viện SQLite bên dưới được biên dịch với. Các chế độ luồng SQLite là

  1. đơn luồng. Trong chế độ này, tất cả các mutex đều bị vô hiệu hóa và SQLite không an toàn khi sử dụng trong nhiều luồng đơn lẻ cùng một lúc

  2. đa luồng. Trong chế độ này, SQLite có thể được sử dụng an toàn bởi nhiều luồng với điều kiện là không có kết nối cơ sở dữ liệu đơn lẻ nào được sử dụng đồng thời trong hai hoặc nhiều luồng

  3. nối tiếp. Ở chế độ tuần tự hóa, SQLite có thể được nhiều luồng sử dụng một cách an toàn mà không bị hạn chế

Ánh xạ từ các chế độ luồng SQLite sang DB-API 2. 0 mức độ an toàn luồng như sau

Chế độ luồng SQLite

chủ đề an toàn

SQLITE_THREADSAFE

DB-API 2. 0 ý nghĩa

đơn luồng

0

0

Chủ đề có thể không chia sẻ mô-đun

đa luồng

1

2

Chủ đề có thể chia sẻ mô-đun, nhưng không kết nối

nối tiếp

3

1

Chủ đề có thể chia sẻ mô-đun, kết nối và con trỏ

Đã thay đổi trong phiên bản 3. 11. Đặt động luồng an toàn thay vì mã hóa cứng thành

cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
147.

sqlite3. phiên bản

Số phiên bản của mô-đun này là

cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
143. Đây không phải là phiên bản của thư viện SQLite

sqlite3. version_info

Số phiên bản của mô-đun này là

cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
09 của
cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
145. Đây không phải là phiên bản của thư viện SQLite

Đối tượng kết nối¶

lớp sqlite3. Kết nối

Mỗi cơ sở dữ liệu SQLite mở được đại diện bởi một đối tượng

cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
7, đối tượng này được tạo bằng cách sử dụng
cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
5. Mục đích chính của chúng là tạo ra các đối tượng
cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
00 và Kiểm soát giao dịch .

Xem thêm

  • Cách sử dụng các phương pháp phím tắt kết nối

  • Cách sử dụng trình quản lý bối cảnh kết nối

Kết nối cơ sở dữ liệu SQLite có các thuộc tính và phương thức sau

con trỏ[nhà máy=Con trỏ]

Tạo và trả về một đối tượng

cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
00. Phương thức con trỏ chấp nhận một nhà máy tham số tùy chọn duy nhất. Nếu được cung cấp, đây phải là một phiên bản có thể gọi được trả về một phiên bản của
cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
00 hoặc các lớp con của nó

blobopen[bảng , cột, row, /, *, readonly=False, name='main']

Mở một điều khiển

cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
156 cho một BLOB hiện có

Thông số
  • table [str] – Tên của bảng chứa blob

  • cột [str] – Tên của cột chứa đốm màu

  • row [str] – Tên của hàng chứa đốm màu

  • chỉ đọc [bool] – Đặt thành

    cur.execute["""
        INSERT INTO movie VALUES
            ['Monty Python and the Holy Grail', 1975, 8.2],
            ['And Now for Something Completely Different', 1971, 7.5]
    """]
    
    106 nếu blob được mở mà không có quyền ghi. Mặc định là
    cur.execute["""
        INSERT INTO movie VALUES
            ['Monty Python and the Holy Grail', 1975, 8.2],
            ['And Now for Something Completely Different', 1971, 7.5]
    """]
    
    107

  • name [str] – Tên của cơ sở dữ liệu chứa blob. Mặc định là

    cur.execute["""
        INSERT INTO movie VALUES
            ['Monty Python and the Holy Grail', 1975, 8.2],
            ['And Now for Something Completely Different', 1971, 7.5]
    """]
    
    159

tăng

OperationalError - Khi cố gắng mở một đốm màu trong bảng

cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
160

loại trả lại

Bãi

Ghi chú

Không thể thay đổi kích thước đốm màu bằng lớp

cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
156. Sử dụng hàm SQL
cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
162 để tạo đốm màu có kích thước cố định

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

cam kết[]

Cam kết mọi giao dịch đang chờ xử lý vào cơ sở dữ liệu. Nếu không có giao dịch mở, phương pháp này là không hoạt động

rollback[]

Quay lại điểm bắt đầu của bất kỳ giao dịch đang chờ xử lý nào. Nếu không có giao dịch mở, phương pháp này là không hoạt động

đóng[]

Đóng kết nối cơ sở dữ liệu. Bất kỳ giao dịch đang chờ xử lý nào đều không được cam kết ngầm;

thực thi[sql , tham số=[], /]

Tạo một đối tượng

cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
00 mới và gọi
cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
119 trên đó với sql và tham số đã cho. Trả về đối tượng con trỏ mới

executemany[sql , tham số, /]

Tạo một đối tượng

cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
00 mới và gọi
cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
167 trên đó với sql và tham số đã cho. Trả về đối tượng con trỏ mới

executescript[sql_script , /]

Tạo một đối tượng

cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
00 mới và gọi
cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
169 trên đó với sql_script đã cho. Trả về đối tượng con trỏ mới

create_function[tên , narg, func, *, deterministic=False]

Tạo hoặc xóa hàm SQL do người dùng định nghĩa

Thông số
  • name [str] – Tên của hàm SQL

  • narg [int] – Số lượng đối số mà hàm SQL có thể chấp nhận. Nếu

    cur.execute["""
        INSERT INTO movie VALUES
            ['Monty Python and the Holy Grail', 1975, 8.2],
            ['And Now for Something Completely Different', 1971, 7.5]
    """]
    
    170, nó có thể nhận bất kỳ số đối số nào

  • func [ gọi lại . Không có] – Có thể gọi được gọi khi hàm SQL được gọi. Có thể gọi được phải trả về một loại vốn được hỗ trợ bởi SQLite . Đặt thành

    cur.execute["""
        INSERT INTO movie VALUES
            ['Monty Python and the Holy Grail', 1975, 8.2],
            ['And Now for Something Completely Different', 1971, 7.5]
    """]
    
    43 để xóa hàm SQL hiện có.

  • tất định [bool] – Nếu

    cur.execute["""
        INSERT INTO movie VALUES
            ['Monty Python and the Holy Grail', 1975, 8.2],
            ['And Now for Something Completely Different', 1971, 7.5]
    """]
    
    106, hàm SQL đã tạo được đánh dấu là tất định, cho phép SQLite thực hiện các tối ưu hóa bổ sung

tăng

NotSupportedError - Nếu xác định được sử dụng với các phiên bản SQLite cũ hơn 3. 8. 3

Mới trong phiên bản 3. 8. Tham số xác định.

Ví dụ

cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
15

create_aggregate[tên , /, n_arg, aggregate_class]

Tạo hoặc xóa hàm tổng hợp SQL do người dùng xác định

Thông số
  • name [str] – Tên của hàm tổng hợp SQL

  • n_arg [int] – Số lượng đối số mà hàm tổng hợp SQL có thể chấp nhận. Nếu

    cur.execute["""
        INSERT INTO movie VALUES
            ['Monty Python and the Holy Grail', 1975, 8.2],
            ['And Now for Something Completely Different', 1971, 7.5]
    """]
    
    170, nó có thể nhận bất kỳ số đối số nào

  • lớp_tổng hợp [ lớp . Không có] –

    Một lớp phải thực hiện các phương pháp sau

    • cur.execute["""
          INSERT INTO movie VALUES
              ['Monty Python and the Holy Grail', 1975, 8.2],
              ['And Now for Something Completely Different', 1971, 7.5]
      """]
      
      174. Thêm một hàng vào tổng hợp

    • ____1175. Trả về kết quả cuối cùng của tổng hợp dưới dạng một loại vốn được SQLite hỗ trợ .

    Số lượng đối số mà phương thức

    cur.execute["""
        INSERT INTO movie VALUES
            ['Monty Python and the Holy Grail', 1975, 8.2],
            ['And Now for Something Completely Different', 1971, 7.5]
    """]
    
    174 phải chấp nhận được kiểm soát bởi n_arg

    Đặt thành

    cur.execute["""
        INSERT INTO movie VALUES
            ['Monty Python and the Holy Grail', 1975, 8.2],
            ['And Now for Something Completely Different', 1971, 7.5]
    """]
    
    43 để xóa hàm tổng hợp SQL hiện có

Ví dụ

cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
16

create_window_function[tên , num_params, aggregate_class, /]

Tạo hoặc xóa chức năng cửa sổ tổng hợp do người dùng xác định

Thông số
  • name [str] – Tên của hàm cửa sổ tổng hợp SQL để tạo hoặc xóa

  • num_params [int] – Số lượng đối số mà hàm cửa sổ tổng hợp SQL có thể chấp nhận. Nếu

    cur.execute["""
        INSERT INTO movie VALUES
            ['Monty Python and the Holy Grail', 1975, 8.2],
            ['And Now for Something Completely Different', 1971, 7.5]
    """]
    
    170, nó có thể nhận bất kỳ số đối số nào

  • lớp_tổng hợp [ lớp . Không có] –

    Một lớp phải thực hiện các phương thức sau

    • cur.execute["""
          INSERT INTO movie VALUES
              ['Monty Python and the Holy Grail', 1975, 8.2],
              ['And Now for Something Completely Different', 1971, 7.5]
      """]
      
      174. Thêm một hàng vào cửa sổ hiện tại

    • cur.execute["""
          INSERT INTO movie VALUES
              ['Monty Python and the Holy Grail', 1975, 8.2],
              ['And Now for Something Completely Different', 1971, 7.5]
      """]
      
      180. Trả về giá trị hiện tại của tổng hợp

    • cur.execute["""
          INSERT INTO movie VALUES
              ['Monty Python and the Holy Grail', 1975, 8.2],
              ['And Now for Something Completely Different', 1971, 7.5]
      """]
      
      181. Xóa một hàng khỏi cửa sổ hiện tại

    • ____1175. Trả về kết quả cuối cùng của tổng hợp dưới dạng một loại vốn được SQLite hỗ trợ .

    Số lượng đối số mà các phương thức

    cur.execute["""
        INSERT INTO movie VALUES
            ['Monty Python and the Holy Grail', 1975, 8.2],
            ['And Now for Something Completely Different', 1971, 7.5]
    """]
    
    174 và
    cur.execute["""
        INSERT INTO movie VALUES
            ['Monty Python and the Holy Grail', 1975, 8.2],
            ['And Now for Something Completely Different', 1971, 7.5]
    """]
    
    180 phải chấp nhận được kiểm soát bởi num_params

    Đặt thành

    cur.execute["""
        INSERT INTO movie VALUES
            ['Monty Python and the Holy Grail', 1975, 8.2],
            ['And Now for Something Completely Different', 1971, 7.5]
    """]
    
    43 để xóa chức năng cửa sổ tổng hợp SQL hiện có

tăng

NotSupportedError – Nếu được sử dụng với phiên bản SQLite cũ hơn 3. 25. 0, không hỗ trợ các chức năng cửa sổ tổng hợp

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

Ví dụ

cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
17

create_collation[tên , có thể gọi]

Tạo một đối chiếu có tên name bằng chức năng đối chiếu có thể gọi được. có thể gọi được thông qua hai đối số

cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
143 và nó sẽ trả về một
cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
187

  • cur.execute["""
        INSERT INTO movie VALUES
            ['Monty Python and the Holy Grail', 1975, 8.2],
            ['And Now for Something Completely Different', 1971, 7.5]
    """]
    
    147 nếu cái đầu tiên được đặt cao hơn cái thứ hai

  • cur.execute["""
        INSERT INTO movie VALUES
            ['Monty Python and the Holy Grail', 1975, 8.2],
            ['And Now for Something Completely Different', 1971, 7.5]
    """]
    
    170 nếu cái đầu tiên được đặt thấp hơn cái thứ hai

  • cur.execute["""
        INSERT INTO movie VALUES
            ['Monty Python and the Holy Grail', 1975, 8.2],
            ['And Now for Something Completely Different', 1971, 7.5]
    """]
    
    100 nếu chúng được sắp xếp bằng nhau

Ví dụ sau đây cho thấy một đối chiếu sắp xếp ngược lại

cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
18

Xóa chức năng đối chiếu bằng cách đặt có thể gọi thành

cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
43

Đã thay đổi trong phiên bản 3. 11. Tên đối chiếu có thể chứa bất kỳ ký tự Unicode nào. Trước đó, chỉ các ký tự ASCII được phép.

gián đoạn[]

Gọi phương thức này từ một luồng khác để hủy bỏ mọi truy vấn có thể đang thực thi trên kết nối. Các truy vấn bị hủy bỏ sẽ đưa ra một ngoại lệ

set_authorizer[authorizer_callback]

Đăng ký ủy quyền có thể gọi được để được gọi cho mỗi lần cố gắng truy cập một cột của bảng trong cơ sở dữ liệu. Cuộc gọi lại phải trả về một trong số

cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
135,
cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
136 hoặc
cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
138 để báo hiệu cách thư viện SQLite bên dưới xử lý quyền truy cập vào cột

Đối số đầu tiên cho cuộc gọi lại biểu thị loại hoạt động nào sẽ được ủy quyền. Đối số thứ hai và thứ ba sẽ là đối số hoặc

cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
43 tùy thuộc vào đối số thứ nhất. Đối số thứ 4 là tên của cơ sở dữ liệu [“main”, “temp”, v.v. ] nếu có. Đối số thứ 5 là tên của trình kích hoạt hoặc chế độ xem trong cùng chịu trách nhiệm cho nỗ lực truy cập hoặc
cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
43 nếu nỗ lực truy cập này trực tiếp từ mã SQL đầu vào

Vui lòng tham khảo tài liệu SQLite về các giá trị có thể có cho đối số thứ nhất và ý nghĩa của đối số thứ hai và thứ ba tùy thuộc vào đối số thứ nhất. Tất cả các hằng số cần thiết đều có sẵn trong mô-đun

cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
0

Chuyển

cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
43 dưới dạng ủy quyền_callback sẽ vô hiệu hóa trình ủy quyền

Đã thay đổi trong phiên bản 3. 11. Đã thêm hỗ trợ để tắt trình ủy quyền bằng cách sử dụng

cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
43.

set_progress_handler[progress_handler , n]

Đăng ký process_handler có thể gọi để được gọi cho mỗi n hướng dẫn của máy ảo SQLite. Điều này hữu ích nếu bạn muốn được gọi từ SQLite trong các hoạt động chạy dài, chẳng hạn như để cập nhật GUI

Nếu bạn muốn xóa bất kỳ trình xử lý tiến trình nào đã cài đặt trước đó, hãy gọi phương thức bằng

cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
43 cho process_handler

Trả về một giá trị khác 0 từ hàm xử lý sẽ chấm dứt truy vấn hiện đang thực thi và khiến truy vấn tăng ngoại lệ

cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
501

set_trace_callback[trace_callback]

Đăng ký dấu vết gọi lại có thể gọi được để được gọi cho mỗi câu lệnh SQL thực sự được thực thi bởi phần phụ trợ SQLite

Đối số duy nhất được truyền cho lệnh gọi lại là câu lệnh [dưới dạng

cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
79] đang được thực thi. Giá trị trả về của cuộc gọi lại bị bỏ qua. Lưu ý rằng chương trình phụ trợ không chỉ chạy các câu lệnh được truyền cho các phương thức
cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
503. Các nguồn khác bao gồm quản lý giao dịch của mô-đun
cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
0 và việc thực thi các trình kích hoạt được xác định trong cơ sở dữ liệu hiện tại.

Vượt qua

cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
43 dưới dạng trace_callback sẽ vô hiệu hóa lệnh gọi lại theo dõi

Ghi chú

Các ngoại lệ được đưa ra trong cuộc gọi lại theo dõi không được phổ biến. Là một công cụ hỗ trợ phát triển và gỡ lỗi, hãy sử dụng

cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
506 để cho phép in truy nguyên từ các ngoại lệ được đưa ra trong lệnh gọi lại theo dõi

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

enable_load_extension[đã bật , /]

Cho phép công cụ SQLite tải các phần mở rộng SQLite từ thư viện dùng chung nếu được bật là

cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
106; . Các tiện ích mở rộng SQLite có thể xác định các chức năng mới, tổng hợp hoặc triển khai bảng ảo hoàn toàn mới. Một phần mở rộng nổi tiếng là phần mở rộng tìm kiếm toàn văn được phân phối với SQLite

Ghi chú

Mô-đun

cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
0 không được xây dựng với hỗ trợ tiện ích mở rộng có thể tải theo mặc định, vì một số nền tảng [đặc biệt là macOS] có các thư viện SQLite được biên dịch mà không có tính năng này. Để nhận hỗ trợ tiện ích mở rộng có thể tải, bạn phải vượt qua tùy chọn
cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
509 để định cấu hình

Tăng sự kiện kiểm tra

cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
510 với các đối số
cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
511,
cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
512.

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

Đã thay đổi trong phiên bản 3. 10. Đã thêm sự kiện kiểm tra

cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
510.

cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
19

load_extension[đường dẫn , /]

Tải tiện ích mở rộng SQLite từ thư viện dùng chung có tại đường dẫn. Cho phép tải tiện ích mở rộng bằng

cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
514 trước khi gọi phương thức này

Tăng sự kiện kiểm tra

cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
515 với các đối số
cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
511,
cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
517.

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

Đã thay đổi trong phiên bản 3. 10. Đã thêm sự kiện kiểm tra

cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
515.

iterdump[]

Trả về một trình lặp để kết xuất cơ sở dữ liệu dưới dạng mã nguồn SQL. Hữu ích khi lưu cơ sở dữ liệu trong bộ nhớ để phục hồi sau này. Tương tự như lệnh

cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
519 trong shell sqlite3.

Ví dụ

cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
50

dự phòng[mục tiêu , *, pages=- 1, progress=None, name='main', sleep=0.250]

Tạo bản sao lưu của cơ sở dữ liệu SQLite

Hoạt động ngay cả khi cơ sở dữ liệu đang được truy cập bởi các máy khách khác hoặc đồng thời bởi cùng một kết nối

Thông số
  • đích [Kết nối] – Kết nối cơ sở dữ liệu để lưu bản sao lưu vào

  • pages [int] – Số trang cần sao chép cùng một lúc. Nếu bằng hoặc nhỏ hơn

    cur.execute["""
        INSERT INTO movie VALUES
            ['Monty Python and the Holy Grail', 1975, 8.2],
            ['And Now for Something Completely Different', 1971, 7.5]
    """]
    
    100, toàn bộ cơ sở dữ liệu sẽ được sao chép trong một bước. Mặc định là
    cur.execute["""
        INSERT INTO movie VALUES
            ['Monty Python and the Holy Grail', 1975, 8.2],
            ['And Now for Something Completely Different', 1971, 7.5]
    """]
    
    170

  • tiến trình [ gọi lại . Không có] – Nếu được đặt thành có thể gọi được, thì nó được gọi với ba đối số nguyên cho mỗi lần lặp sao lưu. trạng thái của lần lặp cuối cùng, số trang còn lại vẫn được sao chép và tổng số trang. Mặc định là

    cur.execute["""
        INSERT INTO movie VALUES
            ['Monty Python and the Holy Grail', 1975, 8.2],
            ['And Now for Something Completely Different', 1971, 7.5]
    """]
    
    43.

  • name [str] – Tên của cơ sở dữ liệu để sao lưu.

    cur.execute["""
        INSERT INTO movie VALUES
            ['Monty Python and the Holy Grail', 1975, 8.2],
            ['And Now for Something Completely Different', 1971, 7.5]
    """]
    
    159 [mặc định] cho cơ sở dữ liệu chính,
    cur.execute["""
        INSERT INTO movie VALUES
            ['Monty Python and the Holy Grail', 1975, 8.2],
            ['And Now for Something Completely Different', 1971, 7.5]
    """]
    
    524 cho cơ sở dữ liệu tạm thời hoặc tên của cơ sở dữ liệu tùy chỉnh như được đính kèm bằng cách sử dụng câu lệnh SQL
    cur.execute["""
        INSERT INTO movie VALUES
            ['Monty Python and the Holy Grail', 1975, 8.2],
            ['And Now for Something Completely Different', 1971, 7.5]
    """]
    
    525

  • ngủ [float] – Số giây để ngủ giữa các lần thử liên tiếp để sao lưu các trang còn lại

Ví dụ 1, sao chép một cơ sở dữ liệu hiện có sang một cơ sở dữ liệu khác

cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
51

Ví dụ 2, sao chép cơ sở dữ liệu hiện có vào một bản sao tạm thời

cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
52

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

getlimit[danh mục , /]

Nhận giới hạn thời gian chạy kết nối

Thông số

danh mục [int] – Danh mục giới hạn SQLite được truy vấn

loại trả lại

int

tăng

Lỗi lập trình - Nếu danh mục không được thư viện SQLite bên dưới nhận dạng

Ví dụ, truy vấn độ dài tối đa của câu lệnh SQL cho

cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
7
cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
8 [mặc định là 1000000000]

cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
53

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

setlimit[danh mục , giới hạn, /]

Đặt giới hạn thời gian chạy kết nối. Nỗ lực tăng giới hạn trên giới hạn trên cứng của nó bị cắt ngắn một cách lặng lẽ đến giới hạn trên cứng. Bất kể giới hạn có bị thay đổi hay không, giá trị trước đó của giới hạn được trả về

Thông số
  • danh mục [int] – Danh mục giới hạn SQLite sẽ được đặt

  • giới hạn [int] – Giá trị của giới hạn mới. Nếu âm, giới hạn hiện tại không thay đổi

loại trả lại

int

tăng

Lỗi lập trình - Nếu danh mục không được thư viện SQLite bên dưới nhận dạng

Ví dụ, giới hạn số lượng cơ sở dữ liệu đính kèm là 1 cho

cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
7
cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
8 [giới hạn mặc định là 10]

cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
54

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

xếp thứ tự[* , tên='main']

Tuần tự hóa cơ sở dữ liệu thành một đối tượng

cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
124. Đối với một tệp cơ sở dữ liệu trên đĩa thông thường, việc tuần tự hóa chỉ là một bản sao của tệp đĩa. Đối với cơ sở dữ liệu trong bộ nhớ hoặc cơ sở dữ liệu “tạm thời”, tuần tự hóa là cùng một chuỗi byte sẽ được ghi vào đĩa nếu cơ sở dữ liệu đó được sao lưu vào đĩa

Thông số

name [str] – Tên cơ sở dữ liệu sẽ được sắp xếp theo thứ tự. Mặc định là

cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
159

loại trả lại

byte

Ghi chú

Phương pháp này chỉ khả dụng nếu thư viện SQLite cơ bản có API tuần tự hóa

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

giải tuần tự hóa[dữ liệu , /, *, name='main']

Giải tuần tự hóa cơ sở dữ liệu

cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
532 thành một
cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
7. Phương pháp này khiến kết nối cơ sở dữ liệu bị ngắt kết nối khỏi tên cơ sở dữ liệu và mở lại tên dưới dạng cơ sở dữ liệu trong bộ nhớ dựa trên tuần tự hóa có trong dữ liệu

Thông số
  • dữ liệu [byte] – Cơ sở dữ liệu tuần tự hóa

  • name [str] – Tên cơ sở dữ liệu để giải tuần tự hóa thành. Mặc định là

    cur.execute["""
        INSERT INTO movie VALUES
            ['Monty Python and the Holy Grail', 1975, 8.2],
            ['And Now for Something Completely Different', 1971, 7.5]
    """]
    
    159

tăng
  • OperationalError – Nếu kết nối cơ sở dữ liệu hiện đang tham gia vào một giao dịch đọc hoặc một hoạt động sao lưu

  • DatabaseError – Nếu dữ liệu không chứa cơ sở dữ liệu SQLite hợp lệ

  • OverflowError - Nếu

    cur.execute["""
        INSERT INTO movie VALUES
            ['Monty Python and the Holy Grail', 1975, 8.2],
            ['And Now for Something Completely Different', 1971, 7.5]
    """]
    
    535 lớn hơn
    cur.execute["""
        INSERT INTO movie VALUES
            ['Monty Python and the Holy Grail', 1975, 8.2],
            ['And Now for Something Completely Different', 1971, 7.5]
    """]
    
    536

Ghi chú

Phương pháp này chỉ khả dụng nếu thư viện SQLite bên dưới có API deserialize

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

in_transaction

Thuộc tính chỉ đọc này tương ứng với chế độ tự động ký SQLite cấp thấp

cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
106 nếu một giao dịch đang hoạt động [có những thay đổi không được cam kết],
cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
107 nếu không

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

cấp_độ cô lập

Thuộc tính này kiểm soát xử lý giao dịch do

cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
0 thực hiện. Nếu được đặt thành
cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
43, các giao dịch sẽ không bao giờ được mở hoàn toàn. Nếu được đặt thành một trong số
cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
102,
cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
104 hoặc
cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
103, tương ứng với hành vi giao dịch SQLite cơ bản, thì quản lý giao dịch ngầm được thực hiện.

Nếu không bị ghi đè bởi tham số Isolation_level của

cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
125, giá trị mặc định là
cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
545, là bí danh của
cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
102

row_factory

cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
547 ban đầu cho các đối tượng
cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
00 được tạo từ kết nối này. Việc gán cho thuộc tính này không ảnh hưởng đến
cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
547 con trỏ hiện có thuộc kết nối này, chỉ những con trỏ mới. Là
cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
43 theo mặc định, nghĩa là mỗi hàng được trả về dưới dạng
cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
09

Xem Cách tạo và sử dụng các xưởng tạo hàng để biết thêm chi tiết.

text_factory

Một khả năng gọi được chấp nhận một tham số

cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
124 và trả về một đại diện văn bản của nó. Có thể gọi được gọi cho các giá trị SQLite với kiểu dữ liệu
cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
553. Theo mặc định, thuộc tính này được đặt thành
cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
79. Thay vào đó, nếu bạn muốn trả lại
cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
124, hãy đặt text_factory thành
cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
124

Ví dụ

cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
55

total_changes

Trả về tổng số hàng cơ sở dữ liệu đã được sửa đổi, chèn hoặc xóa kể từ khi kết nối cơ sở dữ liệu được mở

Đối tượng con trỏ¶

Một đối tượng

cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
00 đại diện cho một con trỏ cơ sở dữ liệu được sử dụng để thực thi các câu lệnh SQL và quản lý ngữ cảnh của thao tác tìm nạp. Con trỏ được tạo bằng cách sử dụng
cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
558 hoặc bằng cách sử dụng bất kỳ phương thức phím tắt kết nối nào .

Đối tượng con trỏ là trình lặp , nghĩa là nếu bạn

cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
119 một truy vấn
cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
48, bạn có thể chỉ cần lặp lại con trỏ để tìm nạp các hàng kết quả.

cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
56

lớp sqlite3. Con trỏ

Một phiên bản

cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
00 có các thuộc tính và phương thức sau

thực thi[sql , tham số=[], /]

Thực thi câu lệnh SQL sql. Liên kết các giá trị với câu lệnh bằng cách sử dụng phần giữ chỗ ánh xạ tới trình tự hoặc

cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
562 .

cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
119 sẽ chỉ thực thi một câu lệnh SQL duy nhất. Nếu bạn cố gắng thực hiện nhiều hơn một câu lệnh với nó, nó sẽ gây ra lỗi
cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
564. Sử dụng
cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
169 nếu bạn muốn thực thi nhiều câu lệnh SQL với một lệnh gọi

Nếu

cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
101 không phải là
cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
43, thì sql là một câu lệnh
cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
44,
cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
569,
cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
570 hoặc
cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
571 và không có giao dịch mở, một giao dịch được mở hoàn toàn trước khi thực hiện sql

executemany[sql , tham số, /]

Thực thi được tham số hóa Câu lệnh SQL sql đối với tất cả các trình tự tham số hoặc ánh xạ được tìm thấy trong các tham số trình tự. Cũng có thể sử dụng một iterator cung cấp các tham số thay vì một chuỗi. Sử dụng xử lý giao dịch ngầm giống như

cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
119.

Ví dụ

cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
57

executescript[sql_script , /]

Thực thi các câu lệnh SQL trong sql_script. Nếu có một giao dịch đang chờ xử lý, một câu lệnh ngầm

cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
573 sẽ được thực hiện trước. Không có kiểm soát giao dịch ngầm nào khác được thực hiện;

sql_script phải là một

cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
143

Ví dụ

cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
58

tìm nạp[]

Nếu

cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
547 là
cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
43, trả về tập kết quả truy vấn hàng tiếp theo là một
cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
09. Khác, chuyển nó đến nhà máy sản xuất hàng và trả về kết quả của nó. Trả lại
cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
43 nếu không có thêm dữ liệu

fetchmany[size=con trỏ. kích thước mảng]

Trả về nhóm hàng tiếp theo của kết quả truy vấn dưới dạng

cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
72. Trả về danh sách trống nếu không còn hàng nào nữa

Số lượng hàng cần tìm nạp cho mỗi cuộc gọi được chỉ định bởi tham số kích thước. Nếu kích thước không được cung cấp,

cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
580 sẽ xác định số lượng hàng sẽ được tìm nạp. Nếu có ít hàng hơn kích thước, số hàng có sẵn sẽ được trả về

Lưu ý rằng có những cân nhắc về hiệu suất liên quan đến tham số kích thước. Để có hiệu suất tối ưu, tốt nhất nên sử dụng thuộc tính arraysize. Nếu tham số kích thước được sử dụng, thì tốt nhất là giữ nguyên giá trị từ cuộc gọi

cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
581 này sang cuộc gọi tiếp theo

tìm nạp[]

Trả về tất cả các hàng [còn lại] của kết quả truy vấn dưới dạng

cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
72. Trả về danh sách trống nếu không có hàng nào. Lưu ý rằng thuộc tính
cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
580 có thể ảnh hưởng đến hiệu suất của thao tác này

đóng[]

Đóng con trỏ ngay bây giờ [chứ không phải bất cứ khi nào

cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
584 được gọi]

Con trỏ sẽ không sử dụng được từ thời điểm này trở đi;

setinputsizes[kích thước , /]

Yêu cầu bởi DB-API. Không làm gì trong

cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
0

setoutputsize[kích thước , cột=None, /]

Yêu cầu bởi DB-API. Không làm gì trong

cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
0

kích thước mảng

Thuộc tính đọc/ghi kiểm soát số hàng được trả về bởi

cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
581. Giá trị mặc định là 1 có nghĩa là một hàng sẽ được tìm nạp cho mỗi cuộc gọi

kết nối

Read-only attribute that provides the SQLite database

cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
7 belonging to the cursor. Đối tượng
cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
00 được tạo bằng cách gọi
cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
9 sẽ có thuộc tính
cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
511 đề cập đến con

cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
59

mô tả

Thuộc tính chỉ đọc cung cấp tên cột của truy vấn cuối cùng. Để duy trì khả năng tương thích với API Python DB, nó trả về 7-bộ cho mỗi cột trong đó sáu mục cuối cùng của mỗi bộ là

cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
43

Nó cũng được đặt cho các câu lệnh

cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
48 mà không có bất kỳ hàng nào phù hợp

lastrowid

Thuộc tính chỉ đọc cung cấp id hàng của hàng được chèn cuối cùng. Nó chỉ được cập nhật sau khi câu lệnh

cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
44 hoặc
cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
571 thành công sử dụng phương thức
cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
119. Đối với các câu lệnh khác, sau
cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
167 hoặc
cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
169 hoặc nếu chèn không thành công, giá trị của
cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
100 sẽ không thay đổi. Giá trị ban đầu của
cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
100 là
cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
43

Ghi chú

Chèn vào bảng

cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
160 không được ghi lại

Đã thay đổi trong phiên bản 3. 6. Đã thêm hỗ trợ cho câu lệnh

cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
571.

rowcount

Read-only attribute that provides the number of modified rows for

cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
44,
cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
569,
cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
570, and
cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
571 statements; is
cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
170 for other statements, including CTE queries. Nó chỉ được cập nhật bằng các phương pháp
cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
119 và
cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
167

row_factory

Kiểm soát cách trình bày một hàng được tìm nạp từ

cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
00 này. Nếu
cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
43, một hàng được biểu diễn dưới dạng
cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
09. Có thể được đặt thành
cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
115 đi kèm; .
callable that accepts two arguments, a
cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
00 object and the
cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
09 of row values, and returns a custom object representing an SQLite row.

Mặc định là những gì

cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
118 đã được đặt khi
cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
00 được tạo. Việc gán cho thuộc tính này không ảnh hưởng đến
cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
118 của kết nối gốc

Xem Cách tạo và sử dụng các xưởng tạo hàng để biết thêm chi tiết.

Đối tượng hàng¶

lớp sqlite3. Hàng

Một phiên bản

cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
121 đóng vai trò là một
cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
547 được tối ưu hóa cao cho các đối tượng
cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
7. Nó hỗ trợ phép lặp, kiểm tra đẳng thức, truy cập
cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
124 và ánh xạ theo tên cột và chỉ mục.

Hai đối tượng

cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
121 so sánh bằng nhau nếu chúng có tên và giá trị cột giống hệt nhau

Xem Cách tạo và sử dụng các xưởng tạo hàng để biết thêm chi tiết.

phím[]

Trả lại một

cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
72 tên cột là
cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
127. Ngay sau một truy vấn, nó là phần tử đầu tiên của mỗi bộ trong
cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
128

Đã thay đổi trong phiên bản 3. 5. Đã thêm hỗ trợ cắt lát.

Đối tượng đốm màu¶

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

lớp sqlite3. Blob

A

cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
156 instance is a file-like object that can read and write data in an SQLite BLOB. Gọi
cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
130 để lấy kích thước [số byte] của đốm màu. Sử dụng các chỉ số và lát để truy cập trực tiếp vào dữ liệu blob.

Sử dụng

cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
156 làm trình quản lý ngữ cảnh để đảm bảo rằng thanh điều khiển blob được đóng sau khi sử dụng.

cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
10

đóng[]

Đóng đốm màu

Blob sẽ không sử dụng được từ thời điểm này trở đi. Một ngoại lệ

cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
132 [hoặc phân lớp] sẽ được đưa ra nếu có bất kỳ thao tác nào khác được thực hiện với blob

read[length=- 1 , /]

Đọc các byte dữ liệu có độ dài từ đốm màu ở vị trí bù hiện tại. Nếu đạt đến cuối đốm màu, dữ liệu lên tới EOF sẽ được trả về. Khi độ dài không được chỉ định hoặc là số âm,

cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
133 sẽ đọc cho đến hết đốm màu

write[data , /]

Ghi dữ liệu vào đốm màu ở phần bù hiện tại. Chức năng này không thể thay đổi độ dài blob. Viết vượt quá phần cuối của đốm màu sẽ tăng

cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
134

kể[]

Return the current access position of the blob

tìm kiếm[ , gốc=os.SEEK_SET , /]

Đặt vị trí truy cập hiện tại của blob thành offset. Đối số gốc mặc định là

cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
135 [định vị đốm màu tuyệt đối]. Other values for origin are
cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
136 [seek relative to the current position] and
cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
137 [seek relative to the blob’s end]

Chuẩn bị đối tượngProtocol¶

lớp sqlite3. Chuẩn bị giao thức

Mục đích duy nhất của loại PrepareProtocol là hoạt động như một giao thức thích ứng kiểu PEP 246 cho các đối tượng có thể tự thích ứng để native SQLite types.

Ngoại lệ¶

Hệ thống phân cấp ngoại lệ được xác định bởi DB-API 2. 0 [PEP 249]

ngoại lệ sqlite3. Cảnh báo

Ngoại lệ này hiện không được mô-đun

cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
0 đưa ra, nhưng có thể được đưa ra bởi các ứng dụng sử dụng
cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
0, ví dụ: nếu hàm do người dùng xác định cắt bớt dữ liệu trong khi chèn.
cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
140 là một lớp con của
cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
141

ngoại lệ sqlite3. Lỗi

Lớp cơ sở của các ngoại lệ khác trong mô-đun này. Sử dụng điều này để bắt tất cả các lỗi với một câu lệnh

cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
142 duy nhất.
cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
132 là một phân lớp của
cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
141

Nếu ngoại lệ bắt nguồn từ bên trong thư viện SQLite, hai thuộc tính sau đây sẽ được thêm vào ngoại lệ

sqlite_errorcode

Mã lỗi số từ API SQLite

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

sqlite_errorname

Tên tượng trưng của mã lỗi số từ API SQLite

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

ngoại lệ sqlite3. Lỗi giao diện

Ngoại lệ được đưa ra do lạm dụng API SQLite C cấp thấp. Nói cách khác, nếu ngoại lệ này được đưa ra, nó có thể chỉ ra một lỗi trong mô-đun

cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
0.
cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
146 là một lớp con của
cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
132

ngoại lệ sqlite3. Lỗi cơ sở dữ liệu

Ngoại lệ đưa ra cho các lỗi có liên quan đến cơ sở dữ liệu. Điều này đóng vai trò là ngoại lệ cơ bản cho một số loại lỗi cơ sở dữ liệu. Nó chỉ được nuôi ngầm thông qua các phân lớp chuyên biệt.

cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
148 is a subclass of
cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
132

ngoại lệ sqlite3. Lỗi dữ liệu

Exception raised for errors caused by problems with the processed data, like numeric values out of range, and strings which are too long.

cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
150 là một lớp con của
cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
148

ngoại lệ sqlite3. Lỗi thao tác

Exception raised for errors that are related to the database’s operation, and not necessarily under the control of the programmer. Ví dụ: không tìm thấy đường dẫn cơ sở dữ liệu hoặc không thể xử lý giao dịch.

cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
501 là một lớp con của
cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
148

ngoại lệ sqlite3. Lỗi toàn vẹn

Ngoại lệ được đưa ra khi tính toàn vẹn quan hệ của cơ sở dữ liệu bị ảnh hưởng, e. g. kiểm tra khóa ngoại không thành công. Nó là một lớp con của

cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
148

ngoại lệ sqlite3. Lỗi nội bộ

Exception raised when SQLite encounters an internal error. Nếu vấn đề này xuất hiện, điều đó có thể cho biết có sự cố với thư viện SQLite thời gian chạy.

cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
155 là một phân lớp của
cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
148

ngoại lệ sqlite3. ProgrammingError

Ngoại lệ được đưa ra đối với các lỗi lập trình API của

cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
0, chẳng hạn như cung cấp sai số liên kết cho một truy vấn hoặc cố gắng vận hành trên một
cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
7 đã đóng.
cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
564 là một lớp con của
cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
148

ngoại lệ sqlite3. Lỗi NotSupported

Ngoại lệ được đưa ra trong trường hợp API phương thức hoặc cơ sở dữ liệu không được hỗ trợ bởi thư viện SQLite bên dưới. For example, setting deterministic to

cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
106 in
cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
162, if the underlying SQLite library does not support deterministic functions.
cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
163 là một lớp con của
cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
148

Các loại SQLite và Python¶

SQLite nguyên bản hỗ trợ các loại sau.

cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
137,
cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
166,
cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
167,
cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
553,
cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
169

The following Python types can thus be sent to SQLite without any problem

loại trăn

loại SQLite

cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
43

cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
137

cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
172

cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
166

cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
174

cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
167

cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
79

cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
553

cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
124

cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
169

Đây là cách các loại SQLite được chuyển đổi thành các loại Python theo mặc định

loại SQLite

loại trăn

cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
137

cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
43

cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
166

cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
172

cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
167

cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
174

cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
553

phụ thuộc vào

cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
187,
cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
79 theo mặc định

cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
169

cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
124

Hệ thống loại của mô-đun

cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
0 có thể mở rộng theo hai cách. bạn có thể lưu trữ các loại Python bổ sung trong cơ sở dữ liệu SQLite qua bộ điều hợp đối tượng và bạn có thể để mô-đun
cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
0 chuyển đổi các loại SQLite thành các loại Python qua converters.

Bộ điều hợp và bộ chuyển đổi mặc định¶

There are default adapters for the date and datetime types in the datetime module. Chúng sẽ được gửi dưới dạng ngày ISO/dấu thời gian ISO tới SQLite

Bộ chuyển đổi mặc định được đăng ký dưới tên “ngày” cho

cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
193 và dưới tên “dấu thời gian” cho
cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
194

Bằng cách này, bạn có thể sử dụng ngày/dấu thời gian từ Python mà không cần thêm bất kỳ thao tác nào trong hầu hết các trường hợp. The format of the adapters is also compatible with the experimental SQLite date/time functions

Ví dụ sau minh họa điều này

cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
11

Nếu dấu thời gian được lưu trữ trong SQLite có phần phân số dài hơn 6 số, thì giá trị của dấu thời gian đó sẽ bị bộ chuyển đổi dấu thời gian cắt ngắn thành độ chính xác micro giây

Ghi chú

Trình chuyển đổi “dấu thời gian” mặc định bỏ qua các giá trị bù UTC trong cơ sở dữ liệu và luôn trả về một đối tượng

cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
194 ngây thơ. Để duy trì độ lệch UTC trong dấu thời gian, hãy tắt bộ chuyển đổi hoặc đăng ký bộ chuyển đổi nhận biết độ lệch với
cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
74

Làm thế nào để hướng dẫn¶

Cách sử dụng trình giữ chỗ để liên kết các giá trị trong truy vấn SQL¶

Các hoạt động SQL thường cần sử dụng các giá trị từ các biến Python. Tuy nhiên, hãy cẩn thận khi sử dụng các thao tác chuỗi của Python để lắp ráp các truy vấn, vì chúng dễ bị tấn công SQL injection. Ví dụ: kẻ tấn công có thể chỉ cần đóng một trích dẫn và đưa vào

cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
197 để chọn tất cả các hàng

cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
12

Thay vào đó, hãy sử dụng thay thế tham số của DB-API. Để chèn một biến vào một chuỗi truy vấn, hãy sử dụng một trình giữ chỗ trong chuỗi và thay thế các giá trị thực tế vào truy vấn bằng cách cung cấp chúng dưới dạng một

cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
09 giá trị cho đối số thứ hai của phương thức
cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
119 của con trỏ. Một câu lệnh SQL có thể sử dụng một trong hai loại trình giữ chỗ. dấu chấm hỏi [kiểu qmark] hoặc trình giữ chỗ được đặt tên [kiểu được đặt tên]. Đối với kiểu qmark,
cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
700 phải là trình tự . For the named style, it can be either a sequence or
cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
562 instance. Độ dài của chuỗi phải khớp với số lượng phần giữ chỗ, nếu không thì sẽ có một số
cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
564. Nếu một
cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
562 được cung cấp, nó phải chứa các khóa cho tất cả các tham số được đặt tên. Bất kỳ mục bổ sung được bỏ qua. Đây là một ví dụ về cả hai phong cách.

cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
13

Cách điều chỉnh các loại Python tùy chỉnh thành các giá trị SQLite¶

SQLite chỉ hỗ trợ một số loại dữ liệu hạn chế. Để lưu trữ các loại Python tùy chỉnh trong cơ sở dữ liệu SQLite, hãy điều chỉnh chúng theo một trong Các loại Python mà SQLite hiểu được .

Có hai cách để điều chỉnh các đối tượng Python thành các kiểu SQLite. để đối tượng của bạn tự điều chỉnh hoặc sử dụng bộ điều hợp có thể gọi được. Cái sau sẽ được ưu tiên hơn cái trước. Đối với một thư viện xuất một loại tùy chỉnh, có thể hợp lý khi cho phép loại đó tự điều chỉnh. As an application developer, it may make more sense to take direct control by registering custom adapter functions

Cách viết các đối tượng có thể thích ứng¶

Giả sử chúng ta có một lớp

cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
704 đại diện cho một cặp tọa độ,
cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
705 và
cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
706, trong hệ tọa độ Descartes. Cặp tọa độ sẽ được lưu trữ dưới dạng chuỗi văn bản trong cơ sở dữ liệu, sử dụng dấu chấm phẩy để phân tách tọa độ. This can be implemented by adding a
cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
707 method which returns the adapted value. Đối tượng được chuyển đến giao thức sẽ thuộc loại
cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
708

cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
14

Cách đăng ký bộ điều hợp có thể gọi được¶

Khả năng khác là tạo một hàm chuyển đổi đối tượng Python thành loại tương thích với SQLite. Chức năng này sau đó có thể được đăng ký bằng cách sử dụng

cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
709

cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
15

Cách chuyển đổi các giá trị SQLite thành các loại Python tùy chỉnh¶

Viết một bộ điều hợp cho phép bạn chuyển đổi từ các loại Python tùy chỉnh sang các giá trị SQLite. Để có thể chuyển đổi từ các giá trị SQLite sang các loại Python tùy chỉnh, chúng tôi sử dụng trình chuyển đổi

Hãy quay lại lớp học

cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
704. Chúng tôi đã lưu trữ tọa độ x và y được phân tách bằng dấu chấm phẩy dưới dạng chuỗi trong SQLite

Đầu tiên, chúng ta sẽ định nghĩa một hàm chuyển đổi chấp nhận chuỗi làm tham số và xây dựng một đối tượng

cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
704 từ nó

Ghi chú

Các hàm chuyển đổi luôn được truyền một đối tượng

cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
124, bất kể kiểu dữ liệu SQLite cơ bản

cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
16

Bây giờ chúng ta cần thông báo cho

cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
0 khi nào nó nên chuyển đổi một giá trị SQLite nhất định. Điều này được thực hiện khi kết nối với cơ sở dữ liệu, sử dụng tham số detect_types của
cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
125. Có ba lựa chọn

  • ngầm định. đặt detect_types thành

    cur.execute["""
        INSERT INTO movie VALUES
            ['Monty Python and the Holy Grail', 1975, 8.2],
            ['And Now for Something Completely Different', 1971, 7.5]
    """]
    
    76

  • rõ ràng. đặt detect_types thành

    cur.execute["""
        INSERT INTO movie VALUES
            ['Monty Python and the Holy Grail', 1975, 8.2],
            ['And Now for Something Completely Different', 1971, 7.5]
    """]
    
    77

  • Cả hai. đặt detect_types thành

    cur.execute["""
        INSERT INTO movie VALUES
            ['Monty Python and the Holy Grail', 1975, 8.2],
            ['And Now for Something Completely Different', 1971, 7.5]
    """]
    
    717. Tên cột được ưu tiên hơn các loại đã khai báo

The following example illustrates the implicit and explicit approaches

cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
17

Công thức bộ chuyển đổi và bộ chuyển đổi¶

Phần này hiển thị công thức cho các bộ điều hợp và bộ chuyển đổi phổ biến

cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
18

Cách sử dụng các phương pháp phím tắt kết nối¶

Sử dụng các phương thức

cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
119,
cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
167 và
cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
169 của lớp
cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
7, mã của bạn có thể được viết chính xác hơn vì bạn không phải tạo các đối tượng
cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
00 [thường là không cần thiết] một cách rõ ràng. Thay vào đó, các đối tượng
cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
00 được tạo hoàn toàn và các phương thức tắt này trả về các đối tượng con trỏ. Bằng cách này, bạn có thể thực thi một câu lệnh
cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
48 và lặp lại nó trực tiếp chỉ bằng một lệnh gọi duy nhất trên đối tượng
cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
7

cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
19

Cách sử dụng trình quản lý bối cảnh kết nối¶

Một đối tượng

cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
7 có thể được sử dụng làm trình quản lý bối cảnh tự động chuyển giao hoặc khôi phục các giao dịch đang mở khi rời khỏi phần thân của trình quản lý bối cảnh. Nếu phần thân của câu lệnh
cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
727 kết thúc mà không có ngoại lệ, giao dịch được cam kết. Nếu cam kết này không thành công hoặc nếu phần thân của câu lệnh
cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
727 đưa ra một ngoại lệ chưa được phát hiện, giao dịch sẽ được khôi phục

If there is no open transaction upon leaving the body of the

cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
727 statement, the context manager is a no-op

Ghi chú

The context manager neither implicitly opens a new transaction nor closes the connection

cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
70

Cách làm việc với SQLite URI¶

Một số thủ thuật URI hữu ích bao gồm

  • Mở cơ sở dữ liệu ở chế độ chỉ đọc

cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
71

  • Không ngầm tạo một tệp cơ sở dữ liệu mới nếu nó chưa tồn tại;

cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
72

  • Tạo cơ sở dữ liệu trong bộ nhớ có tên dùng chung

cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
73

Bạn có thể tìm thêm thông tin về tính năng này, bao gồm danh sách các tham số trong tài liệu SQLite URI

Cách tạo và sử dụng row factory¶

Theo mặc định,

cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
0 đại diện cho mỗi hàng là một
cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
09. If a
cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
09 does not suit your needs, you can use the
cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
115 class or a custom
cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
547

Mặc dù

cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
547 tồn tại như một thuộc tính trên cả
cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
00 và
cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
7, nhưng bạn nên đặt
cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
118 để tất cả các con trỏ được tạo từ kết nối sẽ sử dụng cùng một hàng.

cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
121 cung cấp quyền truy cập được đặt tên theo chỉ mục và phân biệt chữ hoa chữ thường đối với các cột, với chi phí bộ nhớ tối thiểu và tác động đến hiệu suất đối với một
cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
09. Để sử dụng
cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
121 làm nhà sản xuất hàng, hãy gán nó cho thuộc tính
cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
547

cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
74

Các truy vấn hiện trả về các đối tượng

cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
121

cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
75

Bạn có thể tạo một

cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
547 tùy chỉnh trả về mỗi hàng dưới dạng một
cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
562, với các tên cột được ánh xạ tới các giá trị

cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
76

Using it, queries now return a

cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
562 instead of a
cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
09

cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
77

Nhà máy sản xuất hàng sau trả về một bộ có tên .

cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
78

cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
749 có thể được sử dụng như sau

cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
79

Với một số điều chỉnh, công thức trên có thể được điều chỉnh để sử dụng

cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
750 hoặc bất kỳ lớp tùy chỉnh nào khác, thay vì
cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
751

Explanation¶

Kiểm soát giao dịch¶

Mô-đun

cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
0 không tuân theo quy trình xử lý giao dịch được đề xuất bởi PEP 249

Nếu thuộc tính kết nối

cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
101 không phải là
cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
43, các giao dịch mới được mở hoàn toàn trước khi
cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
119 và
cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
167 thực hiện các câu lệnh
cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
44,
cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
569,
cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
570 hoặc
cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
571; . Sử dụng các phương pháp
cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
163 và
cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
762 để tương ứng cam kết và khôi phục các giao dịch đang chờ xử lý. Bạn có thể chọn hành vi giao dịch SQLite cơ bản — nghĩa là, liệu và loại câu lệnh
cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
763 nào
cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
0 có thực thi ngầm hay không — thông qua thuộc tính
cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
101

Nếu

cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
101 được đặt thành
cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
43, không có giao dịch nào được mở hoàn toàn. Điều này khiến thư viện SQLite cơ bản ở chế độ tự động cam kết, nhưng cũng cho phép người dùng thực hiện xử lý giao dịch của riêng họ bằng cách sử dụng các câu lệnh SQL rõ ràng. Chế độ autocommit của thư viện SQLite cơ bản có thể được truy vấn bằng thuộc tính
cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
768

The

cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
169 method implicitly commits any pending transaction before execution of the given SQL script, regardless of the value of
cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
101

Làm cách nào để lấy tên bảng trong SQLite Python?

Để liệt kê tất cả các bảng trong cơ sở dữ liệu SQLite3, bạn nên truy vấn bảng sqlite_master rồi sử dụng hàm fetchall[] để tìm nạp kết quả từ câu lệnh SELECT. The sqlite_master is the master table in SQLite3, which stores all tables.

Làm cách nào để lấy tên bảng trong SQLite?

Nếu bạn đang chạy chương trình truy cập dòng lệnh sqlite3, bạn có thể gõ ". tables" để lấy danh sách tất cả các bảng . Hoặc bạn có thể gõ ". lược đồ" để xem lược đồ cơ sở dữ liệu hoàn chỉnh bao gồm tất cả các bảng và chỉ mục.

Làm cách nào để hiển thị bảng trong sqlite3 Python?

Các bước để Tìm nạp tất cả các bảng bằng SQLite3 trong Python .
Tạo đối tượng kết nối bằng phương thức connect[], sqliteConnection = sqlite3. kết nối ['SQLite_Retrieving_data. db']
Đã tạo một truy vấn SQLite mà chúng tôi sẽ tìm kiếm danh sách tất cả các bảng có trong cơ sở dữ liệu sqlite3

Cách lấy tên cột trong bảng sqlite3?

PRAGMA table_info[table_name]; sẽ giúp bạn có danh sách tất cả các tên cột.

Chủ Đề