Chèn SQLite Python

SQLite là thư viện C cung cấp cơ sở dữ liệu dựa trên đĩa nhẹ, không yêu cầu quy trình máy chủ riêng biệt và cho phép truy cập cơ sở dữ liệu bằng một biến thể không chuẩn của ngôn ngữ truy vấn SQL. Some applications can use SQLite for internal data storage. It’s also possible to prototype an application using SQLite and then port the code to a larger database such as PostgreSQL or Oracle

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 was written by Gerhard Häring. It provides an SQL interface compliant with the DB-API 2. 0 specification described by PEP 249, and requires SQLite 3. 7. 15 hoặc mới hơn

This document includes four main sections

  • Tutorial teaches how to use the

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

  • Reference describes the classes and functions this module defines.

  • How-to guides details how to handle specific tasks.

  • Explanation provides in-depth background on transaction control.

See also

https. //www. sqlite. org

The SQLite web page; the documentation describes the syntax and the available data types for the supported SQL dialect

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

Tutorial, reference and examples for learning SQL syntax

PEP 249 - Database API Specification 2. 0

PEP written by Marc-André Lemburg

Tutorial¶

In this tutorial, you will create a database of Monty Python movies using basic

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 functionality. It assumes a fundamental understanding of database concepts, including cursors and transactions

First, we need to create a new database and open a database connection to allow

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 to work with it. 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)
""")
5 to to create a connection to the database
cur.execute("""
    INSERT INTO movie VALUES
        ('Monty Python and the Holy Grail', 1975, 8.2),
        ('And Now for Something Completely Different', 1971, 7.5)
""")
6 in the current working directory, implicitly creating it if it does not exist

import sqlite3
con = sqlite3.connect("tutorial.db")

The returned

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 object
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 represents the connection to the on-disk database

In order to execute SQL statements and fetch results from SQL queries, we will need to use a database cursor. 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)
""")
9 to create 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)
""")
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

Now that we’ve got a database connection and a cursor, we can create a database table

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 with columns for title, release year, and review score. For simplicity, we can just use column names in the table declaration – thanks to the flexible typing feature of SQLite, specifying the data types is optional. Execute 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)
""")
02 statement by calling
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). Execute that query by calling
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, 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)
""")
08 to fetch the resulting row

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. If we query
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 for a non-existent table
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,
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 will 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

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

Now, add two rows of data supplied as SQL literals by executing 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 statement, once again by calling
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)
""")

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)
""")
44 statement implicitly opens a transaction, which needs to be committed before changes are saved in the database (see Transaction control for details). 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

We can verify that the data was inserted correctly by executing 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)
""")
48 query. 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

Now, insert three more rows by calling

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

Notice that

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 placeholders are used to bind
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 to the query. Always use placeholders instead of string formatting to bind Python values to SQL statements, to avoid SQL injection attacks (see How to use placeholders to bind values in SQL queries for more details).

We can verify that the new rows were inserted by executing 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)
""")
48 query, this time iterating over the results of the query

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

Finally, verify that the database has been written to disk by calling

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 close the existing connection, opening a new one, creating a new cursor, then querying the database

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

You’ve now created an SQLite database using 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, inserted data and retrieved values from it in multiple ways

See also

  • How-to guides for further reading.

    • How to use placeholders to bind values in SQL queries

    • How to adapt custom Python types to SQLite values

    • How to convert SQLite values to custom Python types

    • How to use the connection context manager

    • How to create and use row factories

  • Explanation for in-depth background on transaction control.

Reference¶

Module functions¶

sqlite3. connect(database , timeout=5. 0 , detect_types=0 , isolation_level='DEFERRED' , check_same_thread=True , factory=sqlite3. Connection , cached_statements=128 , uri=False)

Open a connection to an SQLite database

Parameters
  • database ( path-like object ) – The path to the database file to be opened. 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. If another connection opens a transaction to modify the database, it will be locked until that transaction is committed. Default five seconds

  • detect_types (int) – Control whether and how data types not natively supported by SQLite are looked up to be converted to Python types, using the converters registered 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)
    """)
    
    74. Set it to any combination (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)
    """)
    
    75, bitwise or) 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)
    """)
    
    76 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)
    """)
    
    77 to enable this. Column names takes precedence over declared types if both flags are set. Types cannot be detected for generated fields (for example
    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), even when the detect_types parameter is set;
    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 will be returned instead. Theo mặc định (
    cur.execute("""
        INSERT INTO movie VALUES
            ('Monty Python and the Holy Grail', 1975, 8.2),
            ('And Now for Something Completely Different', 1971, 7.5)
    """)
    
    100), tính năng phát hiện loại bị tắt.

  • isolation_level (str. Không có) –

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

  • check_same_thread (bool) – Nếu

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

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

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

  • cached_statements (int) – Số câu lệnh mà

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

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

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

loại trả lại

Sự liên quan

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

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

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

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

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

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

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

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

sqlite3. complete_statement(câu lệnh)

Trả lại

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

Ví dụ

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

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

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

sqlite3. enable_callback_tracebacks(flag , /)

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

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

Đăng ký một

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

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

sqlite3. register_adapter(loại , bộ chuyển đổi, /)

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

sqlite3. register_converter(typename , converter, /)

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

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

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

Hằng số mô-đun¶

sqlite3. PARSE_COLNAMES

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

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

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

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

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

sqlite3. PARSE_DECLTYPES

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

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

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

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

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

sqlite3. SQLITE_OKsqlite3. SQLITE_DENYsqlite3. SQLITE_IGNORE

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

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

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

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

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

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

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

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

sqlite3. apilevel

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

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

sqlite3. paramstyle

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

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

Ghi chú

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ợ. Tuy nhiên, DB-API không cho phép nhiều giá trị 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)
""")
146

sqlite3. sqlite_version

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

cur.execute("""
    INSERT INTO movie VALUES
        ('Monty Python and the Holy Grail', 1975, 8.2),
        ('And Now for Something Completely Different', 1971, 7.5)
""")
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, cho biết mức độ an toàn của luồng mà mô-đun

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

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

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

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

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

Chế độ luồng SQLite

chủ đề an toàn

SQLITE_THREADSAFE

DB-API 2. 0 ý nghĩa

đơn luồng

0

0

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

đa luồng

1

2

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

nối tiếp

3

1

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

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

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

sqlite3. phiên bản

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

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

sqlite3. version_info

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

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

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

lớp sqlite3. Kết nối

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

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

See also

  • 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(bảng , cột, row, /, *, readonly=False, name='main')

Mở tay cầ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)
""")
160 cho BLOB hiện có

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. Mặc định là
    cur.execute("""
        INSERT INTO movie VALUES
            ('Monty Python and the Holy Grail', 1975, 8.2),
            ('And Now for Something Completely Different', 1971, 7.5)
    """)
    
    107

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

    cur.execute("""
        INSERT INTO movie VALUES
            ('Monty Python and the Holy Grail', 1975, 8.2),
            ('And Now for Something Completely Different', 1971, 7.5)
    """)
    
    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ố=(), /)

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

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

executemany(sql , tham số, /)

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

cur.execute("""
    INSERT INTO movie VALUES
        ('Monty Python and the Holy Grail', 1975, 8.2),
        ('And Now for Something Completely Different', 1971, 7.5)
""")
00 mới và gọi
cur.execute("""
    INSERT INTO movie VALUES
        ('Monty Python and the Holy Grail', 1975, 8.2),
        ('And Now for Something Completely Different', 1971, 7.5)
""")
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. Trả về đối tượng con trỏ mới

create_function(tên , narg, func, *, deterministic=False)

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

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ó.

  • tất định (bool) – Nếu

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

tăng

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

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

Thí dụ

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

create_aggregate(tên , /, n_arg, aggregate_class)

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

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

  • lớp_tổng hợp ( lớp . Không có) –

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

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

    Đặt thành

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

Thí dụ

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

create_window_function(tên , num_params, aggregate_class, /)

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

Parameters
  • name (str) – Tên của hàm cửa sổ tổng hợp SQL để tạo hoặc xóa

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

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

  • lớp_tổng hợp ( lớp . Không có) –

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

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

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

    cur.execute("""
        INSERT INTO movie VALUES
            ('Monty Python and the Holy Grail', 1975, 8.2),
            ('And Now for Something Completely Different', 1971, 7.5)
    """)
    
    178 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)
    """)
    
    184 phải chấp nhận được kiểm soát bởi num_params

    Đặt thành

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

tăng

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

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

Thí dụ

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

create_collation(tên , có thể gọi)

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

cur.execute("""
    INSERT INTO movie VALUES
        ('Monty Python and the Holy Grail', 1975, 8.2),
        ('And Now for Something Completely Different', 1971, 7.5)
""")
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 nếu chúng được sắp xếp bằng nhau

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

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

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

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

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

gián đoạn()

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

set_authorizer(authorizer_callback)

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

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

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

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

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

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

Chuyển

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

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

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

set_progress_handler(progress_handler , n)

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

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

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

Trả về một giá trị khác 0 từ hàm xử lý sẽ chấm dứt truy vấn hiện đang thực 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)

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

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

cur.execute("""
    INSERT INTO movie VALUES
        ('Monty Python and the Holy Grail', 1975, 8.2),
        ('And Now for Something Completely Different', 1971, 7.5)
""")
79) đang được thực thi. Giá trị trả về của cuộc gọi lại bị bỏ qua. Lưu ý rằng chương trình phụ trợ không chỉ chạy các câu lệnh được truyền cho các phương thức
cur.execute("""
    INSERT INTO movie VALUES
        ('Monty Python and the Holy Grail', 1975, 8.2),
        ('And Now for Something Completely Different', 1971, 7.5)
""")
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 , /)

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

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

Ghi chú

Mô-đun

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

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

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

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

cur.execute("""
    INSERT INTO movie VALUES
        ('Monty Python and the Holy Grail', 1975, 8.2),
        ('And Now for Something Completely Different', 1971, 7.5)
""")
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. Đã thêm 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)
""")
519.

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.

Thí 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
  • đích (Kết nối) – Kết nối cơ sở dữ liệu để lưu bản sao lưu vào

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

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

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

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

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

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

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

getlimit(danh mục , /)

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

Parameters

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

loại trả lại

int

tăng

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

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

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

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

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

setlimit(danh mục , giới hạn, /)

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

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

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

loại trả lại

int

tăng

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

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

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

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

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

xếp thứ tự(* , tên='main')

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

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

Parameters

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

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

Thí 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

Nếu

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

executemany(sql , tham số, /)

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

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

Thí dụ

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

executescript(sql_script , /)

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

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

Thí dụ

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

tìm nạp()

Nếu

cur.execute("""
    INSERT INTO movie VALUES
        ('Monty Python and the Holy Grail', 1975, 8.2),
        ('And Now for Something Completely Different', 1971, 7.5)
""")
551 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, hãy trả về kết quả truy vấn hàng tiếp theo được đặt là
cur.execute("""
    INSERT INTO movie VALUES
        ('Monty Python and the Holy Grail', 1975, 8.2),
        ('And Now for Something Completely Different', 1971, 7.5)
""")
09. Khác, chuyển nó đến nhà máy sản xuất hàng và trả về kết quả của nó. Trả lại
cur.execute("""
    INSERT INTO movie VALUES
        ('Monty Python and the Holy Grail', 1975, 8.2),
        ('And Now for Something Completely Different', 1971, 7.5)
""")
43 nếu không có thêm dữ liệu

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

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

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

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

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

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

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

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

setinputsizes(kích thước , /)

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

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

setoutputsize(kích thước , cột=None, /)

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

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

kích thước mảng

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

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

kết nối

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. Giá trị ban đầu của
cur.execute("""
    INSERT INTO movie VALUES
        ('Monty Python and the Holy Grail', 1975, 8.2),
        ('And Now for Something Completely Different', 1971, 7.5)
""")
104 là
cur.execute("""
    INSERT INTO movie VALUES
        ('Monty Python and the Holy Grail', 1975, 8.2),
        ('And Now for Something Completely Different', 1971, 7.5)
""")
43

Ghi chú

Chèn vào bảng

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

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

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

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

cur.execute("""
    INSERT INTO movie VALUES
        ('Monty Python and the Holy Grail', 1975, 8.2),
        ('And Now for Something Completely Different', 1971, 7.5)
""")
00 này. Nếu
cur.execute("""
    INSERT INTO movie VALUES
        ('Monty Python and the Holy Grail', 1975, 8.2),
        ('And Now for Something Completely Different', 1971, 7.5)
""")
43, một hàng được biểu diễn dưới dạng
cur.execute("""
    INSERT INTO movie VALUES
        ('Monty Python and the Holy Grail', 1975, 8.2),
        ('And Now for Something Completely Different', 1971, 7.5)
""")
09. Có thể được đặt thành
cur.execute("""
    INSERT INTO movie VALUES
        ('Monty Python and the Holy Grail', 1975, 8.2),
        ('And Now for Something Completely Different', 1971, 7.5)
""")
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 gốc

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

Đối tượng hàng¶

lớp sqlite3. Hàng

Một phiên bản

cur.execute("""
    INSERT INTO movie VALUES
        ('Monty Python and the Holy Grail', 1975, 8.2),
        ('And Now for Something Completely Different', 1971, 7.5)
""")
125 đóng vai trò là một
cur.execute("""
    INSERT INTO movie VALUES
        ('Monty Python and the Holy Grail', 1975, 8.2),
        ('And Now for Something Completely Different', 1971, 7.5)
""")
551 được tối ưu hóa cao cho các đối tượng
cur.execute("""
    INSERT INTO movie VALUES
        ('Monty Python and the Holy Grail', 1975, 8.2),
        ('And Now for Something Completely Different', 1971, 7.5)
""")
7. Nó hỗ trợ phép lặp, kiểm tra tính bằng, 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.

phím()

Trả lại một

cur.execute("""
    INSERT INTO movie VALUES
        ('Monty Python and the Holy Grail', 1975, 8.2),
        ('And Now for Something Completely Different', 1971, 7.5)
""")
72 tên cột là
cur.execute("""
    INSERT INTO movie VALUES
        ('Monty Python and the Holy Grail', 1975, 8.2),
        ('And Now for Something Completely Different', 1971, 7.5)
""")
131. Ngay sau một truy vấn, nó là phần tử đầu tiên của mỗi bộ trong
cur.execute("""
    INSERT INTO movie VALUES
        ('Monty Python and the Holy Grail', 1975, 8.2),
        ('And Now for Something Completely Different', 1971, 7.5)
""")
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. 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)
""")
134 để lấy kích thước (số byte) của đốm màu. Sử dụng các chỉ số và lát để truy cập trực tiếp vào dữ liệu blob.

Sử dụng

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

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

đóng()

Đóng đốm màu

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

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

đọc(độ dài=- 1, /)

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

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

ghi(dữ liệu , /)

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

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

kể()

Trả về vị trí truy cập hiện tại của blob

tìm kiếm( , gốc=os.SEEK_SET , /)

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

cur.execute("""
    INSERT INTO movie VALUES
        ('Monty Python and the Holy Grail', 1975, 8.2),
        ('And Now for Something Completely Different', 1971, 7.5)
""")
139 (định vị đốm màu tuyệt đối). Các giá trị khác cho nguồn gốc 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)
""")
140 (tìm kiếm liên quan đến vị trí hiện tại) 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)
""")
141 (tìm kiếm liên quan đến phần cuối của blob)

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

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

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

Ngoại lệ¶

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

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

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

cur.execute("""
    INSERT INTO movie VALUES
        ('Monty Python and the Holy Grail', 1975, 8.2),
        ('And Now for Something Completely Different', 1971, 7.5)
""")
0 đưa ra, nhưng có thể được đưa ra bởi các ứng dụng sử dụng
cur.execute("""
    INSERT INTO movie VALUES
        ('Monty Python and the Holy Grail', 1975, 8.2),
        ('And Now for Something Completely Different', 1971, 7.5)
""")
0, ví dụ: nếu hàm do người dùng xác định cắt bớt dữ liệu trong khi chèn.
cur.execute("""
    INSERT INTO movie VALUES
        ('Monty Python and the Holy Grail', 1975, 8.2),
        ('And Now for Something Completely Different', 1971, 7.5)
""")
144 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)
""")
145

ngoại lệ sqlite3. Lỗi

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

cur.execute("""
    INSERT INTO movie VALUES
        ('Monty Python and the Holy Grail', 1975, 8.2),
        ('And Now for Something Completely Different', 1971, 7.5)
""")
146 duy nhất.
cur.execute("""
    INSERT INTO movie VALUES
        ('Monty Python and the Holy Grail', 1975, 8.2),
        ('And Now for Something Completely Different', 1971, 7.5)
""")
136 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)
""")
145

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

sqlite_errorcode

Mã lỗi số từ API SQLite

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

sqlite_errorname

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

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

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

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

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

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

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

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

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

Đã đư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. Ví dụ: không tìm thấy đường dẫn cơ sở dữ liệu hoặc không thể xử lý giao dịch.

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

ngoại lệ sqlite3. Lỗi NotSupported

Ngoại lệ được đưa ra trong trường hợp API phương thức hoặc cơ sở dữ liệu không được hỗ trợ bởi thư viện SQLite bên dưới. 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

Do đó, các loại Python sau đây có thể được gửi tới SQLite mà không gặp vấn đề gì

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

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

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

If a timestamp stored in SQLite has a fractional part longer than 6 numbers, its value will be truncated to microsecond precision by the timestamp converter

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. To preserve UTC offsets in timestamps, either leave converters disabled, or register an offset-aware converter 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)
""")
74

How-to guides¶

How to use placeholders to bind values in SQL queries¶

SQL operations usually need to use values from Python variables. However, beware of using Python’s string operations to assemble queries, as they are vulnerable to SQL injection attacks (see the xkcd webcomic for a humorous example of what can go wrong)

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. To insert a variable into a query string, use a placeholder in the string, and substitute the actual values into the query by providing them 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 values to the second argument of the cursor’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)
""")
119 method. 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)
""")
703 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. If a
cur.execute("""
    INSERT INTO movie VALUES
        ('Monty Python and the Holy Grail', 1975, 8.2),
        ('And Now for Something Completely Different', 1971, 7.5)
""")
566 is given, it must contain keys for all named parameters. Any extra items are ignored. Here’s an example of both styles.

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

How to adapt custom Python types to SQLite values¶

SQLite supports only a limited set of data types natively. To store custom Python types in SQLite databases, adapt them to one of the Python types SQLite natively understands .

There are two ways to adapt Python objects to SQLite types. letting your object adapt itself, or using an adapter callable. The latter will take precedence above the former. For a library that exports a custom type, it may make sense to enable that type to adapt itself. As an application developer, it may make more sense to take direct control by registering custom adapter functions

How to write adaptable objects¶

Suppose we have a

cur.execute("""
    INSERT INTO movie VALUES
        ('Monty Python and the Holy Grail', 1975, 8.2),
        ('And Now for Something Completely Different', 1971, 7.5)
""")
707 class that represents a pair of coordinates,
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 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)
""")
709, in a Cartesian coordinate system. The coordinate pair will be stored as a text string in the database, using a semicolon to separate the coordinates. This can be implemented by adding a
cur.execute("""
    INSERT INTO movie VALUES
        ('Monty Python and the Holy Grail', 1975, 8.2),
        ('And Now for Something Completely Different', 1971, 7.5)
""")
710 method which returns the adapted value. The object passed to protocol will be of type
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

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)
""")
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)
""")
15

How to convert SQLite values to custom Python types¶

Writing an adapter lets you convert from custom Python types to SQLite values. Để có thể chuyển đổi từ các giá trị SQLite sang các loại Python tùy chỉnh, chúng tôi sử dụng trình chuyển đổi

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

cur.execute("""
    INSERT INTO movie VALUES
        ('Monty Python and the Holy Grail', 1975, 8.2),
        ('And Now for Something Completely Different', 1971, 7.5)
""")
707. 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)
""")
707 từ nó

Ghi chú

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

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

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

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

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

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

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

  • rõ ràng. set detect_types 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)
    """)
    
    77

  • Both. set detect_types 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)
    """)
    
    720. Column names take precedence over declared types

The following example illustrates the implicit and explicit approaches

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

Adapter and converter recipes¶

This section shows recipes for common adapters and converters

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

How to use connection shortcut methods¶

Using 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)
""")
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, 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)
""")
173 methods of the
cur.execute("""
    INSERT INTO movie VALUES
        ('Monty Python and the Holy Grail', 1975, 8.2),
        ('And Now for Something Completely Different', 1971, 7.5)
""")
7 class, your code can be written more concisely because you don’t have to create the (often superfluous)
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 objects explicitly. Instead, 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)
""")
00 objects are created implicitly and these shortcut methods return the cursor objects. This way, you can execute 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)
""")
48 statement and iterate over it directly using only a single call on 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)
""")
7 object

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

How to use the connection context manager¶

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 object can be used as a context manager that automatically commits or rolls back open transactions when leaving the body of the context manager. If the body of the
cur.execute("""
    INSERT INTO movie VALUES
        ('Monty Python and the Holy Grail', 1975, 8.2),
        ('And Now for Something Completely Different', 1971, 7.5)
""")
730 statement finishes without exceptions, the transaction is committed. If this commit fails, or if the body of the
cur.execute("""
    INSERT INTO movie VALUES
        ('Monty Python and the Holy Grail', 1975, 8.2),
        ('And Now for Something Completely Different', 1971, 7.5)
""")
730 statement raises an uncaught exception, the transaction is rolled back

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

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

Ghi chú

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

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

How to work with SQLite URIs¶

Some useful URI tricks include

  • Open a database in read-only mode

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

  • Do not implicitly create a new database file if it does not already exist; 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)
    """)
    
    505 if unable to create a new file

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

  • Create a shared named in-memory database

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

More information about this feature, including a list of parameters, can be found in the SQLite URI documentation

How to create and use row factories¶

By default,

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 represents 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)
""")
09. If a
cur.execute("""
    INSERT INTO movie VALUES
        ('Monty Python and the Holy Grail', 1975, 8.2),
        ('And Now for Something Completely Different', 1971, 7.5)
""")
09 does not suit your needs, you can use the
cur.execute("""
    INSERT INTO movie VALUES
        ('Monty Python and the Holy Grail', 1975, 8.2),
        ('And Now for Something Completely Different', 1971, 7.5)
""")
119 class or a custom
cur.execute("""
    INSERT INTO movie VALUES
        ('Monty Python and the Holy Grail', 1975, 8.2),
        ('And Now for Something Completely Different', 1971, 7.5)
""")
551

While

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 exists as an attribute both on 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)
""")
00 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)
""")
7, it is recommended to set
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, so all cursors created from the connection will use the same 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)
""")
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. To use
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 as a row factory, assign it 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)
""")
551 attribute

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

Queries now 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)
""")
125 objects

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

Using it, queries now return a

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

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

The following row factory returns a named tuple .

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)
""")
752 can be used as follows

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)
""")
753, 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)
""")
754

Explanation¶

Transaction control¶

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

If the connection attribute

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, new transactions are implicitly opened before
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 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)
""")
171 executes
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 statements; for other statements, no implicit transaction handling is performed. Use the
cur.execute("""
    INSERT INTO movie VALUES
        ('Monty Python and the Holy Grail', 1975, 8.2),
        ('And Now for Something Completely Different', 1971, 7.5)
""")
167 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)
""")
765 methods to respectively commit and roll back pending transactions. 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)
""")
766 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. This leaves the underlying SQLite library in autocommit mode, but also allows the user to perform their own transaction handling using explicit SQL statements. 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)
""")
771

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)
""")
173 method implicitly commits any pending transaction before execution of the given SQL script, regardless of the value of
cur.execute("""
    INSERT INTO movie VALUES
        ('Monty Python and the Holy Grail', 1975, 8.2),
        ('And Now for Something Completely Different', 1971, 7.5)
""")
101

What are the method to insert data in the SQLite table?

SQLite INSERT – inserting a single row into a table .
First, specify the name of the table to which you want to insert data after the INSERT INTO keywords
Second, add a comma-separated list of columns after the table name. Danh sách cột là tùy chọn. .
Third, add a comma-separated list of values after the VALUES keyword

Can SQLite be used in Python?

SQLite is a self-contained, file-based SQL database. SQLite comes bundled with Python and can be used in any of your Python applications without having to install any additional software .

Làm cách nào để tạo tệp SQLite trong Python?

Các bước tạo cơ sở dữ liệu trong Python bằng sqlite3 .
Step 1. Tạo cơ sở dữ liệu và bảng. In this step, you'll see how to create. .
Bước 2. Chèn giá trị vào bảng. Đối với bước này, hãy chèn dữ liệu sau vào bảng 'sản phẩm'. .
Bước 3. Hiển thị kết quả