Sqlite có tốt cho trăn không?

SQLite là một công cụ cơ sở dữ liệu rất dễ sử dụng có trong Python. SQLite là mã nguồn mở và là cơ sở dữ liệu tuyệt vời cho các dự án nhỏ hơn, dự án sở thích hoặc thử nghiệm và phát triển

Chúng tôi đã phát hành một khóa học video đầy đủ để giúp bạn tìm hiểu kiến ​​thức cơ bản về cách sử dụng SQLite3 với Python. Bạn sẽ tìm hiểu cách tạo cơ sở dữ liệu và bảng, thêm dữ liệu, sắp xếp dữ liệu, tạo báo cáo, lấy dữ liệu cụ thể, v.v. Khóa học được giảng dạy bởi John Elder từ Codemy. com

Đối với khóa học này, bạn phải có kiến ​​thức cơ bản về lập trình Python, nhưng bạn không cần biết gì về cơ sở dữ liệu hoặc SQLite

Trong khóa học này, bạn sẽ học

  • cơ sở dữ liệu là gì
  • cách cài đặt Python
  • cách cài đặt thiết bị đầu cuối git bash
  • cách sử dụng sqlite trong chương trình Python
  • cách kết nối với cơ sở dữ liệu trong Python
  • cách tạo cơ sở dữ liệu
  • cách tạo bảng
  • cách chèn một bản ghi vào bảng
  • cách chèn nhiều bản ghi vào bảng
  • hiểu các kiểu dữ liệu
  • cách chọn dữ liệu từ bảng
  • cách định dạng kết quả của bạn
  • cách sử dụng mệnh đề where
  • cách sử dụng mệnh đề like và ký tự đại diện
  • cách sử dụng AND và OR
  • cách cập nhật bản ghi
  • cách giới hạn và sắp xếp kết quả
  • làm thế nào để xóa bản ghi
  • cách xóa [thả] bảng và sao lưu
  • và hơn thế nữa

Vào cuối khóa học, bạn sẽ học cách xây dựng một ứng dụng rất cơ bản để giúp củng cố tất cả những điều từ khóa học

Bạn có thể xem khóa học video bên dưới hoặc trên freeCodeCamp. org kênh YouTube [1. đồng hồ 5 giờ]

QUẢNG CÁO

QUẢNG CÁO

Beau Carnes

Tôi là giáo viên và nhà phát triển với freeCodeCamp. tổ chức. Tôi chạy freeCodeCamp. tổ chức kênh YouTube

Nếu bạn đọc đến đây, hãy tweet cho tác giả để cho họ thấy bạn quan tâm. Tweet một lời cảm ơn

Học cách viết mã miễn phí. Chương trình giảng dạy mã nguồn mở của freeCodeCamp đã giúp hơn 40.000 người có được việc làm với tư cách là nhà phát triển. Bắt đầu

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

We can verify that the new table has been created by querying 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]
"""]
04 table built-in to SQLite, which should now contain an entry for 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]
"""]
01 table definition [see The Schema Table for details]. 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

We can see that the table has been created, as the query returns 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 containing the table’s name. 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]. Call
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 on the connection object to commit the transaction.

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. Use the now-familiar
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 to assign the result 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]
"""]
07, and call
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 to return all resulting rows

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

The result is 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]
"""]
72 of two
cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
09s, one per row, each containing that row’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]
"""]
74 value

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

Each row is a two-item

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
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, matching the columns selected in the query

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.

    • How to use placeholders to bind values in SQL queries

    • 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

    • How to use the connection context manager

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

  • Explanation for in-depth background on transaction control.

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 , isolation_level='DEFERRED' , check_same_thread=True , factory=sqlite3. Kết nối , cached_statements=128, uri=False]

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

Parameters
  • 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.

  • timeout [float] – How many seconds the connection should wait before raising an exception, if the database is locked by another connection. 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í. By default, 128 statements

  • 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

Raises an auditing event

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 with argument
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.

Raises an auditing event

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 with argument
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. The uri parameter.

Changed in version 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 dấu 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 , /]

Enable or disable callback tracebacks. By default you will not get any tracebacks in user-defined functions, aggregates, converters, authorizer callbacks etc. 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. Afterwards, you will get tracebacks from callbacks on
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

Register an

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 for an improved debug experience

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, /]

Register an adapter callable to adapt the Python type type into an SQLite type. 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. The types are declared when the database table is created.
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 will look up a converter function using the first word of the declared type as the converter dictionary key. 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],

  • The SQL statement should be aborted with an error [

    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. Hard-coded 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]
"""]
139

sqlite3. paramstyle

String constant stating the type of parameter marker formatting expected by 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]
"""]
0 module. Yêu cầu bởi DB-API. Hard-coded 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]
"""]
141

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 hỗ trợ các 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]
"""]
143,
cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
144 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]
"""]
145, bởi vì đó là những gì mà thư viện SQLite bên dưới hỗ trợ. However, the DB-API does not allow multiple values for 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]
"""]
146 attribute

sqlite3. sqlite_version

Version number of the runtime SQLite library as 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]
"""]
147

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]
"""]
149

sqlite3. an toàn luồng

Hằng số nguyên theo yêu cầu của DB-API 2. 0, stating the level of thread safety 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]
"""]
0 module supports. 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. Single-thread. In this mode, all mutexes are disabled and SQLite is unsafe to use in more than a single thread at once

  2. Multi-thread. In this mode, SQLite can be safely used by multiple threads provided that no single database connection is used simultaneously in two or more threads

  3. Serialized. In serialized mode, SQLite can be safely used by multiple threads with no restriction

The mappings from SQLite threading modes to DB-API 2. 0 threadsafety levels are as follows

Chế độ luồng SQLite

threadsafety

SQLITE_THREADSAFE

DB-API 2. 0 meaning

đơn luồng

0

0

Threads may not share the module

multi-thread

1

2

Threads may share the module, but not connections

nối tiếp

3

1

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

Changed in version 3. 11. Set threadsafety dynamically instead of hard-coding it 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]
"""]
151.

sqlite3. version

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]
"""]
147. Đây không phải là phiên bản của thư viện SQLite

sqlite3. version_info

Version number of this module as 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 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]
"""]
149. Đây không phải là phiên bản của thư viện SQLite

Connection objects¶

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

  • How to use the connection context manager

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[table , column , row , / , * , readonly=False , name='main']

Open 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]
"""]
160 handle to an existing BLOB

Parameters
  • 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. Defaults 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]
    """]
    
    107

  • name [str] – The name of the database where the blob is located. 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]
    """]
    
    163

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]
"""]
164

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]
"""]
160. 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]
"""]
166 để 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ố=[], /]

Create a new

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 call
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 on it with the given sql and parameters. Trả về đối tượng con trỏ mới

executemany[sql , parameters , /]

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]
"""]
171 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]
"""]
173 trên đó với sql_script đã cho. Return the new cursor object

create_function[name , narg , func , * , deterministic=False]

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

Parameters
  • 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]
    """]
    
    174, 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ó.

  • deterministic [bool] – If

    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, the created SQL function is marked as deterministic, which allows SQLite to perform additional optimizations

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[name , / , 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

Parameters
  • 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]
    """]
    
    174, nó có thể nhận bất kỳ số đối số nào

  • aggregate_class [ class . 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]
      """]
      
      178. Thêm một hàng vào tổng hợp

    • ____1179. 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]
    """]
    
    178 phải chấp nhận được kiểm soát bởi n_arg

    Set 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]
    """]
    
    43 to remove an existing SQL aggregate function

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, /]

Create or remove a user-defined aggregate window function

Parameters
  • name [str] – The name of the SQL aggregate window function to create or remove

  • 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]
    """]
    
    174, nó có thể nhận bất kỳ số đối số nào

  • aggregate_class [ class . Không có] –

    A class that must implement the following methods

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

    • ____1179. 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ợ .

    The number of arguments that 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]
    """]
    
    178 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]
    """]
    
    184 methods must accept is controlled by 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, which does not support aggregate window functions

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]

Create a collation named name using the collating function callable. 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]
"""]
147 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]
"""]
191

  • cur.execute["""
        INSERT INTO movie VALUES
            ['Monty Python and the Holy Grail', 1975, 8.2],
            ['And Now for Something Completely Different', 1971, 7.5]
    """]
    
    151 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]
    """]
    
    174 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 if they are ordered equal

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

Changed in version 3. 11. The collation name can contain any Unicode character. Earlier, only ASCII characters were allowed.

interrupt[]

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]

Register callable authorizer_callback to be invoked for each attempt to access a column of a table in the database. The callback should return one 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]
"""]
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, 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]
"""]
138 to signal how access to the column should be handled by the underlying SQLite library

The first argument to the callback signifies what kind of operation is to be authorized. The second and third argument will be arguments 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]
"""]
43 depending on the first argument. Đố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. All necessary constants are available in 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]
"""]
0 module

Passing

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 as authorizer_callback will disable the authorizer

Đã 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 hiện 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]
"""]
505

set_trace_callback[trace_callback]

Register callable trace_callback to be invoked for each SQL statement that is actually executed by the SQLite backend

The only argument passed to the callback is the statement [as

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] that is being executed. The return value of the callback is ignored. 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]
"""]
507. 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]
"""]
510 để 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 , /]

Enable the SQLite engine to load SQLite extensions from shared libraries if enabled 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]
"""]
106; else, disallow loading SQLite extensions. 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]
"""]
513 để đị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]
"""]
514 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]
"""]
515,
cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
516.

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]
"""]
514.

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[path , /]

Tải tiện ích mở rộng SQLite từ thư viện dùng chung có tại đường dẫn. Enable extension loading with

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

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]
"""]
519 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]
"""]
515,
cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
521.

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

Đã thay đổi trong phiên bản 3. 10. Added 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]
"""]
519 auditing event.

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]
"""]
523 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

Parameters
  • target [Connection] – The database connection to save the backup to

  • pages [int] – Số trang cần sao chép cùng một lúc. If equal to or less than

    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, the entire database is copied in a single step. Defaults 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]
    """]
    
    174

  • progress [ callback .  None] – If set to a callable, it is invoked with three integer arguments for every backup iteration. 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. Defaults 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]
    """]
    
    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]
    """]
    
    163 [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]
    """]
    
    528 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]
    """]
    
    529

  • 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

New in version 3. 7

getlimit[category , /]

Get a connection runtime limit

Parameters

category [int] – The SQLite limit category to be queried

loại trả lại

int

tăng

ProgrammingError – If category is not recognised by the underlying SQLite library

Example, query the maximum length of an SQL statement 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]
"""]
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 [the default is 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[category , limit , /]

Set a connection runtime limit. Attempts to increase a limit above its hard upper bound are silently truncated to the hard upper bound. Regardless of whether or not the limit was changed, the prior value of the limit is returned

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

  • limit [int] – The value of the new limit. Nếu âm, giới hạn hiện tại không thay đổi

loại trả lại

int

tăng

ProgrammingError – If category is not recognised by the underlying SQLite library

Example, limit the number of attached databases to 1 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]
"""]
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 [the default limit is 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

serialize[* , name='main']

Serialize a database into 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]
"""]
124 object. For an ordinary on-disk database file, the serialization is just a copy of the disk file. For an in-memory database or a “temp” database, the serialization is the same sequence of bytes which would be written to disk if that database were backed up to disk

Parameters

name [str] – The database name to be serialized. Defaults 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]
"""]
163

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]
"""]
536 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

Parameters
  • 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]
    """]
    
    163

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]
    """]
    
    539 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]
    """]
    
    540

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]
"""]
549, 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]
"""]
551 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]
"""]
551 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]
"""]
557. 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]
"""]
562 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]
"""]
566 .

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ó, thì nó 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]
"""]
568. 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]
"""]
173 nếu bạn muốn thực thi nhiều câu lệnh SQL với một lệnh gọi

If

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 is not
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, sql is an
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]
"""]
573,
cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
574, 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]
"""]
575 statement, and there is no open transaction, a transaction is implicitly opened before executing sql

executemany[sql , parameters , /]

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. Uses the same implicit transaction handling as

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 , /]

Execute the SQL statements in 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]
"""]
577 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]
"""]
147

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[]

If

cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
551 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]
"""]
43, return the next row query result set as 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. Khác, chuyển nó đến nhà máy sản xuất hàng và trả về kết quả của nó. Return
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 if no more data is available

fetchmany[size=con trỏ. arraysize]

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]
"""]
584 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ề

Note there are performance considerations involved with the size parameter. Để 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]
"""]
585 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]
"""]
584 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]
"""]
588 được gọi]

The cursor will be unusable from this point forward; 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]
"""]
568 exception will be raised if any operation is attempted with the cursor

setinputsizes[sizes , /]

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]
"""]
585. 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

connection

Thuộc tính chỉ đọc cung cấp cơ sở dữ liệu 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]
"""]
7 thuộc về con trỏ. Đố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]
"""]
515 đề 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]
"""]
575 thành công sử dụng 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. Đố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]
"""]
171 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]
"""]
173 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]
"""]
104 sẽ không thay đổi. The initial 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]
"""]
104 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]
"""]
43

Ghi chú

Inserts into

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

Changed in version 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]
"""]
575.

số hàng

Thuộc tính chỉ đọc cung cấp số lượng hàng đã sửa đổi 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]
"""]
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]
"""]
573,
cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
574 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]
"""]
575; . 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]
"""]
171

row_factory

Control how a row fetched from this

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 is represented. If
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, a row is represented as 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. 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]
"""]
119 đ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]
"""]
122 đã đượ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]
"""]
122 của kết nối chính

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

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 instance serves as a highly optimized
cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
551 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]
"""]
7 objects. 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]
"""]
128 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]
"""]
125 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.

keys[]

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]
"""]
72 of column names as
cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
131. Immediately after a query, it is the first member of each tuple 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]
"""]
132

Đã 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

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]
"""]
160 là một đối tượng dạng tệp có thể đọc và ghi dữ liệu trong SQLite BLOB. Call
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 to get the size [number of bytes] of the blob. 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]
"""]
160 làm trình quản lý ngữ cảnh để đảm bảo rằng tay cầm blob được đóng lại 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[]

Close the blob

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

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 [or subclass] exception will be raised if any further operation is attempted with the blob

read[length=- 1 , /]

Read length bytes of data from the blob at the current offset position. If the end of the blob is reached, the data up to EOF will be returned. When length is not specified, or is negative,

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 will read until the end of the blob

write[data , /]

Write data to the blob at the current offset. This function cannot change the blob length. Writing beyond the end of the blob will raise

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

tell[]

Return the current access position of the blob

seek[offset , origin=os. SEEK_SET , /]

Set the current access position of the blob to offset. The origin argument defaults 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]
"""]
139 [absolute blob positioning]. 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]
"""]
140 [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]
"""]
141 [seek relative to the blob’s end]

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

class sqlite3. PrepareProtocol

The PrepareProtocol type’s single purpose is to act as a PEP 246 style adaption protocol for objects that can adapt themselves to native SQLite types .

Exceptions¶

The exception hierarchy is defined by the DB-API 2. 0 [PEP 249]

exception sqlite3. Warning

This exception is not currently raised by 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]
"""]
0 module, but may be raised by applications using
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, for example if a user-defined function truncates data while inserting.
cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
144 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]
"""]
145

exception sqlite3. Error

The base class of the other exceptions in this module. Use this to catch all errors with one single

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 statement.
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 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]
"""]
145

If the exception originated from within the SQLite library, the following two attributes are added to the exception

sqlite_errorcode

The numeric error code from the SQLite API

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

sqlite_errorname

The symbolic name of the numeric error code from the SQLite API

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

exception sqlite3. InterfaceError

Exception raised for misuse of the low-level SQLite C API. In other words, if this exception is raised, it probably indicates a bug in 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]
"""]
0 module.
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]
"""]
136

exception sqlite3. DatabaseError

Exception raised for errors that are related to the database. This serves as the base exception for several types of database errors. It is only raised implicitly through the specialised subclasses.

cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
152 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]
"""]
136

exception sqlite3. DataError

Đã đưa ra ngoại lệ cho các lỗi do sự cố với dữ liệu được xử lý, chẳng hạn như giá trị số nằm ngoài phạm vi và chuỗi quá dà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]
"""]
154 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]
"""]
152

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

Ngoại lệ được đưa ra đối với các lỗi liên quan đến hoạt động của cơ sở dữ liệu và không nhất thiết nằm dưới sự kiểm soát của lập trình viên. For example, the database path is not found, or a transaction could not be processed.

cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
505 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]
"""]
152

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 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]
"""]
152

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

Ngoại lệ được đưa ra khi SQLite gặp lỗi nội bộ. 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]
"""]
159 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]
"""]
152

ngoại lệ sqlite3. Lỗi lập trình

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]
"""]
568 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]
"""]
152

exception 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. Ví dụ: đặt hàm xác định 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 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]
"""]
166, nếu thư viện SQLite cơ bản không hỗ trợ các hàm xá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]
"""]
167 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]
"""]
152

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]
"""]
170,
cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
171,
cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
557,
cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
173

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]
"""]
176

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

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

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

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]
"""]
557

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]
"""]
173

Đâ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]
"""]
170

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

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

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

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

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]
"""]
191,
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]
"""]
173

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. you can store additional Python types in an SQLite database via object adapters , and you can let 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]
"""]
0 module convert SQLite types to Python types via converters .

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

Có các bộ điều hợp mặc định cho các loại ngày và giờ trong mô-đun ngày giờ. 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]
"""]
197 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]
"""]
198

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. Định dạng của bộ điều hợp cũng tương thích với các hàm ngày/giờ SQLite thử nghiệm

The following example demonstrates this

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ú

The default “timestamp” converter ignores UTC offsets in the database and always returns a naive

cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
198 object. Để 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]
"""]
701 để 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

Instead, use the DB-API’s parameter substitution. Để 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ỏ. An SQL statement may use one of two kinds of placeholders. question marks [qmark style] or named placeholders [named style]. For the qmark style,
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 must be a sequence . 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]
"""]
566 instance. The length of the sequence must match the number of placeholders, or 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]
"""]
568 is raised. 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]
"""]
566 đượ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. Any extra items are ignored. Đâ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 .

There are two ways to adapt Python objects to SQLite types. để đố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. Với tư cách là nhà phát triển ứng dụng, việc kiểm soát trực tiếp bằng cách đăng ký các chức năng của bộ điều hợp tùy chỉnh có thể hợp lý hơn

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]
"""]
708 đạ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]
"""]
709 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]
"""]
710, 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 độ. Điều này có thể được thực hiện bằng cách thê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]
"""]
711 trả về giá trị đã điều chỉnh. Đố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]
"""]
712

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¶

The other possibility is to create a function that converts the Python object to an SQLite-compatible type. This function can then be registered using

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

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

Let’s go back to 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]
"""]
708 class. 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]
"""]
708 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. There are three options

  • Implicit. đặ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]
    """]
    
    721. Tên cột được ưu tiên hơn các loại đã khai báo

Ví dụ sau minh họa các cách tiếp cận ngầm và rõ rà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]
"""]
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]
"""]
171 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]
"""]
173 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 [thường là thừ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 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]
"""]
731 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]
"""]
731 đưa ra một ngoại lệ chưa được phát hiện, giao dịch sẽ được khôi phục

Nếu không có giao dịch mở nào khi rời khỏi 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]
"""]
731, trình quản lý bối cảnh không hoạt động

Ghi chú

Trình quản lý bối cảnh không ngầm mở một giao dịch mới cũng như không đóng 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]
"""]
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. 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]
"""]
09 không phù hợp với nhu cầu của bạn, bạn có thể sử dụ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]
"""]
119 hoặc 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]
"""]
551 tùy chỉnh

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]
"""]
551 tồn tại dưới dạng 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]
"""]
122, vì vậy 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]
"""]
125 provides indexed and case-insensitive named access to columns, with minimal memory overhead and performance impact over 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. Để 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]
"""]
125 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]
"""]
551

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]
"""]
125

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

You can create 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]
"""]
551 that returns each row as 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]
"""]
566, with column names mapped to values

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

Sử dụng nó, các truy vấn hiệ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]
"""]
566 thay 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

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]
"""]
753 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

With some adjustments, the above recipe can be adapted to use 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]
"""]
754, or any other custom class, 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]
"""]
755

Giải trình¶

Kiểm soát giao dịch¶

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]
"""]
0 module does not adhere to the transaction handling recommended by 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ở ngầm 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]
"""]
171 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]
"""]
573,
cur.execute["""
    INSERT INTO movie VALUES
        ['Monty Python and the Holy Grail', 1975, 8.2],
        ['And Now for Something Completely Different', 1971, 7.5]
"""]
574 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]
"""]
575; . 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]
"""]
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]
"""]
766 để tương ứng cam kết và khôi phục các giao dịch đang chờ xử lý. You can choose the underlying SQLite transaction behaviour — that is, whether and what type 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]
"""]
767 statements
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 implicitly executes – via 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]
"""]
101 attribute

If

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 is set 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]
"""]
43, no transactions are implicitly opened at all. Đ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]
"""]
772

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]
"""]
173 hoàn toàn cam kết bất kỳ giao dịch đang chờ xử lý nào trước khi thực thi tập lệnh SQL đã cho, bất kể 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]
"""]
101 là bao nhiêu

Can Python work with SQLite?

Mô-đun Python SQLite3 được sử dụng để tích hợp cơ sở dữ liệu SQLite với Python . Nó là một Python DBI API 2 được tiêu chuẩn hóa. 0 và cung cấp giao diện đơn giản và dễ sử dụng để tương tác với cơ sở dữ liệu SQLite. Không cần phải cài đặt riêng mô-đun này vì nó đi kèm với Python sau phiên bản 2. phiên bản 5x.

Tại sao nên sử dụng SQLite trong Python?

SQLite là thư viện C cung cấp cơ sở dữ liệu nhẹ dựa trên đĩa 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ộ. . Some applications can use SQLite for internal data storage.

Cơ sở dữ liệu nào là tốt nhất cho Python?

SQLite có lẽ là cơ sở dữ liệu đơn giản nhất để kết nối với ứng dụng Python vì bạn không cần cài đặt bất kỳ mô-đun Python SQL bên ngoài nào để làm như vậy. Theo mặc định, bản cài đặt Python của bạn chứa thư viện Python SQL có tên sqlite3 mà bạn có thể sử dụng để tương tác với cơ sở dữ liệu SQLite.

Làm cách nào để sử dụng SQLite cho Python?

Kết nối cơ sở dữ liệu Python SQLite .
Nhập mô-đun sqlite3. .
Sử dụng phương thức connect[]. .
Sử dụng phương thức con trỏ []. .
Sử dụng phương thức exec[]. .
Trích xuất kết quả bằng cách sử dụng hàm tìm nạp [].
Đóng con trỏ và các đối tượng kết nối. .
Bắt ngoại lệ cơ sở dữ liệu nếu có thể xảy ra trong quá trình kết nối này

Chủ Đề