Hướng dẫn how does sqlite3 work in python? - sqlite3 hoạt động như thế nào trong python?

Mã nguồn: lib/sqlite3/ Lib/sqlite3/

Show

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

Mô -đun

cur.execute("""
    INSERT INTO movie VALUES
        ('Monty Python and the Holy Grail', 1975, 8.2),
        ('And Now for Something Completely Different', 1971, 7.5)
""")
0 được viết bởi Gerhard Häring. Nó cung cấp một giao diện SQL tuân thủ đặc tả DB-API 2.0 được mô tả bởi PEP 249 và yêu cầu SQLite 3.7.15 hoặc mới hơn.PEP 249, and requires SQLite 3.7.15 or newer.

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

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

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

  • Tham khảo mô tả các lớp và chức năng mô -đun này xác định. describes the classes and functions this module defines.

  • Hướng dẫn hướng dẫn chi tiết cách xử lý các nhiệm vụ cụ thể. details how to handle specific tasks.

  • Giải thích cung cấp nền tảng chuyên sâu về kiểm soát giao dịch. provides in-depth background on transaction control.

Xem thêm

https://www.sqlite.org

Trang web sqlite; Tài liệu mô tả cú pháp và các loại dữ liệu có sẵn cho phương ngữ SQL được hỗ trợ.

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

Hướng dẫn, tham khảo và ví dụ cho việc học cú pháp SQL.

PEP 249 - Thông số kỹ thuật API cơ sở dữ liệu 2.0 - Database API Specification 2.0

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

Hướng dẫn¶

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

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

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

cur.execute("""
    INSERT INTO movie VALUES
        ('Monty Python and the Holy Grail', 1975, 8.2),
        ('And Now for Something Completely Different', 1971, 7.5)
""")
0 hoạt động với nó. Gọi
cur.execute("""
    INSERT INTO movie VALUES
        ('Monty Python and the Holy Grail', 1975, 8.2),
        ('And Now for Something Completely Different', 1971, 7.5)
""")
4 để tạo kết nối với 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)
""")
5 trong thư mục làm việc hiện tại, hoàn toàn tạo ra nó nếu nó không tồn tại:

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

Đối tượng

cur.execute("""
    INSERT INTO movie VALUES
        ('Monty Python and the Holy Grail', 1975, 8.2),
        ('And Now for Something Completely Different', 1971, 7.5)
""")
6 đã 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)
""")
7 đại diện cho kết nối với cơ sở dữ liệu trên đĩa.

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

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

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

>>> res = cur.execute("SELECT score FROM movie")
>>> res.fetchall()
[(8.2,), (7.5,)]
0 với các cột cho tiêu đề, năm phát hành và điểm đánh giá. Để đơn giản, chúng ta chỉ có thể sử dụng tên cột trong khai báo bảng - nhờ tính năng gõ linh hoạt của SQLite, chỉ định các loại dữ liệu là tùy chọn. Thực hiện câu lệnh
>>> res = cur.execute("SELECT score FROM movie")
>>> res.fetchall()
[(8.2,), (7.5,)]
1 bằng cách gọi
>>> res = cur.execute("SELECT score FROM movie")
>>> res.fetchall()
[(8.2,), (7.5,)]
2:

cur.execute("CREATE TABLE movie(title, year, score)")

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

>>> res = cur.execute("SELECT score FROM movie")
>>> res.fetchall()
[(8.2,), (7.5,)]
3 tích hợp cho SQLite, giờ đây sẽ chứa một mục nhập cho định nghĩa bảng
>>> res = cur.execute("SELECT score FROM movie")
>>> res.fetchall()
[(8.2,), (7.5,)]
0 (xem bảng lược đồ để biết chi tiết). Thực hiện truy vấn đó bằng cách gọi
>>> res = cur.execute("SELECT score FROM movie")
>>> res.fetchall()
[(8.2,), (7.5,)]
2, gán kết quả cho
>>> res = cur.execute("SELECT score FROM movie")
>>> res.fetchall()
[(8.2,), (7.5,)]
6 và gọi
>>> res = cur.execute("SELECT score FROM movie")
>>> res.fetchall()
[(8.2,), (7.5,)]
7 để tìm nạp hàng kết quả:

>>> res = cur.execute("SELECT name FROM sqlite_master")
>>> res.fetchone()
('movie',)

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

>>> res = cur.execute("SELECT score FROM movie")
>>> res.fetchall()
[(8.2,), (7.5,)]
8 chứa tên bảng. Nếu chúng tôi truy vấn
>>> res = cur.execute("SELECT score FROM movie")
>>> res.fetchall()
[(8.2,), (7.5,)]
3 cho bảng không tồn tại
data = [
    ("Monty Python Live at the Hollywood Bowl", 1982, 7.9),
    ("Monty Python's The Meaning of Life", 1983, 7.5),
    ("Monty Python's Life of Brian", 1979, 8.0),
]
cur.executemany("INSERT INTO movie VALUES(?, ?, ?)", data)
con.commit()  # Remember to commit the transaction after executing INSERT.
0,
>>> res = cur.execute("SELECT score FROM movie")
>>> res.fetchall()
[(8.2,), (7.5,)]
7 sẽ trả về
data = [
    ("Monty Python Live at the Hollywood Bowl", 1982, 7.9),
    ("Monty Python's The Meaning of Life", 1983, 7.5),
    ("Monty Python's Life of Brian", 1979, 8.0),
]
cur.executemany("INSERT INTO movie VALUES(?, ?, ?)", data)
con.commit()  # Remember to commit the transaction after executing INSERT.
2:

>>> res = cur.execute("SELECT name FROM sqlite_master WHERE name='spam'")
>>> res.fetchone() is None
True

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

data = [
    ("Monty Python Live at the Hollywood Bowl", 1982, 7.9),
    ("Monty Python's The Meaning of Life", 1983, 7.5),
    ("Monty Python's Life of Brian", 1979, 8.0),
]
cur.executemany("INSERT INTO movie VALUES(?, ?, ?)", data)
con.commit()  # Remember to commit the transaction after executing INSERT.
3, một lần nữa bằng cách gọi
>>> res = cur.execute("SELECT score FROM movie")
>>> res.fetchall()
[(8.2,), (7.5,)]
2:

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

Tuyên bố

data = [
    ("Monty Python Live at the Hollywood Bowl", 1982, 7.9),
    ("Monty Python's The Meaning of Life", 1983, 7.5),
    ("Monty Python's Life of Brian", 1979, 8.0),
]
cur.executemany("INSERT INTO movie VALUES(?, ?, ?)", data)
con.commit()  # Remember to commit the transaction after executing INSERT.
3 ngầm mở một giao dịch, cần phải được cam kết trước khi các thay đổi được lưu trong cơ sở dữ liệu (xem kiểm soát giao dịch để biết chi tiết). Gọi
data = [
    ("Monty Python Live at the Hollywood Bowl", 1982, 7.9),
    ("Monty Python's The Meaning of Life", 1983, 7.5),
    ("Monty Python's Life of Brian", 1979, 8.0),
]
cur.executemany("INSERT INTO movie VALUES(?, ?, ?)", data)
con.commit()  # Remember to commit the transaction after executing INSERT.
6 trên đối tượng kết nối để thực hiện giao dịch:Transaction control for details). Call
data = [
    ("Monty Python Live at the Hollywood Bowl", 1982, 7.9),
    ("Monty Python's The Meaning of Life", 1983, 7.5),
    ("Monty Python's Life of Brian", 1979, 8.0),
]
cur.executemany("INSERT INTO movie VALUES(?, ?, ?)", data)
con.commit()  # Remember to commit the transaction after executing INSERT.
6 on the connection object to commit the transaction:

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

data = [
    ("Monty Python Live at the Hollywood Bowl", 1982, 7.9),
    ("Monty Python's The Meaning of Life", 1983, 7.5),
    ("Monty Python's Life of Brian", 1979, 8.0),
]
cur.executemany("INSERT INTO movie VALUES(?, ?, ?)", data)
con.commit()  # Remember to commit the transaction after executing INSERT.
7. Sử dụng
>>> res = cur.execute("SELECT score FROM movie")
>>> res.fetchall()
[(8.2,), (7.5,)]
2 quen thuộc để gán kết quả cho
>>> res = cur.execute("SELECT score FROM movie")
>>> res.fetchall()
[(8.2,), (7.5,)]
6 và gọi
>>> for row in cur.execute("SELECT year, title FROM movie ORDER BY year"):
...     print(row)
(1971, 'And Now for Something Completely Different')
(1975, 'Monty Python and the Holy Grail')
(1979, "Monty Python's Life of Brian")
(1982, 'Monty Python Live at the Hollywood Bowl')
(1983, "Monty Python's The Meaning of Life")
0 để trả về tất cả các hàng kết quả:

>>> res = cur.execute("SELECT score FROM movie")
>>> res.fetchall()
[(8.2,), (7.5,)]

Kết quả là

>>> for row in cur.execute("SELECT year, title FROM movie ORDER BY year"):
...     print(row)
(1971, 'And Now for Something Completely Different')
(1975, 'Monty Python and the Holy Grail')
(1979, "Monty Python's Life of Brian")
(1982, 'Monty Python Live at the Hollywood Bowl')
(1983, "Monty Python's The Meaning of Life")
1 của hai
>>> res = cur.execute("SELECT score FROM movie")
>>> res.fetchall()
[(8.2,), (7.5,)]
8, một mỗi hàng, mỗi hàng chứa giá trị hàng ____ ____ của ____.

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

>>> for row in cur.execute("SELECT year, title FROM movie ORDER BY year"):
...     print(row)
(1971, 'And Now for Something Completely Different')
(1975, 'Monty Python and the Holy Grail')
(1979, "Monty Python's Life of Brian")
(1982, 'Monty Python Live at the Hollywood Bowl')
(1983, "Monty Python's The Meaning of Life")
4:

data = [
    ("Monty Python Live at the Hollywood Bowl", 1982, 7.9),
    ("Monty Python's The Meaning of Life", 1983, 7.5),
    ("Monty Python's Life of Brian", 1979, 8.0),
]
cur.executemany("INSERT INTO movie VALUES(?, ?, ?)", data)
con.commit()  # Remember to commit the transaction after executing INSERT.

Lưu ý rằng

>>> for row in cur.execute("SELECT year, title FROM movie ORDER BY year"):
...     print(row)
(1971, 'And Now for Something Completely Different')
(1975, 'Monty Python and the Holy Grail')
(1979, "Monty Python's Life of Brian")
(1982, 'Monty Python Live at the Hollywood Bowl')
(1983, "Monty Python's The Meaning of Life")
5 Người giữ chỗ được sử dụng để liên kết
>>> for row in cur.execute("SELECT year, title FROM movie ORDER BY year"):
...     print(row)
(1971, 'And Now for Something Completely Different')
(1975, 'Monty Python and the Holy Grail')
(1979, "Monty Python's Life of Brian")
(1982, 'Monty Python Live at the Hollywood Bowl')
(1983, "Monty Python's The Meaning of Life")
6 với truy vấn. Luôn sử dụng các trình giữ chỗ thay vì định dạng chuỗi để liên kết các giá trị python với các câu lệnh SQL, để tránh các cuộc tấn công tiêm SQL (xem cách sử dụng trình giữ chỗ để liên kết các giá trị trong các truy vấn SQL để biết thêm chi tiết).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).

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

data = [
    ("Monty Python Live at the Hollywood Bowl", 1982, 7.9),
    ("Monty Python's The Meaning of Life", 1983, 7.5),
    ("Monty Python's Life of Brian", 1979, 8.0),
]
cur.executemany("INSERT INTO movie VALUES(?, ?, ?)", data)
con.commit()  # Remember to commit the transaction after executing INSERT.
7, lần này lặp lại kết quả của truy vấn:

>>> for row in cur.execute("SELECT year, title FROM movie ORDER BY year"):
...     print(row)
(1971, 'And Now for Something Completely Different')
(1975, 'Monty Python and the Holy Grail')
(1979, "Monty Python's Life of Brian")
(1982, 'Monty Python Live at the Hollywood Bowl')
(1983, "Monty Python's The Meaning of Life")

Mỗi hàng là một

>>> res = cur.execute("SELECT score FROM movie")
>>> res.fetchall()
[(8.2,), (7.5,)]
8 của
>>> for row in cur.execute("SELECT year, title FROM movie ORDER BY year"):
...     print(row)
(1971, 'And Now for Something Completely Different')
(1975, 'Monty Python and the Holy Grail')
(1979, "Monty Python's Life of Brian")
(1982, 'Monty Python Live at the Hollywood Bowl')
(1983, "Monty Python's The Meaning of Life")
9 hai mục, khớp các cột được chọn trong truy vấn.

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

>>> con.close()
>>> new_con = sqlite3.connect("tutorial.db")
>>> new_cur = new_con.cursor()
>>> res = new_cur.execute("SELECT title, year FROM movie ORDER BY score DESC")
>>> title, year = res.fetchone()
>>> print(f'The highest scoring Monty Python movie is {title!r}, released in {year}')
The highest scoring Monty Python movie is 'Monty Python and the Holy Grail', released in 1975
0 để đóng kết nối hiện có, mở một cái mới, tạo một con trỏ mới, sau đó truy vấn cơ sở dữ liệu:

>>> con.close()
>>> new_con = sqlite3.connect("tutorial.db")
>>> new_cur = new_con.cursor()
>>> res = new_cur.execute("SELECT title, year FROM movie ORDER BY score DESC")
>>> title, year = res.fetchone()
>>> print(f'The highest scoring Monty Python movie is {title!r}, released in {year}')
The highest scoring Monty Python movie is 'Monty Python and the Holy Grail', released in 1975

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

cur.execute("""
    INSERT INTO movie VALUES
        ('Monty Python and the Holy Grail', 1975, 8.2),
        ('And Now for Something Completely Different', 1971, 7.5)
""")
0, chèn dữ liệu và lấy các giá trị từ nó theo nhiều cách.

Xem thêm

  • Trang web sqlite; Tài liệu mô tả cú pháp và các loại dữ liệu có sẵn cho phương ngữ SQL được hỗ trợ. for further reading:

    • https://www.w3schools.com/sql/

    • Hướng dẫn, tham khảo và ví dụ cho việc học cú pháp SQL.

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

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

  • Giải thích cho nền tảng chuyên sâu về kiểm soát giao dịch. for in-depth background on transaction control.

Tài liệu tham khảo¶

Chức năng mô -đun

________ 82 ________ 83 (cơ sở dữ liệu, thời gian chờ = 5.0, Detect_types = 0, sleam_level = 'hoãn lại', kiểm tra_same_thread = true, factory = sqlite3.connect(database, timeout=5.0, detect_types=0, isolation_level='DEFERRED', check_same_thread=True, factory=sqlite3.Connection, cached_statements=100, uri=False)

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

Thông số
  • Cơ sở dữ liệu (đối tượng giống như đường dẫn)-Đường dẫn đến tệp cơ sở dữ liệu sẽ được mở. Vượt qua

    >>> con.close()
    >>> new_con = sqlite3.connect("tutorial.db")
    >>> new_cur = new_con.cursor()
    >>> res = new_cur.execute("SELECT title, year FROM movie ORDER BY score DESC")
    >>> title, year = res.fetchone()
    >>> print(f'The highest scoring Monty Python movie is {title!r}, released in {year}')
    The highest scoring Monty Python movie is 'Monty Python and the Holy Grail', released in 1975
    
    4 để mở kết nối với cơ sở dữ liệu có trong RAM thay vì trên đĩa. (path-like object) – The path to the database file to be opened. Pass
    >>> con.close()
    >>> new_con = sqlite3.connect("tutorial.db")
    >>> new_cur = new_con.cursor()
    >>> res = new_cur.execute("SELECT title, year FROM movie ORDER BY score DESC")
    >>> title, year = res.fetchone()
    >>> print(f'The highest scoring Monty Python movie is {title!r}, released in {year}')
    The highest scoring Monty Python movie is 'Monty Python and the Holy Grail', released in 1975
    
    4 to open a connection to a database that is in RAM instead of on disk.

  • Thời gian chờ (Float) - Kết nối nên chờ bao nhiêu giây trước khi tăng ngoại lệ, nếu cơ sở dữ liệu bị khóa bởi một kết nối khác. Nếu một kết nối khác mở một giao dịch để sửa đổi cơ sở dữ liệu, nó sẽ bị khóa cho đến khi giao dịch đó được thực hiện. Mặc định năm giây. (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) - Kiểm soát xem và cách các loại dữ liệu không được hỗ trợ bởi SQLite được tra cứu để được chuyển đổi thành các loại Python, sử dụng các bộ chuyển đổi được đăng ký với

    >>> con.close()
    >>> new_con = sqlite3.connect("tutorial.db")
    >>> new_cur = new_con.cursor()
    >>> res = new_cur.execute("SELECT title, year FROM movie ORDER BY score DESC")
    >>> title, year = res.fetchone()
    >>> print(f'The highest scoring Monty Python movie is {title!r}, released in {year}')
    The highest scoring Monty Python movie is 'Monty Python and the Holy Grail', released in 1975
    
    5. Đặt nó thành bất kỳ sự kết hợp nào (sử dụng
    >>> con.close()
    >>> new_con = sqlite3.connect("tutorial.db")
    >>> new_cur = new_con.cursor()
    >>> res = new_cur.execute("SELECT title, year FROM movie ORDER BY score DESC")
    >>> title, year = res.fetchone()
    >>> print(f'The highest scoring Monty Python movie is {title!r}, released in {year}')
    The highest scoring Monty Python movie is 'Monty Python and the Holy Grail', released in 1975
    
    6, bitwise hoặc) của
    >>> con.close()
    >>> new_con = sqlite3.connect("tutorial.db")
    >>> new_cur = new_con.cursor()
    >>> res = new_cur.execute("SELECT title, year FROM movie ORDER BY score DESC")
    >>> title, year = res.fetchone()
    >>> print(f'The highest scoring Monty Python movie is {title!r}, released in {year}')
    The highest scoring Monty Python movie is 'Monty Python and the Holy Grail', released in 1975
    
    7 và
    >>> con.close()
    >>> new_con = sqlite3.connect("tutorial.db")
    >>> new_cur = new_con.cursor()
    >>> res = new_cur.execute("SELECT title, year FROM movie ORDER BY score DESC")
    >>> title, year = res.fetchone()
    >>> print(f'The highest scoring Monty Python movie is {title!r}, released in {year}')
    The highest scoring Monty Python movie is 'Monty Python and the Holy Grail', released in 1975
    
    8 để kích hoạt điều này. Tên cột được ưu tiên hơn các loại được khai báo nếu cả hai cờ được đặt. Các loại không thể được phát hiện cho các trường được tạo (ví dụ
    >>> con.close()
    >>> new_con = sqlite3.connect("tutorial.db")
    >>> new_cur = new_con.cursor()
    >>> res = new_cur.execute("SELECT title, year FROM movie ORDER BY score DESC")
    >>> title, year = res.fetchone()
    >>> print(f'The highest scoring Monty Python movie is {title!r}, released in {year}')
    The highest scoring Monty Python movie is 'Monty Python and the Holy Grail', released in 1975
    
    9), ngay cả khi tham số Detect_Types được đặt;
    >>> sqlite3.complete_statement("SELECT foo FROM bar;")
    True
    >>> sqlite3.complete_statement("SELECT foo")
    False
    
    0 sẽ được trả lại thay thế. Theo mặc định (
    >>> sqlite3.complete_statement("SELECT foo FROM bar;")
    True
    >>> sqlite3.complete_statement("SELECT foo")
    False
    
    1), loại phát hiện bị vô hiệu hóa.
    (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
    >>> con.close()
    >>> new_con = sqlite3.connect("tutorial.db")
    >>> new_cur = new_con.cursor()
    >>> res = new_cur.execute("SELECT title, year FROM movie ORDER BY score DESC")
    >>> title, year = res.fetchone()
    >>> print(f'The highest scoring Monty Python movie is {title!r}, released in {year}')
    The highest scoring Monty Python movie is 'Monty Python and the Holy Grail', released in 1975
    
    5. Set it to any combination (using
    >>> con.close()
    >>> new_con = sqlite3.connect("tutorial.db")
    >>> new_cur = new_con.cursor()
    >>> res = new_cur.execute("SELECT title, year FROM movie ORDER BY score DESC")
    >>> title, year = res.fetchone()
    >>> print(f'The highest scoring Monty Python movie is {title!r}, released in {year}')
    The highest scoring Monty Python movie is 'Monty Python and the Holy Grail', released in 1975
    
    6, bitwise or) of
    >>> con.close()
    >>> new_con = sqlite3.connect("tutorial.db")
    >>> new_cur = new_con.cursor()
    >>> res = new_cur.execute("SELECT title, year FROM movie ORDER BY score DESC")
    >>> title, year = res.fetchone()
    >>> print(f'The highest scoring Monty Python movie is {title!r}, released in {year}')
    The highest scoring Monty Python movie is 'Monty Python and the Holy Grail', released in 1975
    
    7 and
    >>> con.close()
    >>> new_con = sqlite3.connect("tutorial.db")
    >>> new_cur = new_con.cursor()
    >>> res = new_cur.execute("SELECT title, year FROM movie ORDER BY score DESC")
    >>> title, year = res.fetchone()
    >>> print(f'The highest scoring Monty Python movie is {title!r}, released in {year}')
    The highest scoring Monty Python movie is 'Monty Python and the Holy Grail', released in 1975
    
    8 to enable this. Column names takes precedence over declared types if both flags are set. Types cannot be detected for generated fields (for example
    >>> con.close()
    >>> new_con = sqlite3.connect("tutorial.db")
    >>> new_cur = new_con.cursor()
    >>> res = new_cur.execute("SELECT title, year FROM movie ORDER BY score DESC")
    >>> title, year = res.fetchone()
    >>> print(f'The highest scoring Monty Python movie is {title!r}, released in {year}')
    The highest scoring Monty Python movie is 'Monty Python and the Holy Grail', released in 1975
    
    9), even when the detect_types parameter is set;
    >>> sqlite3.complete_statement("SELECT foo FROM bar;")
    True
    >>> sqlite3.complete_statement("SELECT foo")
    False
    
    0 will be returned instead. By default (
    >>> sqlite3.complete_statement("SELECT foo FROM bar;")
    True
    >>> sqlite3.complete_statement("SELECT foo")
    False
    
    1), type detection is disabled.

  • secholation_level (str | none) -

    >>> sqlite3.complete_statement("SELECT foo FROM bar;")
    True
    >>> sqlite3.complete_statement("SELECT foo")
    False
    
    2 của kết nối, kiểm soát xem và làm thế nào các giao dịch được mở ngầm. Có thể là
    >>> sqlite3.complete_statement("SELECT foo FROM bar;")
    True
    >>> sqlite3.complete_statement("SELECT foo")
    False
    
    3 (mặc định),
    >>> sqlite3.complete_statement("SELECT foo FROM bar;")
    True
    >>> sqlite3.complete_statement("SELECT foo")
    False
    
    4 hoặc
    >>> sqlite3.complete_statement("SELECT foo FROM bar;")
    True
    >>> sqlite3.complete_statement("SELECT foo")
    False
    
    5; hoặc
    data = [
        ("Monty Python Live at the Hollywood Bowl", 1982, 7.9),
        ("Monty Python's The Meaning of Life", 1983, 7.5),
        ("Monty Python's Life of Brian", 1979, 8.0),
    ]
    cur.executemany("INSERT INTO movie VALUES(?, ?, ?)", data)
    con.commit()  # Remember to commit the transaction after executing INSERT.
    
    2 để vô hiệu hóa các giao dịch mở ngầm. Xem kiểm soát giao dịch để biết thêm.
    (str | None) – The
    >>> sqlite3.complete_statement("SELECT foo FROM bar;")
    True
    >>> sqlite3.complete_statement("SELECT foo")
    False
    
    2 of the connection, controlling whether and how transactions are implicitly opened. Can be
    >>> sqlite3.complete_statement("SELECT foo FROM bar;")
    True
    >>> sqlite3.complete_statement("SELECT foo")
    False
    
    3 (default),
    >>> sqlite3.complete_statement("SELECT foo FROM bar;")
    True
    >>> sqlite3.complete_statement("SELECT foo")
    False
    
    4 or
    >>> sqlite3.complete_statement("SELECT foo FROM bar;")
    True
    >>> sqlite3.complete_statement("SELECT foo")
    False
    
    5; or
    data = [
        ("Monty Python Live at the Hollywood Bowl", 1982, 7.9),
        ("Monty Python's The Meaning of Life", 1983, 7.5),
        ("Monty Python's Life of Brian", 1979, 8.0),
    ]
    cur.executemany("INSERT INTO movie VALUES(?, ?, ?)", data)
    con.commit()  # Remember to commit the transaction after executing INSERT.
    
    2 to disable opening transactions implicitly. See Transaction control for more.

  • check_same_thread (bool) - Nếu

    >>> sqlite3.complete_statement("SELECT foo FROM bar;")
    True
    >>> sqlite3.complete_statement("SELECT foo")
    False
    
    7 (mặc định), chỉ có luồng tạo mới có thể sử dụng kết nối. Nếu
    >>> sqlite3.complete_statement("SELECT foo FROM bar;")
    True
    >>> sqlite3.complete_statement("SELECT foo")
    False
    
    8, kết nối có thể được chia sẻ trên nhiều luồng; Nếu vậy, các hoạt động viết nên được người dùng tuần tự hóa để tránh tham nhũng dữ liệu.
    (bool) – If
    >>> sqlite3.complete_statement("SELECT foo FROM bar;")
    True
    >>> sqlite3.complete_statement("SELECT foo")
    False
    
    7 (default), only the creating thread may use the connection. If
    >>> sqlite3.complete_statement("SELECT foo FROM bar;")
    True
    >>> sqlite3.complete_statement("SELECT foo")
    False
    
    8, the connection may be shared across multiple threads; if so, write operations should be serialized by the user to avoid data corruption.

  • 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)
    """)
    
    6 để 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)
    """)
    
    6 mặc định.
    (Connection) – A custom subclass of
    cur.execute("""
        INSERT INTO movie VALUES
            ('Monty Python and the Holy Grail', 1975, 8.2),
            ('And Now for Something Completely Different', 1971, 7.5)
    """)
    
    6 to create the connection with, if not the 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)
    """)
    
    6 class.

  • Bộ nhớ cache_statements (int) - Số lượng 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 nên lưu trữ bên trong cho kết nối này, để tránh phân tích chi phí. Theo mặc định, 100 câu. (int) – The number of statements 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)
    """)
    
    0 should internally cache for this connection, to avoid parsing overhead. By default, 100 statements.

  • URI (BOOL) - Nếu được đặt thành

    >>> sqlite3.complete_statement("SELECT foo FROM bar;")
    True
    >>> sqlite3.complete_statement("SELECT foo")
    False
    
    7, 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 sơ đồ phải là
    cur.execute("CREATE TABLE movie(title, year, score)")
    
    03 và đường dẫn có thể tương đối hoặc tuyệt đối. Chuỗi truy vấn cho phép chuyển các tham số cho SQLite, cho phép nhiều cách làm việc với URI SQLite.
    (bool) – If set to
    >>> sqlite3.complete_statement("SELECT foo FROM bar;")
    True
    >>> sqlite3.complete_statement("SELECT foo")
    False
    
    7, database is interpreted as a URI with a file path and an optional query string. The scheme part must be
    cur.execute("CREATE TABLE movie(title, year, score)")
    
    03, and the path can be relative or absolute. The query string allows passing parameters to SQLite, enabling various How to work with SQLite URIs.

Loại trở lại

Sự liên quan

Tăng một sự kiện kiểm toán

cur.execute("CREATE TABLE movie(title, year, score)")
04 với đối số
cur.execute("CREATE TABLE movie(title, year, score)")
05.auditing event
cur.execute("CREATE TABLE movie(title, year, score)")
04 with argument
cur.execute("CREATE TABLE movie(title, year, score)")
05.

Tăng một sự kiện kiểm toán

cur.execute("CREATE TABLE movie(title, year, score)")
06 với đối số
cur.execute("CREATE TABLE movie(title, year, score)")
07.auditing event
cur.execute("CREATE TABLE movie(title, year, score)")
06 with argument
cur.execute("CREATE TABLE movie(title, year, score)")
07.

Mới trong phiên bản 3.4: Tham số URI.The uri parameter.

Đã 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 giống như đường dẫn, không chỉ là một chuỗi.database can now also be a path-like object, not only a string.

Mới trong phiên bản 3.10: Sự kiện kiểm toán

cur.execute("CREATE TABLE movie(title, year, score)")
06.The
cur.execute("CREATE TABLE movie(title, year, score)")
06 auditing event.

________ 82 ________ 110 (tuyên bố) ¶(statement)

Trả về

>>> sqlite3.complete_statement("SELECT foo FROM bar;")
True
>>> sqlite3.complete_statement("SELECT foo")
False
7 Nếu câu lệnh String 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 xem không có chữ viết nào không được giải thích và tuyên bố bị chấm dứt bởi một dấu chấm phẩy.

Ví dụ:

>>> sqlite3.complete_statement("SELECT foo FROM bar;")
True
>>> sqlite3.complete_statement("SELECT foo")
False

Hàm này có thể hữu ích trong đầu vào dòng lệnh để xác định xem văn bản đã nhập dường như tạo thành một câu lệnh SQL hoàn chỉnh hay nếu cần đầu vào bổ sung trước khi gọi

cur.execute("CREATE TABLE movie(title, year, score)")
12.

________ 82 ________ 114 (cờ, /) ¶(flag, /)

Bật hoặc vô hiệu hóa các dấu vết 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, tổng hợp, bộ chuyển đổi, trình gọi ủy quyền do người dùng xác định, 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 bằng cờ được đặt thành

>>> sqlite3.complete_statement("SELECT foo FROM bar;")
True
>>> sqlite3.complete_statement("SELECT foo")
False
7. Sau đó, bạn sẽ nhận được dấu vết từ các cuộc gọi lại trên
cur.execute("CREATE TABLE movie(title, year, score)")
16. Sử dụng
>>> sqlite3.complete_statement("SELECT foo FROM bar;")
True
>>> sqlite3.complete_statement("SELECT foo")
False
8 để vô hiệu hóa tính năng một lần nữa.

________ 82 ________ 119 (loại, bộ điều hợp, /) ¶(type, adapter, /)

Đăng ký một bộ chuyển đổi 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à đối số duy nhất của nó và phải trả về giá trị của một loại mà SQLite tự hiểu.type that SQLite natively understands.

________ 82 ________ 121 (Typename, Converter, /) ¶(typename, converter, /)

Đăng ký bộ chuyển đổi có thể gọi để chuyển đổi các đối tượng sqlite của loại tên kiểu thành một đối tượng Python thuộc loại cụ thể. Bộ chuyển đổi được gọi cho tất cả các giá trị sqlite của loại kiểu loại; Nó được truyền một đối tượng

cur.execute("CREATE TABLE movie(title, year, score)")
22 và sẽ trả về một đối tượng thuộc loại python mong muốn. Tham khảo ý kiến ​​tham số Detect_types của
cur.execute("CREATE TABLE movie(title, year, score)")
23 để biết thông tin về cách phát hiện loại hoạt động.

LƯU Ý: Tên kiểu và tên của loại trong truy vấn của bạn được khớp với trường hợp không nhạy cảm.

Hằng số mô -đun

________ 82 ________ 125¶

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

cur.execute("CREATE TABLE movie(title, year, score)")
23 để 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 Query, làm phím từ điển chuyển đổi. Tên loại phải được bọc trong ngoặc vuông (
cur.execute("CREATE TABLE movie(title, year, score)")
27).

cur.execute("CREATE TABLE movie(title, year, score)")
0

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

>>> con.close()
>>> new_con = sqlite3.connect("tutorial.db")
>>> new_cur = new_con.cursor()
>>> res = new_cur.execute("SELECT title, year FROM movie ORDER BY score DESC")
>>> title, year = res.fetchone()
>>> print(f'The highest scoring Monty Python movie is {title!r}, released in {year}')
The highest scoring Monty Python movie is 'Monty Python and the Holy Grail', released in 1975
7 bằng toán tử
>>> con.close()
>>> new_con = sqlite3.connect("tutorial.db")
>>> new_cur = new_con.cursor()
>>> res = new_cur.execute("SELECT title, year FROM movie ORDER BY score DESC")
>>> title, year = res.fetchone()
>>> print(f'The highest scoring Monty Python movie is {title!r}, released in {year}')
The highest scoring Monty Python movie is 'Monty Python and the Holy Grail', released in 1975
6 (bitwise hoặc).

________ 82 ________ 131¶

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

cur.execute("CREATE TABLE movie(title, year, score)")
23 để tra cứu chức năng chuyển đổi bằng cách sử dụng các loại được 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 chức năng 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("CREATE TABLE movie(title, year, score)")
1

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

>>> con.close()
>>> new_con = sqlite3.connect("tutorial.db")
>>> new_cur = new_con.cursor()
>>> res = new_cur.execute("SELECT title, year FROM movie ORDER BY score DESC")
>>> title, year = res.fetchone()
>>> print(f'The highest scoring Monty Python movie is {title!r}, released in {year}')
The highest scoring Monty Python movie is 'Monty Python and the Holy Grail', released in 1975
8 bằng toán tử
>>> con.close()
>>> new_con = sqlite3.connect("tutorial.db")
>>> new_cur = new_con.cursor()
>>> res = new_cur.execute("SELECT title, year FROM movie ORDER BY score DESC")
>>> title, year = res.fetchone()
>>> print(f'The highest scoring Monty Python movie is {title!r}, released in {year}')
The highest scoring Monty Python movie is 'Monty Python and the Holy Grail', released in 1975
6 (bitwise hoặc).

________ 82 ________ 137¶ ________ 82 ________ 139¶ ________ 82 ________ 141¶

Những lá cờ nên được trả về bởi Authorizer_callback có thể gọi được chuyển đến

cur.execute("CREATE TABLE movie(title, year, score)")
42, để cho biết liệu:

  • Quyền truy cập được cho phép (

    cur.execute("CREATE TABLE movie(title, year, score)")
    
    43),

  • Câu lệnh SQL phải bị hủy bỏ với lỗi (

    cur.execute("CREATE TABLE movie(title, year, score)")
    
    44)

  • Cột nên được coi là giá trị

    cur.execute("CREATE TABLE movie(title, year, score)")
    
    45 (
    cur.execute("CREATE TABLE movie(title, year, score)")
    
    46)

________ 82 ________ 148¶

Chuỗi không đổi nêu rõ mức DB-API được hỗ trợ. Yêu cầu bởi DB-API. Mã hóa cứng đến

cur.execute("CREATE TABLE movie(title, year, score)")
49.

________ 82 ________ 151¶

Chuỗi không đổi nêu loại định dạng đánh dấu tham số được dự kiến ​​bởi 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. Yêu cầu bởi DB-API. Mã hóa cứng đến
cur.execute("CREATE TABLE movie(title, year, score)")
53.

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ác kiểu tham số
cur.execute("CREATE TABLE movie(title, year, score)")
55 và
cur.execute("CREATE TABLE movie(title, year, score)")
56 DB-API, bởi vì đó là những gì thư viện SQLite cơ bản hỗ trợ. Tuy nhiên, DB-API không cho phép nhiều giá trị cho thuộc tính
cur.execute("CREATE TABLE movie(title, year, score)")
57.

________ 82 ________ 159¶

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

cur.execute("CREATE TABLE movie(title, year, score)")
60.

________ 82 ________ 162¶

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

>>> res = cur.execute("SELECT score FROM movie")
>>> res.fetchall()
[(8.2,), (7.5,)]
8 của
cur.execute("CREATE TABLE movie(title, year, score)")
64.

________ 82 ________ 166¶

Hằng số số nguyên theo yêu cầu của DB-API, nêu rõ mức độ an toàn của luồng là 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ợ. Hiện tại được mã hóa cứng đến
cur.execute("CREATE TABLE movie(title, year, score)")
68, có nghĩa là các chủ đề có thể chia sẻ mô-đun, nhưng không phải là kết nối. Tuy nhiên, điều này có thể không phải lúc nào cũng đúng. Bạn có thể kiểm tra chế độ luồng thời gian biên dịch SQLite bên dưới bằng cách sử dụng truy vấn sau:

cur.execute("CREATE TABLE movie(title, year, score)")
2

Lưu ý rằng các cấp SQLite_ThreadSafe không khớp với các cấp DB-API 2.0

cur.execute("CREATE TABLE movie(title, year, score)")
69.

________ 82 ________ 171¶

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

cur.execute("CREATE TABLE movie(title, year, score)")
60. Đây không phải là phiên bản của thư viện SQLite.

________ 82 ________ 174¶

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

>>> res = cur.execute("SELECT score FROM movie")
>>> res.fetchall()
[(8.2,), (7.5,)]
8 của
cur.execute("CREATE TABLE movie(title, year, score)")
64. Đâ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 ________ 82 ________ 178¶

Mỗi cơ sở dữ liệu SQLite mở được biểu thị bằ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)
""")
6, được tạ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)
""")
4. Mục đích chính của họ là tạo các đối tượng
cur.execute("""
    INSERT INTO movie VALUES
        ('Monty Python and the Holy Grail', 1975, 8.2),
        ('And Now for Something Completely Different', 1971, 7.5)
""")
9 và kiểm soát giao dịch.Transaction control.

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

________ 182 (nhà máy = con trỏ) ¶(factory=Cursor)

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)
""")
9. 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 là một ví dụ 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)
""")
9 hoặc các lớp con của nó.

________ 185 ()()

Cam kết bất kỳ 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 có op.

________ 186 ()()

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

________ 187 ()()

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

cur.execute("CREATE TABLE movie(title, year, score)")
88 trước khi đóng để tránh mất những thay đổi đang chờ xử lý.

________ 189 (SQL, tham số = (), /) ¶(sql, parameters=(), /)

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

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

________ 192 (SQL, tham số, /) ¶(sql, parameters, /)

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

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

________ 195 (SQL_Script, /) ¶(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)
""")
9 mới và gọi
cur.execute("CREATE TABLE movie(title, year, score)")
97 trên đó với SQL_Script đã cho. Trả về đối tượng con trỏ mới.

________ 198 (Tên, Narg, Func, \*, xác định = Sai) ¶(name, narg, func, \*, deterministic=False)

Tạo hoặc xóa chức năng SQL do người dùng xác định.

Thông số
  • Tên (str) - Tên của hàm SQL. (str) – The name of the SQL function.

  • NARG (int) - Số lượng đối số mà hàm SQL có thể chấp nhận. Nếu

    cur.execute("CREATE TABLE movie(title, year, score)")
    
    99, nó có thể lấy bất kỳ số lượng đối số nào. (int) – The number of arguments the SQL function can accept. If
    cur.execute("CREATE TABLE movie(title, year, score)")
    
    99, it may take any number of arguments.

  • Func (Callback | none) - một cuộc gọi có thể gọi được gọi khi hàm SQL được gọi. Người có thể gọi phải trả về một loại được hỗ trợ bởi SQLite. Đặt thành

    data = [
        ("Monty Python Live at the Hollywood Bowl", 1982, 7.9),
        ("Monty Python's The Meaning of Life", 1983, 7.5),
        ("Monty Python's Life of Brian", 1979, 8.0),
    ]
    cur.executemany("INSERT INTO movie VALUES(?, ?, ?)", data)
    con.commit()  # Remember to commit the transaction after executing INSERT.
    
    2 để xóa chức năng SQL hiện có. (callback | None) – A callable that is called when the SQL function is invoked. The callable must return a type natively supported by SQLite. Set to
    data = [
        ("Monty Python Live at the Hollywood Bowl", 1982, 7.9),
        ("Monty Python's The Meaning of Life", 1983, 7.5),
        ("Monty Python's Life of Brian", 1979, 8.0),
    ]
    cur.executemany("INSERT INTO movie VALUES(?, ?, ?)", data)
    con.commit()  # Remember to commit the transaction after executing INSERT.
    
    2 to remove an existing SQL function.

  • Xác định (bool) - Nếu

    >>> sqlite3.complete_statement("SELECT foo FROM bar;")
    True
    >>> sqlite3.complete_statement("SELECT foo")
    False
    
    7, hàm SQL được tạo được đánh dấu là xác định, cho phép SQLite thực hiện tối ưu hóa bổ sung. (bool) – If
    >>> sqlite3.complete_statement("SELECT foo FROM bar;")
    True
    >>> sqlite3.complete_statement("SELECT foo")
    False
    
    7, the created SQL function is marked as deterministic, which allows SQLite to perform additional optimizations.

Tăng

NotSupportedError - Nếu xác định được sử dụng với các phiên bản SQLite cũ hơn 3,8.3. – If deterministic is used with SQLite versions older than 3.8.3.

Mới trong phiên bản 3.8: Tham số xác định.The deterministic parameter.

Example:

cur.execute("CREATE TABLE movie(title, year, score)")
3

________ 202 (tên, /, n_arg, tổng hợp_class) ¶(name, /, n_arg, aggregate_class)

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

Thông số
  • Tên (STR) - Tên của hàm tổng hợp SQL. (str) – The name of the SQL aggregate function.

  • 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("CREATE TABLE movie(title, year, score)")
    
    99, nó có thể lấy bất kỳ số lượng đối số nào. (int) – The number of arguments the SQL aggregate function can accept. If
    cur.execute("CREATE TABLE movie(title, year, score)")
    
    99, it may take any number of arguments.

  • tổng hợp_class (lớp | none) - (class | None) –

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

    • >>> res = cur.execute("SELECT name FROM sqlite_master")
      >>> res.fetchone()
      ('movie',)
      
      04: Thêm một hàng vào tổng hợp.

    • >>> res = cur.execute("SELECT name FROM sqlite_master")
      >>> res.fetchone()
      ('movie',)
      
      05: Trả về kết quả cuối cùng của tổng hợp dưới dạng loại được hỗ trợ bởi SQLite.a type natively supported by SQLite.

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

    >>> res = cur.execute("SELECT name FROM sqlite_master")
    >>> res.fetchone()
    ('movie',)
    
    04 phải chấp nhận được kiểm soát bởi N_ARG.

    Đặt thành

    data = [
        ("Monty Python Live at the Hollywood Bowl", 1982, 7.9),
        ("Monty Python's The Meaning of Life", 1983, 7.5),
        ("Monty Python's Life of Brian", 1979, 8.0),
    ]
    cur.executemany("INSERT INTO movie VALUES(?, ?, ?)", data)
    con.commit()  # Remember to commit the transaction after executing INSERT.
    
    2 để loại bỏ hàm tổng hợp SQL hiện có.

Example:

cur.execute("CREATE TABLE movie(title, year, score)")
4

________ 208 (tên, có thể gọi) ¶(name, callable)

Tạo tên đối chiếu tên bằng cách sử dụng hàm đối chiếu có thể gọi được. Có thể gọi được được thông qua hai đối số

cur.execute("CREATE TABLE movie(title, year, score)")
60 và nó sẽ trả về một
>>> res = cur.execute("SELECT name FROM sqlite_master")
>>> res.fetchone()
('movie',)
10:

  • cur.execute("CREATE TABLE movie(title, year, score)")
    
    68 nếu thứ nhất được đặt hàng cao hơn thứ hai

  • cur.execute("CREATE TABLE movie(title, year, score)")
    
    99 Nếu thứ nhất được đặt hàng thấp hơn thứ hai

  • >>> sqlite3.complete_statement("SELECT foo FROM bar;")
    True
    >>> sqlite3.complete_statement("SELECT foo")
    False
    
    1 nếu chúng được đặt hàng bằng nhau

Ví dụ sau đây cho thấy một đối chiếu phân loại ngược:

cur.execute("CREATE TABLE movie(title, year, score)")
5

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

data = [
    ("Monty Python Live at the Hollywood Bowl", 1982, 7.9),
    ("Monty Python's The Meaning of Life", 1983, 7.5),
    ("Monty Python's Life of Brian", 1979, 8.0),
]
cur.executemany("INSERT INTO movie VALUES(?, ?, ?)", data)
con.commit()  # Remember to commit the transaction after executing INSERT.
2.

________ 215 ()()

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

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

cur.execute("CREATE TABLE movie(title, year, score)")
43,
cur.execute("CREATE TABLE movie(title, year, score)")
44 hoặc
cur.execute("CREATE TABLE movie(title, year, score)")
46 để báo hiệu cách truy cập vào cột nên được xử lý bởi thư viện SQLite bên dưới.

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

data = [
    ("Monty Python Live at the Hollywood Bowl", 1982, 7.9),
    ("Monty Python's The Meaning of Life", 1983, 7.5),
    ("Monty Python's Life of Brian", 1979, 8.0),
]
cur.executemany("INSERT INTO movie VALUES(?, ?, ?)", data)
con.commit()  # Remember to commit the transaction after executing INSERT.
2 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 (Hồi giáo chính, Hồi giáo, v.v.) nếu có. Đối số thứ 5 là tên của bộ kích hoạt bên trong hoặc chế độ xem chịu trách nhiệm cho nỗ lực truy cập hoặc
data = [
    ("Monty Python Live at the Hollywood Bowl", 1982, 7.9),
    ("Monty Python's The Meaning of Life", 1983, 7.5),
    ("Monty Python's Life of Brian", 1979, 8.0),
]
cur.executemany("INSERT INTO movie VALUES(?, ?, ?)", data)
con.commit()  # Remember to commit the transaction after executing INSERT.
2 nếu lần thử 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ể cho đối số đầu tiên và ý nghĩa của đối số thứ hai và thứ ba tùy thuộc vào dữ liệu đầu tiên. Tất cả các hằng số cần thiết 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.

________ 222 (Progress_Handler, N) ¶(progress_handler, n)

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

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

data = [
    ("Monty Python Live at the Hollywood Bowl", 1982, 7.9),
    ("Monty Python's The Meaning of Life", 1983, 7.5),
    ("Monty Python's Life of Brian", 1979, 8.0),
]
cur.executemany("INSERT INTO movie VALUES(?, ?, ?)", data)
con.commit()  # Remember to commit the transaction after executing INSERT.
2 cho Progress_Handler.

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

>>> res = cur.execute("SELECT name FROM sqlite_master")
>>> res.fetchone()
('movie',)
24.

________ 225 (Trace_Callback) ¶(trace_callback)

Đăng ký có thể gọi Trace_callback để được gọi cho mỗi câu lệnh SQL thực sự được thực hiện bởi phụ trợ SQLite.

Đối số duy nhất được chuyển cho cuộc gọi lại là câu lệnh (như

>>> sqlite3.complete_statement("SELECT foo FROM bar;")
True
>>> sqlite3.complete_statement("SELECT foo")
False
0) đang được thực thi. Giá trị trả về của cuộc gọi lại bị bỏ qua. Lưu ý rằng phần phụ trợ không chỉ chạy các câu lệnh được chuyển cho các phương thức
>>> res = cur.execute("SELECT name FROM sqlite_master")
>>> res.fetchone()
('movie',)
27. 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à thực hiện các kích hoạt được xác định trong cơ sở dữ liệu hiện tại.transaction management 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)
""")
0 module and the execution of triggers defined in the current database.

Vượt qua

data = [
    ("Monty Python Live at the Hollywood Bowl", 1982, 7.9),
    ("Monty Python's The Meaning of Life", 1983, 7.5),
    ("Monty Python's Life of Brian", 1979, 8.0),
]
cur.executemany("INSERT INTO movie VALUES(?, ?, ?)", data)
con.commit()  # Remember to commit the transaction after executing INSERT.
2 vì Trace_Callback sẽ vô hiệu hóa cuộc gọi lại theo dõi.

Ghi chú

Các ngoại lệ được nêu trong cuộc gọi lại dấu vết không được truyền bá. Là một hỗ trợ phát triển và gỡ lỗi, sử dụng

>>> res = cur.execute("SELECT name FROM sqlite_master")
>>> res.fetchone()
('movie',)
30 để cho phép in dấu vết từ các trường hợp ngoại lệ được nêu trong cuộc gọi lại theo dõi.

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

________ 231 (bật, /) ¶(enabled, /)

Bật công cụ SQLite tải các tiện ích mở rộng SQLite từ các thư viện được chia sẻ nếu được bật là

>>> sqlite3.complete_statement("SELECT foo FROM bar;")
True
>>> sqlite3.complete_statement("SELECT foo")
False
7; khác, không cho phép tải các phần mở rộng sqlite. Các tiện ích mở rộng SQLite có thể xác định các chức năng mới, tập hợp hoặc triển khai bảng ảo hoàn toàn mới. Một tiện ích mở rộng nổi tiếng là phần mở rộng FullText-search được phân phối với sqlite.

Ghi chú

Các ngoại lệ được nêu trong cuộc gọi lại dấu vết không được truyền bá. Là một hỗ trợ phát triển và gỡ lỗi, sử dụng

>>> res = cur.execute("SELECT name FROM sqlite_master")
>>> res.fetchone()
('movie',)
30 để cho phép in dấu vết từ các trường hợp ngoại lệ được nêu trong cuộc gọi lại theo dõi.configure.

Mới trong phiên bản 3.3.auditing event

>>> res = cur.execute("SELECT name FROM sqlite_master")
>>> res.fetchone()
('movie',)
35 with arguments
>>> res = cur.execute("SELECT name FROM sqlite_master")
>>> res.fetchone()
('movie',)
36,
>>> res = cur.execute("SELECT name FROM sqlite_master")
>>> res.fetchone()
('movie',)
37.

________ 231 (bật, /) ¶

Bật công cụ SQLite tải các tiện ích mở rộng SQLite từ các thư viện được chia sẻ nếu được bật là

>>> sqlite3.complete_statement("SELECT foo FROM bar;")
True
>>> sqlite3.complete_statement("SELECT foo")
False
7; khác, không cho phép tải các phần mở rộng sqlite. Các tiện ích mở rộng SQLite có thể xác định các chức năng mới, tập hợp hoặc triển khai bảng ảo hoàn toàn mới. Một tiện ích mở rộng nổi tiếng là phần mở rộng FullText-search được phân phối với sqlite.Added the
>>> res = cur.execute("SELECT name FROM sqlite_master")
>>> res.fetchone()
('movie',)
35 auditing event.

cur.execute("CREATE TABLE movie(title, year, score)")
6

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ợ mở rộng có thể tải theo mặc định, bởi vì một số nền tảng (đáng chú ý là macOS) có các thư viện SQLite được biên dịch mà không có tính năng này. Để có được hỗ trợ tiện ích mở rộng có thể tải, bạn phải vượt qua tùy chọn
>>> res = cur.execute("SELECT name FROM sqlite_master")
>>> res.fetchone()
('movie',)
34 để định cấu hình.(path, /)

Tải một phần mở rộng SQLite từ một thư viện được chia sẻ tại Path. Bật tải mở rộng với

>>> res = cur.execute("SELECT name FROM sqlite_master")
>>> res.fetchone()
('movie',)
40 trước khi gọi phương thức này.

Tăng một sự kiện kiểm toán

>>> res = cur.execute("SELECT name FROM sqlite_master")
>>> res.fetchone()
('movie',)
41 với các đối số
>>> res = cur.execute("SELECT name FROM sqlite_master")
>>> res.fetchone()
('movie',)
36,
>>> res = cur.execute("SELECT name FROM sqlite_master")
>>> res.fetchone()
('movie',)
43.auditing event
>>> res = cur.execute("SELECT name FROM sqlite_master")
>>> res.fetchone()
('movie',)
41 with arguments
>>> res = cur.execute("SELECT name FROM sqlite_master")
>>> res.fetchone()
('movie',)
36,
>>> res = cur.execute("SELECT name FROM sqlite_master")
>>> res.fetchone()
('movie',)
43.

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

>>> res = cur.execute("SELECT name FROM sqlite_master")
>>> res.fetchone()
('movie',)
41.Added the
>>> res = cur.execute("SELECT name FROM sqlite_master")
>>> res.fetchone()
('movie',)
41 auditing event.

________ 245 ()()

Trả về một trình lặp để đổ 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

>>> res = cur.execute("SELECT name FROM sqlite_master")
>>> res.fetchone()
('movie',)
46 trong shell sqlite3.iterator to dump the database as SQL source code. Useful when saving an in-memory database for later restoration. Similar to the
>>> res = cur.execute("SELECT name FROM sqlite_master")
>>> res.fetchone()
('movie',)
46 command in the sqlite3 shell.

Example:

cur.execute("CREATE TABLE movie(title, year, score)")
7

________ 247 (Target, \*, trang = -1, tiến trình = không, name = "chính", ngủ = 0,250)(target, \*, pages=-1, progress=None, name="main", sleep=0.250)

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

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

Thông số
  • Target (Kết nối) - Kết nối cơ sở dữ liệu để lưu bản sao lưu vào. (Connection) – The database connection to save the backup to.

  • Trang (int) - Số lượng trang để sao chép cùng một lúc. Nếu bằng hoặc nhỏ hơn

    >>> sqlite3.complete_statement("SELECT foo FROM bar;")
    True
    >>> sqlite3.complete_statement("SELECT foo")
    False
    
    1, toàn bộ cơ sở dữ liệu được sao chép trong một bước duy nhất. Mặc định là
    cur.execute("CREATE TABLE movie(title, year, score)")
    
    99.
    (int) – The number of pages to copy at a time. If equal to or less than
    >>> sqlite3.complete_statement("SELECT foo FROM bar;")
    True
    >>> sqlite3.complete_statement("SELECT foo")
    False
    
    1, the entire database is copied in a single step. Defaults to
    cur.execute("CREATE TABLE movie(title, year, score)")
    
    99.

  • tiến trình (gọi lại | & nbsp; none) - nếu được đặt thành một cuộc gọi, nó được gọi với ba đối số 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à

    data = [
        ("Monty Python Live at the Hollywood Bowl", 1982, 7.9),
        ("Monty Python's The Meaning of Life", 1983, 7.5),
        ("Monty Python's Life of Brian", 1979, 8.0),
    ]
    cur.executemany("INSERT INTO movie VALUES(?, ?, ?)", data)
    con.commit()  # Remember to commit the transaction after executing INSERT.
    
    2. (callback | None) – If set to a callable, it is invoked with three integer arguments for every backup iteration: the status of the last iteration, the remaining number of pages still to be copied, and the total number of pages. Defaults to
    data = [
        ("Monty Python Live at the Hollywood Bowl", 1982, 7.9),
        ("Monty Python's The Meaning of Life", 1983, 7.5),
        ("Monty Python's Life of Brian", 1979, 8.0),
    ]
    cur.executemany("INSERT INTO movie VALUES(?, ?, ?)", data)
    con.commit()  # Remember to commit the transaction after executing INSERT.
    
    2.

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

    >>> res = cur.execute("SELECT name FROM sqlite_master")
    >>> res.fetchone()
    ('movie',)
    
    51 (mặc định) cho cơ sở dữ liệu chính,
    >>> res = cur.execute("SELECT name FROM sqlite_master")
    >>> res.fetchone()
    ('movie',)
    
    52 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âu lệnh
    >>> res = cur.execute("SELECT name FROM sqlite_master")
    >>> res.fetchone()
    ('movie',)
    
    53 SQL.
    (str) – The name of the database to back up. Either
    >>> res = cur.execute("SELECT name FROM sqlite_master")
    >>> res.fetchone()
    ('movie',)
    
    51 (the default) for the main database,
    >>> res = cur.execute("SELECT name FROM sqlite_master")
    >>> res.fetchone()
    ('movie',)
    
    52 for the temporary database, or the name of a custom database as attached using the
    >>> res = cur.execute("SELECT name FROM sqlite_master")
    >>> res.fetchone()
    ('movie',)
    
    53 SQL statement.

  • Ngủ (Float) - Số giây để ngủ giữa những nỗ lực liên tiếp để sao lưu các trang còn lại. (float) – The number of seconds to sleep between successive attempts to back up remaining pages.

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

cur.execute("CREATE TABLE movie(title, year, score)")
8

Ví dụ 2, sao chép cơ sở dữ liệu hiện có vào một bản sao thoáng qua:

cur.execute("CREATE TABLE movie(title, year, score)")
9

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

________ 254¶

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

>>> sqlite3.complete_statement("SELECT foo FROM bar;")
True
>>> sqlite3.complete_statement("SELECT foo")
False
7 Nếu một giao dịch đang hoạt động (có những thay đổi không cam kết),
>>> sqlite3.complete_statement("SELECT foo FROM bar;")
True
>>> sqlite3.complete_statement("SELECT foo")
False
8 nếu không.

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
>>> res = cur.execute("SELECT name FROM sqlite_master")
>>> res.fetchone()
('movie',)
41.

________ 245 ()transaction handling performed by

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. If set to
data = [
    ("Monty Python Live at the Hollywood Bowl", 1982, 7.9),
    ("Monty Python's The Meaning of Life", 1983, 7.5),
    ("Monty Python's Life of Brian", 1979, 8.0),
]
cur.executemany("INSERT INTO movie VALUES(?, ?, ?)", data)
con.commit()  # Remember to commit the transaction after executing INSERT.
2, transactions are never implicitly opened. If set to one of
>>> sqlite3.complete_statement("SELECT foo FROM bar;")
True
>>> sqlite3.complete_statement("SELECT foo")
False
3,
>>> sqlite3.complete_statement("SELECT foo FROM bar;")
True
>>> sqlite3.complete_statement("SELECT foo")
False
5, or
>>> sqlite3.complete_statement("SELECT foo FROM bar;")
True
>>> sqlite3.complete_statement("SELECT foo")
False
4, corresponding to the underlying SQLite transaction behaviour, implicit transaction management is performed.

Trả về một trình lặp để đổ 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

>>> res = cur.execute("SELECT name FROM sqlite_master")
>>> res.fetchone()
('movie',)
46 trong shell sqlite3.

________ 247 (Target, \*, trang = -1, tiến trình = không, name = "chính", ngủ = 0,250)

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

Example:

>>> res = cur.execute("SELECT name FROM sqlite_master")
>>> res.fetchone()
('movie',)
0

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

Thông số

Target (Kết nối) - Kết nối cơ sở dữ liệu để lưu bản sao lưu vào.

Example:

>>> res = cur.execute("SELECT name FROM sqlite_master")
>>> res.fetchone()
('movie',)
1

Trang (int) - Số lượng trang để sao chép cùng một lúc. Nếu bằng hoặc nhỏ hơn
>>> sqlite3.complete_statement("SELECT foo FROM bar;")
True
>>> sqlite3.complete_statement("SELECT foo")
False
1, toàn bộ cơ sở dữ liệu được sao chép trong một bước duy nhất. Mặc định là
cur.execute("CREATE TABLE movie(title, year, score)")
99.

tiến trình (gọi lại | & nbsp; none) - nếu được đặt thành một cuộc gọi, nó được gọi với ba đối số 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à

data = [
    ("Monty Python Live at the Hollywood Bowl", 1982, 7.9),
    ("Monty Python's The Meaning of Life", 1983, 7.5),
    ("Monty Python's Life of Brian", 1979, 8.0),
]
cur.executemany("INSERT INTO movie VALUES(?, ?, ?)", data)
con.commit()  # Remember to commit the transaction after executing INSERT.
2.

Tên (STR) - Tên của cơ sở dữ liệu để sao lưu. >>> res = cur.execute("SELECT name FROM sqlite_master") >>> res.fetchone() ('movie',) 51 (mặc định) cho cơ sở dữ liệu chính, >>> res = cur.execute("SELECT name FROM sqlite_master") >>> res.fetchone() ('movie',) 52 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âu lệnh >>> res = cur.execute("SELECT name FROM sqlite_master") >>> res.fetchone() ('movie',) 53 SQL.

Ngủ (Float) - Số giây để ngủ giữa những nỗ lực liên tiếp để sao lưu các trang còn lại.connection shortcut methods.

Ví dụ 1, sao chép cơ sở dữ liệu hiện có vào một cơ sở dữ liệu khác:iterators, meaning that if you

cur.execute("CREATE TABLE movie(title, year, score)")
12 a
data = [
    ("Monty Python Live at the Hollywood Bowl", 1982, 7.9),
    ("Monty Python's The Meaning of Life", 1983, 7.5),
    ("Monty Python's Life of Brian", 1979, 8.0),
]
cur.executemany("INSERT INTO movie VALUES(?, ?, ?)", data)
con.commit()  # Remember to commit the transaction after executing INSERT.
7 query, you can simply iterate over the cursor to fetch the resulting rows:

>>> res = cur.execute("SELECT name FROM sqlite_master")
>>> res.fetchone()
('movie',)
2

Ví dụ 2, sao chép cơ sở dữ liệu hiện có vào một bản sao thoáng qua:

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

________ 254¶(sql, parameters=(), /)

Thuộc tính chỉ đọc này tương ứng với chế độ AutoCommit SQLite cấp thấp.placeholders that map to the sequence or

>>> res = cur.execute("SELECT name FROM sqlite_master")
>>> res.fetchone()
('movie',)
87 parameters.

>>> sqlite3.complete_statement("SELECT foo FROM bar;")
True
>>> sqlite3.complete_statement("SELECT foo")
False
7 Nếu một giao dịch đang hoạt động (có những thay đổi không cam kết),
>>> sqlite3.complete_statement("SELECT foo FROM bar;")
True
>>> sqlite3.complete_statement("SELECT foo")
False
8 nếu không.

________ 257¶

Thuộc tính này kiểm soát việc xử lý giao dịch được thực hiện 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)
""")
0. Nếu được đặt thành
data = [
    ("Monty Python Live at the Hollywood Bowl", 1982, 7.9),
    ("Monty Python's The Meaning of Life", 1983, 7.5),
    ("Monty Python's Life of Brian", 1979, 8.0),
]
cur.executemany("INSERT INTO movie VALUES(?, ?, ?)", data)
con.commit()  # Remember to commit the transaction after executing INSERT.
2, các giao dịch không bao giờ được mở ngầm. Nếu được đặt thành một trong
>>> sqlite3.complete_statement("SELECT foo FROM bar;")
True
>>> sqlite3.complete_statement("SELECT foo")
False
3,
>>> sqlite3.complete_statement("SELECT foo FROM bar;")
True
>>> sqlite3.complete_statement("SELECT foo")
False
5 hoặc
>>> sqlite3.complete_statement("SELECT foo FROM bar;")
True
>>> sqlite3.complete_statement("SELECT foo")
False
4, tương ứng với hành vi giao dịch SQLite cơ bản, quản lý giao dịch ngầm sẽ được thực hiện.(sql, parameters, /)

Nếu không được ghi đè bởi tham số slecation_level của

cur.execute("CREATE TABLE movie(title, year, score)")
23, mặc định là
>>> res = cur.execute("SELECT name FROM sqlite_master")
>>> res.fetchone()
('movie',)
64, đây là bí danh cho
>>> sqlite3.complete_statement("SELECT foo FROM bar;")
True
>>> sqlite3.complete_statement("SELECT foo")
False
3.parameterized SQL statement sql against all parameter sequences or mappings found in the sequence parameters. It is also possible to use an iterator yielding parameters instead of a sequence. Uses the same implicit transaction handling as
cur.execute("CREATE TABLE movie(title, year, score)")
12.

Example:

>>> res = cur.execute("SELECT name FROM sqlite_master")
>>> res.fetchone()
('movie',)
3

________ 195 (SQL_Script, /) ¶(sql_script, /)

Thực hiện 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

>>> res = cur.execute("SELECT name FROM sqlite_master WHERE name='spam'")
>>> res.fetchone() is None
True
00 ngầm được thực hiện trước tiên. Không có kiểm soát giao dịch ngầm nào khác được thực hiện; Bất kỳ điều khiển giao dịch phải được thêm vào SQL_Script.

SQL_Script phải là

cur.execute("CREATE TABLE movie(title, year, score)")
60.

Example:

>>> res = cur.execute("SELECT name FROM sqlite_master")
>>> res.fetchone()
('movie',)
4

________ 302 ()()

Nếu

>>> res = cur.execute("SELECT name FROM sqlite_master")
>>> res.fetchone()
('movie',)
69 là
data = [
    ("Monty Python Live at the Hollywood Bowl", 1982, 7.9),
    ("Monty Python's The Meaning of Life", 1983, 7.5),
    ("Monty Python's Life of Brian", 1979, 8.0),
]
cur.executemany("INSERT INTO movie VALUES(?, ?, ?)", data)
con.commit()  # Remember to commit the transaction after executing INSERT.
2, hãy trả về kết quả truy vấn hàng tiếp theo được đặt là
>>> res = cur.execute("SELECT score FROM movie")
>>> res.fetchall()
[(8.2,), (7.5,)]
8. Khác, chuyển nó cho nhà máy hàng và trả về kết quả của nó. Trả về
data = [
    ("Monty Python Live at the Hollywood Bowl", 1982, 7.9),
    ("Monty Python's The Meaning of Life", 1983, 7.5),
    ("Monty Python's Life of Brian", 1979, 8.0),
]
cur.executemany("INSERT INTO movie VALUES(?, ?, ?)", data)
con.commit()  # Remember to commit the transaction after executing INSERT.
2 nếu không có thêm dữ liệu.

________ 307 (size = con trỏ.arraysize) ¶(size=cursor.arraysize)

Trả về bộ hàng tiếp theo của kết quả truy vấn là

>>> for row in cur.execute("SELECT year, title FROM movie ORDER BY year"):
...     print(row)
(1971, 'And Now for Something Completely Different')
(1975, 'Monty Python and the Holy Grail')
(1979, "Monty Python's Life of Brian")
(1982, 'Monty Python Live at the Hollywood Bowl')
(1983, "Monty Python's The Meaning of Life")
1. Trả lại một danh sách trống nếu không có thêm hàng.

Số lượng hàng để tìm nạp trên 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 đưa ra,

>>> res = cur.execute("SELECT name FROM sqlite_master WHERE name='spam'")
>>> res.fetchone() is None
True
09 xác định số lượng hàng sẽ được tìm nạp. Nếu có ít hơn các hàng kích thước, càng nhiều hàng có sẵn được trả về.

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

>>> res = cur.execute("SELECT name FROM sqlite_master WHERE name='spam'")
>>> res.fetchone() is None
True
10 sang cuộc gọi tiếp theo.

________ 311 ()()

Trả về tất cả (còn lại) các hàng của một truy vấn kết quả là

>>> for row in cur.execute("SELECT year, title FROM movie ORDER BY year"):
...     print(row)
(1971, 'And Now for Something Completely Different')
(1975, 'Monty Python and the Holy Grail')
(1979, "Monty Python's Life of Brian")
(1982, 'Monty Python Live at the Hollywood Bowl')
(1983, "Monty Python's The Meaning of Life")
1. Trả lại một danh sách trống nếu không có hàng. Lưu ý rằng thuộc tính
>>> res = cur.execute("SELECT name FROM sqlite_master WHERE name='spam'")
>>> res.fetchone() is None
True
09 có thể ảnh hưởng đến hiệu suất của hoạt động này.

________ 187 ()()

Đóng con trỏ ngay bây giờ (thay vì bất cứ khi nào

>>> res = cur.execute("SELECT name FROM sqlite_master WHERE name='spam'")
>>> res.fetchone() is None
True
15 được gọi).

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

>>> res = cur.execute("SELECT name FROM sqlite_master WHERE name='spam'")
>>> res.fetchone() is None
True
16 sẽ được nâng lên nếu có bất kỳ hoạt động nào được thử với con trỏ.

________ 317 (Kích thước, /) ¶(sizes, /)

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

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

________ 319 (kích thước, cột = không, /) ¶(size, column=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.

________ 319 (kích thước, cột = không, /) ¶

________ 321¶

Thuộc tính đọc/ghi kiểm soát số lượng hàng được trả về bởi
>>> res = cur.execute("SELECT name FROM sqlite_master WHERE name='spam'")
>>> res.fetchone() is None
True
10. 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.

________ 323¶

>>> res = cur.execute("SELECT name FROM sqlite_master")
>>> res.fetchone()
('movie',)
5

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)
""")
6 thuộc về 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)
""")
9 đượ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)
""")
8 sẽ có thuộc tính
>>> res = cur.execute("SELECT name FROM sqlite_master")
>>> res.fetchone()
('movie',)
36 đề cập đến Con:

________ 328¶

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

data = [
    ("Monty Python Live at the Hollywood Bowl", 1982, 7.9),
    ("Monty Python's The Meaning of Life", 1983, 7.5),
    ("Monty Python's Life of Brian", 1979, 8.0),
]
cur.executemany("INSERT INTO movie VALUES(?, ?, ?)", data)
con.commit()  # Remember to commit the transaction after executing INSERT.
2.

Nó được đặt cho các câu lệnh
data = [
    ("Monty Python Live at the Hollywood Bowl", 1982, 7.9),
    ("Monty Python's The Meaning of Life", 1983, 7.5),
    ("Monty Python's Life of Brian", 1979, 8.0),
]
cur.executemany("INSERT INTO movie VALUES(?, ?, ?)", data)
con.commit()  # Remember to commit the transaction after executing INSERT.
7 mà không có bất kỳ hàng phù hợp nào.

________ 331¶

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ác câu lệnh

data = [
    ("Monty Python Live at the Hollywood Bowl", 1982, 7.9),
    ("Monty Python's The Meaning of Life", 1983, 7.5),
    ("Monty Python's Life of Brian", 1979, 8.0),
]
cur.executemany("INSERT INTO movie VALUES(?, ?, ?)", data)
con.commit()  # Remember to commit the transaction after executing INSERT.
3 hoặc
>>> res = cur.execute("SELECT name FROM sqlite_master")
>>> res.fetchone()
('movie',)
96 thành công bằng phương pháp
cur.execute("CREATE TABLE movie(title, year, score)")
12. Đối với các tuyên bố khác, sau
cur.execute("CREATE TABLE movie(title, year, score)")
94 hoặc
cur.execute("CREATE TABLE movie(title, year, score)")
97 hoặc nếu chèn không thành công, giá trị của
>>> res = cur.execute("SELECT name FROM sqlite_master WHERE name='spam'")
>>> res.fetchone() is None
True
37 không thay đổi. Giá trị ban đầu của
>>> res = cur.execute("SELECT name FROM sqlite_master WHERE name='spam'")
>>> res.fetchone() is None
True
37 là
data = [
    ("Monty Python Live at the Hollywood Bowl", 1982, 7.9),
    ("Monty Python's The Meaning of Life", 1983, 7.5),
    ("Monty Python's Life of Brian", 1979, 8.0),
]
cur.executemany("INSERT INTO movie VALUES(?, ?, ?)", data)
con.commit()  # Remember to commit the transaction after executing INSERT.
2.

Ghi chú

Chèn vào bảng

>>> res = cur.execute("SELECT name FROM sqlite_master WHERE name='spam'")
>>> res.fetchone() is None
True
40 không được ghi lại.Added support for the
>>> res = cur.execute("SELECT name FROM sqlite_master")
>>> res.fetchone()
('movie',)
96 statement.

Đã thay đổi trong phiên bản 3.6: Đã thêm hỗ trợ cho câu lệnh
>>> res = cur.execute("SELECT name FROM sqlite_master")
>>> res.fetchone()
('movie',)
96.

________ 342¶

Thuộc tính chỉ đọc cung cấp số lượng hàng đã sửa đổi cho các câu lệnh data = [ ("Monty Python Live at the Hollywood Bowl", 1982, 7.9), ("Monty Python's The Meaning of Life", 1983, 7.5), ("Monty Python's Life of Brian", 1979, 8.0), ] cur.executemany("INSERT INTO movie VALUES(?, ?, ?)", data) con.commit() # Remember to commit the transaction after executing INSERT. 3, >>> res = cur.execute("SELECT name FROM sqlite_master") >>> res.fetchone() ('movie',) 94, >>> res = cur.execute("SELECT name FROM sqlite_master") >>> res.fetchone() ('movie',) 95 và >>> res = cur.execute("SELECT name FROM sqlite_master") >>> res.fetchone() ('movie',) 96; là cur.execute("CREATE TABLE movie(title, year, score)") 99 cho các câu lệnh khác, bao gồm các truy vấn CTE. Nó chỉ được cập nhật bởi các phương thức cur.execute("CREATE TABLE movie(title, year, score)") 12 và cur.execute("CREATE TABLE movie(title, year, score)") 94.

Đối tượng hàng

Lớp ________ 82 ________ 351¶mapping access by column name and index.

Một ví dụ

>>> res = cur.execute("SELECT name FROM sqlite_master")
>>> res.fetchone()
('movie',)
71 đóng vai trò là một
>>> res = cur.execute("SELECT name FROM sqlite_master")
>>> res.fetchone()
('movie',)
69 đượ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)
""")
6. Nó hỗ trợ lặp lại, kiểm tra bình đẳng,
>>> res = cur.execute("SELECT name FROM sqlite_master WHERE name='spam'")
>>> res.fetchone() is None
True
55 và truy cập ánh xạ theo tên cột và chỉ mục.

Hai đối tượng hàng so sánh bằng nhau nếu có các cột bằng nhau và thành viên bằng nhau.()

________ 356 ()

Trả về

>>> for row in cur.execute("SELECT year, title FROM movie ORDER BY year"):
...     print(row)
(1971, 'And Now for Something Completely Different')
(1975, 'Monty Python and the Holy Grail')
(1979, "Monty Python's Life of Brian")
(1982, 'Monty Python Live at the Hollywood Bowl')
(1983, "Monty Python's The Meaning of Life")
1 tên cột là
>>> res = cur.execute("SELECT name FROM sqlite_master WHERE name='spam'")
>>> res.fetchone() is None
True
58. Ngay sau một truy vấn, đây là thành viên đầu tiên của mỗi tuple trong
>>> res = cur.execute("SELECT name FROM sqlite_master WHERE name='spam'")
>>> res.fetchone() is None
True
59.Added support of slicing.

Example:

>>> res = cur.execute("SELECT name FROM sqlite_master")
>>> res.fetchone()
('movie',)
6

Thay đổi trong phiên bản 3.5: Thêm hỗ trợ cắt lát.

Đối tượng chuẩn bị

Lớp ________ 82 ________ 361¶PEP 246 style adaption protocol for objects that can adapt themselves to native SQLite types.

Mục đích duy nhất của PrepareProtocol loại 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 nghi với các loại SQLite gốc.

Ngoại lệ haPEP 249).

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ệ ________ 82 ________ 363¶

Ngoại lệ này được tăng lên 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)
""")
0 nếu truy vấn SQL không phải là
cur.execute("CREATE TABLE movie(title, year, score)")
60 hoặc nếu nhiều câu lệnh được chuyển đến
cur.execute("CREATE TABLE movie(title, year, score)")
12 hoặc
cur.execute("CREATE TABLE movie(title, year, score)")
94.
>>> res = cur.execute("SELECT name FROM sqlite_master")
>>> res.fetchone()
('movie',)
89 là một lớp con của
>>> res = cur.execute("SELECT name FROM sqlite_master WHERE name='spam'")
>>> res.fetchone() is None
True
69.

Ngoại lệ ________ 82 ________ 371¶

Lớp cơ sở của các trường hợp 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
>>> res = cur.execute("SELECT name FROM sqlite_master WHERE name='spam'")
>>> res.fetchone() is None
True
72.
>>> res = cur.execute("SELECT name FROM sqlite_master WHERE name='spam'")
>>> res.fetchone() is None
True
73 là một lớp con của
>>> res = cur.execute("SELECT name FROM sqlite_master WHERE name='spam'")
>>> res.fetchone() is None
True
69.

Ngoại lệ ________ 82 ________ 376¶

Ngoại lệ ________ 82 ________ 382¶

Ngoại lệ được nêu 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ơ sở cho một số loại lỗi cơ sở dữ liệu. Nó chỉ được nuôi dưỡng ngầm thông qua các lớp con chuyên ngành.

>>> res = cur.execute("SELECT name FROM sqlite_master WHERE name='spam'")
>>> res.fetchone() is None
True
83 là một lớp con của
>>> res = cur.execute("SELECT name FROM sqlite_master WHERE name='spam'")
>>> res.fetchone() is None
True
73.

Ngoại lệ ________ 82 ________ 386¶

Ngoại lệ được nêu ra cho các lỗi gây ra bởi các vấn đề với dữ liệu được xử lý, như các giá trị số ngoài phạm vi và các chuỗi quá dài.

>>> res = cur.execute("SELECT name FROM sqlite_master WHERE name='spam'")
>>> res.fetchone() is None
True
87 là một lớp con của
>>> res = cur.execute("SELECT name FROM sqlite_master WHERE name='spam'")
>>> res.fetchone() is None
True
83.

Ngoại lệ ________ 82 ________ 390¶

Ngoại lệ được nêu ra cho các lỗi có liên quan đến hoạt động của cơ sở dữ liệu và không nhất thiết phải dưới sự kiểm soát của lập trình viên. Ví dụ: đường dẫn cơ sở dữ liệu không được tìm thấy hoặc không thể xử lý giao dịch.

>>> res = cur.execute("SELECT name FROM sqlite_master")
>>> res.fetchone()
('movie',)
24 là một lớp con của
>>> res = cur.execute("SELECT name FROM sqlite_master WHERE name='spam'")
>>> res.fetchone() is None
True
83.

Ngoại lệ ________ 82 ________ 394¶

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

>>> res = cur.execute("SELECT name FROM sqlite_master WHERE name='spam'")
>>> res.fetchone() is None
True
83.

Ngoại lệ ________ 82 ________ 397¶

Ngoại lệ được nêu ra khi SQLite gặp phải lỗi nội bộ. Nếu điều này được nâng lên, nó có thể chỉ ra rằng có một vấn đề với thư viện sqlite thời gian chạy.

>>> res = cur.execute("SELECT name FROM sqlite_master WHERE name='spam'")
>>> res.fetchone() is None
True
98 là một lớp con của
>>> res = cur.execute("SELECT name FROM sqlite_master WHERE name='spam'")
>>> res.fetchone() is None
True
83.

Ngoại lệ ________ 82 ________ 401¶

Ngoại lệ được nêu ra cho các lỗi lập trình 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)
""")
0, ví dụ như cố gắng hoạt động 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)
""")
6 đã đóng hoặc cố gắng thực thi các câu lệnh không DML với
cur.execute("CREATE TABLE movie(title, year, score)")
94.
>>> res = cur.execute("SELECT name FROM sqlite_master WHERE name='spam'")
>>> res.fetchone() is None
True
16 là một lớp con của
>>> res = cur.execute("SELECT name FROM sqlite_master WHERE name='spam'")
>>> res.fetchone() is None
True
83.

Ngoại lệ ________ 82 ________ 408¶

Ngoại lệ được nêu 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 cơ bản. Ví dụ: cài đặt xác định thành

>>> sqlite3.complete_statement("SELECT foo FROM bar;")
True
>>> sqlite3.complete_statement("SELECT foo")
False
7 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)
""")
10, nếu thư viện SQLite cơ bản không hỗ trợ các chức năng 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)
""")
11 là một lớp con của
>>> res = cur.execute("SELECT name FROM sqlite_master WHERE name='spam'")
>>> res.fetchone() is None
True
83.

Các loại sqlite và python

SQLite tự nhiên hỗ trợ các loại sau:

cur.execute("CREATE TABLE movie(title, year, score)")
45,
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,
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,
>>> res = cur.execute("SELECT name FROM sqlite_master")
>>> res.fetchone()
('movie',)
74,
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.

Do đó, các loại Python sau đây có thể được gửi đến SQLite mà không có bất kỳ vấn đề nào:

Loại Python

Loại sqlite

data = [
    ("Monty Python Live at the Hollywood Bowl", 1982, 7.9),
    ("Monty Python's The Meaning of Life", 1983, 7.5),
    ("Monty Python's Life of Brian", 1979, 8.0),
]
cur.executemany("INSERT INTO movie VALUES(?, ?, ?)", data)
con.commit()  # Remember to commit the transaction after executing INSERT.
2

cur.execute("CREATE TABLE movie(title, year, score)")
45

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

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

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

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

>>> sqlite3.complete_statement("SELECT foo FROM bar;")
True
>>> sqlite3.complete_statement("SELECT foo")
False
0

>>> res = cur.execute("SELECT name FROM sqlite_master")
>>> res.fetchone()
('movie',)
74

cur.execute("CREATE TABLE movie(title, year, score)")
22

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

Đâ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 Python

cur.execute("CREATE TABLE movie(title, year, score)")
45

data = [
    ("Monty Python Live at the Hollywood Bowl", 1982, 7.9),
    ("Monty Python's The Meaning of Life", 1983, 7.5),
    ("Monty Python's Life of Brian", 1979, 8.0),
]
cur.executemany("INSERT INTO movie VALUES(?, ?, ?)", data)
con.commit()  # Remember to commit the transaction after executing INSERT.
2

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

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

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

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

>>> res = cur.execute("SELECT name FROM sqlite_master")
>>> res.fetchone()
('movie',)
74

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

cur.execute("CREATE TABLE movie(title, year, score)")
22

Đây là cách các loại SQLite được chuyển đổi thành các loại Python theo mặc định:object adapters, and you can let the

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

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) """) 35, >>> sqlite3.complete_statement("SELECT foo FROM bar;") True >>> sqlite3.complete_statement("SELECT foo") False 0 theo mặc định

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 thông qua các 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 loại Python thông qua bộ chuyển đổi.

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

Có bộ điều hợp mặc định cho các loại ngày và DateTime trong mô -đun DateTime. Chúng sẽ được gửi dưới dạng dấu thời gian ISO/ISO đến sqlite.

Các bộ chuyển đổi mặc định được đăng ký dưới tên là Ngày 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)
""")
41 và dưới tên là Tim TimeStamp, 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)
""")
42.

>>> res = cur.execute("SELECT name FROM sqlite_master")
>>> res.fetchone()
('movie',)
7

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ỳ sự lo lắng nào trong hầu hết các trường hợp. Định dạng của các bộ điều hợp cũng tương thích với các hàm ngày/thời gian SQLite thử nghiệm.

Ví dụ sau đây chứng minh điều này.

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

Ghi chú

Bộ chuyển đổi thời gian thời gian mặc định bỏ qua các độ lệch UTC trong cơ sở dữ liệu và luôn trả về một đối tượng cur.execute(""" INSERT INTO movie VALUES ('Monty Python and the Holy Grail', 1975, 8.2), ('And Now for Something Completely Different', 1971, 7.5) """) 42 ngây thơ. Để bảo tồn độ lệch của UTC trong dấu thời gian, rời khỏi bộ chuyển đổi bị vô hiệu hóa hoặc đăng ký bộ chuyển đổi nhận biết bù với >>> con.close() >>> new_con = sqlite3.connect("tutorial.db") >>> new_cur = new_con.cursor() >>> res = new_cur.execute("SELECT title, year FROM movie ORDER BY score DESC") >>> title, year = res.fetchone() >>> print(f'The highest scoring Monty Python movie is {title!r}, released in {year}') The highest scoring Monty Python movie is 'Monty Python and the Holy Grail', released in 1975 5.

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

>>> res = cur.execute("SELECT name FROM sqlite_master")
>>> res.fetchone()
('movie',)
8

Cách sử dụng trình giữ chỗ để liên kết các giá trị trong các truy vấn SQLsequence. For the named style, it can be either a sequence or

>>> res = cur.execute("SELECT name FROM sqlite_master")
>>> res.fetchone()
('movie',)
87 instance. The length of the sequence must match the number of placeholders, or a
>>> res = cur.execute("SELECT name FROM sqlite_master WHERE name='spam'")
>>> res.fetchone() is None
True
16 is raised. If a
>>> res = cur.execute("SELECT name FROM sqlite_master")
>>> res.fetchone()
('movie',)
87 is given, it must contain keys for all named parameters. Any extra items are ignored. Here’s an example of both styles:

>>> res = cur.execute("SELECT name FROM sqlite_master")
>>> res.fetchone()
('movie',)
9

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

SQLite chỉ hỗ trợ một tập hợp các loại dữ liệu giới hạn. Để lưu trữ các loại Python tùy chỉnh trong cơ sở dữ liệu SQLite, hãy điều chỉnh chúng với một trong những loại Python SQLite tự hiểu.Python types SQLite natively understands.

Có hai cách để điều chỉnh các đối tượng Python với các loại SQLite: Để đối tượng của bạn tự thích nghi hoặc sử dụng bộ chuyển đổi có thể gọi được. Sau này sẽ được ưu tiên hơn cái trước. Đối với một thư viện xuất một loại tùy chỉnh, có thể có ý nghĩa để kích hoạt loại đó để tự thích nghi. Là một nhà phát triển ứng dụng, có thể có ý nghĩa hơn khi kiểm soát trực tiếp bằng cách đăng ký các chức năng bộ điều hợp tùy chỉnh.

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

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

cur.execute("""
    INSERT INTO movie VALUES
        ('Monty Python and the Holy Grail', 1975, 8.2),
        ('And Now for Something Completely Different', 1971, 7.5)
""")
51 đại diện cho một cặp tọa độ,
cur.execute("""
    INSERT INTO movie VALUES
        ('Monty Python and the Holy Grail', 1975, 8.2),
        ('And Now for Something Completely Different', 1971, 7.5)
""")
52 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)
""")
53, trong một hệ tọa độ Cartesian. Cặp tọa độ sẽ được lưu trữ dưới dạng chuỗi văn bản trong cơ sở dữ liệu, sử dụng dấu chấm phẩy để tách tọa độ. Điều này có thể được thực hiện bằng cách thêm phương thức
cur.execute("""
    INSERT INTO movie VALUES
        ('Monty Python and the Holy Grail', 1975, 8.2),
        ('And Now for Something Completely Different', 1971, 7.5)
""")
54 trả về giá trị thích nghi. Đối tượng được chuyển đến giao thức sẽ thuộc loại
cur.execute("""
    INSERT INTO movie VALUES
        ('Monty Python and the Holy Grail', 1975, 8.2),
        ('And Now for Something Completely Different', 1971, 7.5)
""")
55.

>>> res = cur.execute("SELECT name FROM sqlite_master WHERE name='spam'")
>>> res.fetchone() is None
True
0

Cách đăng ký Bộ điều hợp Callables¶

Khả năng khác là tạo một hàm chuyển đổi đối tượng Python thành loại tương thích SQLite. Hàm này sau đó có thể được đăng ký 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)
""")
56.

>>> res = cur.execute("SELECT name FROM sqlite_master WHERE name='spam'")
>>> res.fetchone() is None
True
1

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

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

Hãy để Lừa quay trở lại 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)
""")
51. Chúng tôi đã lưu trữ các tọa độ X và Y được phân tách qua các dấu chấm phẩy dưới dạng chuỗi trong sqlite.

Đầu tiên, chúng tôi sẽ xác định một hàm bộ chuyển đổi chấp nhận chuỗi là một 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)
""")
51 từ nó.

Ghi chú

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

cur.execute("CREATE TABLE movie(title, year, score)")
22, bất kể kiểu dữ liệu SQLite cơ bản.always passed a
cur.execute("CREATE TABLE movie(title, year, score)")
22 object, no matter the underlying SQLite data type.

>>> res = cur.execute("SELECT name FROM sqlite_master WHERE name='spam'")
>>> res.fetchone() is None
True
2

Bây giờ chúng ta cần nói

cur.execute("""
    INSERT INTO movie VALUES
        ('Monty Python and the Holy Grail', 1975, 8.2),
        ('And Now for Something Completely Different', 1971, 7.5)
""")
0 khi nó sẽ chuyển đổi một giá trị sqlite đã cho. Đ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 là
cur.execute("CREATE TABLE movie(title, year, score)")
23. Có ba tùy chọn:

  • Insplicit: Đặt Detect_Types thành

    >>> con.close()
    >>> new_con = sqlite3.connect("tutorial.db")
    >>> new_cur = new_con.cursor()
    >>> res = new_cur.execute("SELECT title, year FROM movie ORDER BY score DESC")
    >>> title, year = res.fetchone()
    >>> print(f'The highest scoring Monty Python movie is {title!r}, released in {year}')
    The highest scoring Monty Python movie is 'Monty Python and the Holy Grail', released in 1975
    
    7

  • Rõ ràng: Đặt phát hiện_types thành

    >>> con.close()
    >>> new_con = sqlite3.connect("tutorial.db")
    >>> new_cur = new_con.cursor()
    >>> res = new_cur.execute("SELECT title, year FROM movie ORDER BY score DESC")
    >>> title, year = res.fetchone()
    >>> print(f'The highest scoring Monty Python movie is {title!r}, released in {year}')
    The highest scoring Monty Python movie is 'Monty Python and the Holy Grail', released in 1975
    
    8

  • Cả hai: Đặt phát hiện_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)
    """)
    
    64. Tên cột được ưu tiên hơn các loại được khai báo.

Ví dụ sau đây minh họa các cách tiếp cận ngầm và rõ ràng:

>>> res = cur.execute("SELECT name FROM sqlite_master WHERE name='spam'")
>>> res.fetchone() is None
True
3

Bộ điều hợp và công thức chuyển đổi công thức

Phần này cho thấy công thức nấu ăn cho bộ điều hợp và bộ chuyển đổi chung.

>>> res = cur.execute("SELECT name FROM sqlite_master WHERE name='spam'")
>>> res.fetchone() is None
True
4

Cách sử dụng các phương thức tắt kết nối

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

cur.execute("CREATE TABLE movie(title, year, score)")
12,
cur.execute("CREATE TABLE movie(title, year, score)")
94 và
cur.execute("CREATE TABLE movie(title, year, score)")
97 của lớp
cur.execute("""
    INSERT INTO movie VALUES
        ('Monty Python and the Holy Grail', 1975, 8.2),
        ('And Now for Something Completely Different', 1971, 7.5)
""")
6, mã của bạn có thể được viết chính xác hơn vì bạn không phải tạo ra các đối tượng (thường là thừa)
cur.execute("""
    INSERT INTO movie VALUES
        ('Monty Python and the Holy Grail', 1975, 8.2),
        ('And Now for Something Completely Different', 1971, 7.5)
""")
9. Thay vào đó, các đối tượng
cur.execute("""
    INSERT INTO movie VALUES
        ('Monty Python and the Holy Grail', 1975, 8.2),
        ('And Now for Something Completely Different', 1971, 7.5)
""")
9 được tạo ngầm và các phương thức phím tắt này trả về các đối tượng con trỏ. Bằng cách này, bạn có thể thực thi câu lệnh
data = [
    ("Monty Python Live at the Hollywood Bowl", 1982, 7.9),
    ("Monty Python's The Meaning of Life", 1983, 7.5),
    ("Monty Python's Life of Brian", 1979, 8.0),
]
cur.executemany("INSERT INTO movie VALUES(?, ?, ?)", data)
con.commit()  # Remember to commit the transaction after executing INSERT.
7 và lặp lại trực tiếp bằng một cuộc gọi trên đối tượng
cur.execute("""
    INSERT INTO movie VALUES
        ('Monty Python and the Holy Grail', 1975, 8.2),
        ('And Now for Something Completely Different', 1971, 7.5)
""")
6.

>>> res = cur.execute("SELECT name FROM sqlite_master WHERE name='spam'")
>>> res.fetchone() is None
True
5

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

Một đối tượng

cur.execute("""
    INSERT INTO movie VALUES
        ('Monty Python and the Holy Grail', 1975, 8.2),
        ('And Now for Something Completely Different', 1971, 7.5)
""")
6 có thể được sử dụng như một trình quản lý ngữ cảnh tự động cam kết hoặc quay lại các giao dịch mở khi rời khỏi phần thân của trình quản lý bối cảnh. Nếu phần thân của câu lệnh
cur.execute("""
    INSERT INTO movie VALUES
        ('Monty Python and the Holy Grail', 1975, 8.2),
        ('And Now for Something Completely Different', 1971, 7.5)
""")
74 kết thúc mà không có ngoại lệ, giao dịch sẽ được thực hiện. Nếu cam kết này thất bại, hoặc nếu phần thân của tuyên bố
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 sẽ tăng một ngoại lệ không bị bắt, giao dịch sẽ được quay lại.

Nếu không có giao dịch mở khi rời khỏi phần thân của tuyên bố

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, Trình quản lý bối cảnh là không có.

Ghi chú

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

cur.execute("CREATE TABLE movie(title, year, score)")
22, bất kể kiểu dữ liệu SQLite cơ bản.

>>> res = cur.execute("SELECT name FROM sqlite_master WHERE name='spam'")
>>> res.fetchone() is None
True
6

Bây giờ chúng ta cần nói cur.execute(""" INSERT INTO movie VALUES ('Monty Python and the Holy Grail', 1975, 8.2), ('And Now for Something Completely Different', 1971, 7.5) """) 0 khi nó sẽ chuyển đổi một giá trị sqlite đã cho. Đ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 là cur.execute("CREATE TABLE movie(title, year, score)") 23. Có ba tùy chọn:

Insplicit: Đặt Detect_Types thành

>>> con.close()
>>> new_con = sqlite3.connect("tutorial.db")
>>> new_cur = new_con.cursor()
>>> res = new_cur.execute("SELECT title, year FROM movie ORDER BY score DESC")
>>> title, year = res.fetchone()
>>> print(f'The highest scoring Monty Python movie is {title!r}, released in {year}')
The highest scoring Monty Python movie is 'Monty Python and the Holy Grail', released in 1975
7

  • Rõ ràng: Đặt phát hiện_types thành

    >>> con.close()
    >>> new_con = sqlite3.connect("tutorial.db")
    >>> new_cur = new_con.cursor()
    >>> res = new_cur.execute("SELECT title, year FROM movie ORDER BY score DESC")
    >>> title, year = res.fetchone()
    >>> print(f'The highest scoring Monty Python movie is {title!r}, released in {year}')
    The highest scoring Monty Python movie is 'Monty Python and the Holy Grail', released in 1975
    
    8

>>> res = cur.execute("SELECT name FROM sqlite_master WHERE name='spam'")
>>> res.fetchone() is None
True
7

  • Cả hai: Đặt phát hiện_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)
    """)
    
    64. Tên cột được ưu tiên hơn các loại được khai báo.

>>> res = cur.execute("SELECT name FROM sqlite_master WHERE name='spam'")
>>> res.fetchone() is None
True
8

  • Ví dụ sau đây minh họa các cách tiếp cận ngầm và rõ ràng:

>>> res = cur.execute("SELECT name FROM sqlite_master WHERE name='spam'")
>>> res.fetchone() is None
True
9

Bộ điều hợp và công thức chuyển đổi công thức

Phần này cho thấy công thức nấu ăn cho bộ điều hợp và bộ chuyển đổi chung.

Cách sử dụng các phương thức tắt kết nối

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

cur.execute("CREATE TABLE movie(title, year, score)")
12,
cur.execute("CREATE TABLE movie(title, year, score)")
94 và
cur.execute("CREATE TABLE movie(title, year, score)")
97 của lớp
cur.execute("""
    INSERT INTO movie VALUES
        ('Monty Python and the Holy Grail', 1975, 8.2),
        ('And Now for Something Completely Different', 1971, 7.5)
""")
6, mã của bạn có thể được viết chính xác hơn vì bạn không phải tạo ra các đối tượng (thường là thừa)
cur.execute("""
    INSERT INTO movie VALUES
        ('Monty Python and the Holy Grail', 1975, 8.2),
        ('And Now for Something Completely Different', 1971, 7.5)
""")
9. Thay vào đó, các đối tượng
cur.execute("""
    INSERT INTO movie VALUES
        ('Monty Python and the Holy Grail', 1975, 8.2),
        ('And Now for Something Completely Different', 1971, 7.5)
""")
9 được tạo ngầm và các phương thức phím tắt này trả về các đối tượng con trỏ. Bằng cách này, bạn có thể thực thi câu lệnh
data = [
    ("Monty Python Live at the Hollywood Bowl", 1982, 7.9),
    ("Monty Python's The Meaning of Life", 1983, 7.5),
    ("Monty Python's Life of Brian", 1979, 8.0),
]
cur.executemany("INSERT INTO movie VALUES(?, ?, ?)", data)
con.commit()  # Remember to commit the transaction after executing INSERT.
7 và lặp lại trực tiếp bằng một cuộc gọi trên đối tượng
cur.execute("""
    INSERT INTO movie VALUES
        ('Monty Python and the Holy Grail', 1975, 8.2),
        ('And Now for Something Completely Different', 1971, 7.5)
""")
6.PEP 249.

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

Nếu

>>> sqlite3.complete_statement("SELECT foo FROM bar;")
True
>>> sqlite3.complete_statement("SELECT foo")
False
2 được đặt thành
data = [
    ("Monty Python Live at the Hollywood Bowl", 1982, 7.9),
    ("Monty Python's The Meaning of Life", 1983, 7.5),
    ("Monty Python's Life of Brian", 1979, 8.0),
]
cur.executemany("INSERT INTO movie VALUES(?, ?, ?)", data)
con.commit()  # Remember to commit the transaction after executing INSERT.
2, không có giao dịch nào được mở hoàn toàn. Điều này để lại thư viện SQLite cơ bản trong chế độ AutoCommit, nhưng cũng cho phép người dùng thực hiện xử lý giao dịch của riêng họ bằng cách sử dụng các câu lệnh SQL rõ ràng. Chế độ AutoCommit 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)
""")
94.

Phương thức

cur.execute("CREATE TABLE movie(title, year, score)")
97 hoàn toàn thực hiện bất kỳ giao dịch đang chờ xử lý nào trước khi thực hiện tập lệnh SQL đã cho, bất kể giá trị của
>>> sqlite3.complete_statement("SELECT foo FROM bar;")
True
>>> sqlite3.complete_statement("SELECT foo")
False
2.

Đã thay đổi trong phiên bản 3.6:

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 sử dụng để ngầm thực hiện một giao dịch mở trước các câu lệnh DDL. Đây không còn là trườ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)
""")
0 used to implicitly commit an open transaction before DDL statements. This is no longer the case.

Làm thế nào đọc dữ liệu từ sqlite3 trong python?

SQLite Python: Truy vấn dữ liệu đầu tiên, thiết lập kết nối với cơ sở dữ liệu SQLite bằng cách tạo đối tượng kết nối. Tiếp theo, tạo một đối tượng con trỏ bằng phương thức con trỏ của đối tượng kết nối. Sau đó, thực hiện một câu lệnh CHỌN. Sau đó, hãy gọi phương thức fetchall () của đối tượng con trỏ để tìm nạp dữ liệu.establish a connection to the SQLite database by creating a Connection object. Next, create a Cursor object using the cursor method of the Connection object. Then, execute a SELECT statement. After that, call the fetchall() method of the cursor object to fetch the data.

Làm cách nào để chạy sqlite trong python?

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

Cơ sở dữ liệu SQLite hoạt động như thế nào?

SQLite hoạt động bằng cách biên dịch văn bản SQL thành mã byte, sau đó chạy mã byte đó bằng máy ảo.SQLite3_Prepare_v2 () và các giao diện liên quan hoạt động như một trình biên dịch để chuyển đổi văn bản SQL thành mã byte.Đối tượng SQLITE3_STMT là một thùng chứa cho một chương trình mã byte duy nhất thực hiện một câu lệnh SQL duy nhất.compiling SQL text into bytecode, then running that bytecode using a virtual machine. The sqlite3_prepare_v2() and related interfaces act as a compiler for converting SQL text into bytecode. The sqlite3_stmt object is a container for a single bytecode program that implements a single SQL statement.

SQLite có tốt cho Python không?

SQLite3 có thể được tích hợp với Python bằng mô -đun SQLite3, được viết bởi Gerhard Haring.Nó cung cấp một giao diện SQL tuân thủ đặc tả DB-API 2.0 được mô tả bởi PEP 249. Bạn không cần phải cài đặt mô-đun này một cách riêng biệt vì nó được vận chuyển theo mặc định cùng với Python phiên bản 2.5., which was written by Gerhard Haring. It provides an SQL interface compliant with the DB-API 2.0 specification described by PEP 249. You do not need to install this module separately because it is shipped by default along with Python version 2.5.