Trong bài viết này, chúng ta đã thấy rằng chúng ta có thể sử dụng các truy vấn SQL để thao tác trên DataFrames dễ dàng như thế nào. Điều này cho chúng ta một cơ hội duy nhất. Vũ khí này có thể là một vũ khí mạnh trong kho vũ khí của bất kỳ Nhà khoa học dữ liệu nào, người biết SQL và Python, cả hai
Cả hai đều là ngôn ngữ mạnh mẽ và có điểm mạnh và điểm yếu tương ứng. Sử dụng phương pháp được hiển thị trong bài viết này, hay nói cách khác, sử dụng thư việ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] """]74 và hà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] """]75, chúng ta có thể sử dụng phương pháp tốt nhất và hiệu quả nhất để thao tác dữ liệu, ngay trong môi trường python và thậm chí cả Jupyter Notebook. This is music to my ears. I hope you enjoyed 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] """]76 too 🤓
In this article, you saw how to use SQL queries inside python. But if you want to connect the two most powerful workhorses of the Data Science world, SQL and Python. This is not the end, but only the first step towards getting the “Best of Both Worlds”
End Notes
Now you can start using Python to work upon your data which rests in SQL Databases. In able to connect to your SQL databases, go thru my article How to Access & Use SQL Database with pyodbc in Python. Once you brought it as DataFrame, then all the operations are usual Pandas operations or SQL queries being operated on Pandas DataFrame as you saw in this article
Apart from the function of SQL shown in this article, many other popular SQL functions are easily implementable in Python. Read 15 Pandas functions to replicate basic SQL Queries in Python for learning how to do that
The implied learning in this article was, that you can use Python to do things that you thought were only possible using SQL. There may or may not be straight forward solution to things, but if you are inclined to find it, there are enough resources at your disposal to find a way out. You can look at the mix and match the learning from my book, PYTHON MADE EASY – Step by Step Guide to Programming and Data Analysis using Python for Beginners and Intermediate Level
About the Author. I am Nilabh Nishchhal. I like making seemingly difficult topics easy and write about them. Check out more at https. //www. authornilabh. com/. My attempt to make Python easy and Accessible to all is Python Made Easy
Cover Photo Credit. Photo by Norbert Hentges on Unsplash
The media shown in this article are not owned by Analytics Vidhya and are used at the Author’s discretion
SQLite is a C library that provides a lightweight disk-based database that doesn’t require a separate server process and allows accessing the database using a nonstandard variant of the SQL query language. Some applications can use SQLite for internal data storage. It’s also possible to prototype an application using SQLite and then port the code to a larger database such as PostgreSQL or Oracle
The
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]0 module was written by Gerhard Häring. It provides an SQL interface compliant with the DB-API 2. 0 specification described by PEP 249, and requires SQLite 3. 7. 15 or newer
This document includes four main sections
Tutorial teaches how to use the
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]
0 module.Reference describes the classes and functions this module defines.
How-to guides details how to handle specific tasks.
Explanation provides in-depth background on transaction control.
See also
https. //www. sqlite. orgThe SQLite web page; the documentation describes the syntax and the available data types for the supported SQL dialect
https. //www. w3schools. com/sql/Tutorial, reference and examples for learning SQL syntax
PEP 249 - Database API Specification 2. 0PEP written by Marc-André Lemburg
Hướng dẫn¶
Trong hướng dẫn này, bạn sẽ tạo cơ sở dữ liệu phim Monty Python bằng cách sử dụng chức năng
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]0 cơ bản. Nó giả định một sự hiểu biết cơ bản về các khái niệm cơ sở dữ liệu, bao gồm con trỏ và giao dịch
Đầu tiên, chúng ta cần tạo một cơ sở dữ liệu mới và mở một kết nối cơ sở dữ liệu để cho phép
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]0 làm việc với nó. Gọi
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]5 để tạo kết nối đến cơ sở dữ liệu
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]6 trong thư mục làm việc hiện tại, tạo ngầm nếu chưa tồn tại
import sqlite3 con = sqlite3.connect["tutorial.db"]
Đối tượng
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]7 được trả về
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]8 đại diện cho kết nối với cơ sở dữ liệu trên đĩa
Để thực thi các câu lệnh SQL và tìm nạp kết quả từ các truy vấn SQL, chúng ta sẽ cần sử dụng một con trỏ cơ sở dữ liệu. Gọi
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]9 để tạo
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]00
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]1
Bây giờ chúng ta đã có kết nối cơ sở dữ liệu và con trỏ, chúng ta có thể tạo bảng cơ sở dữ liệu
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]01 với các cột cho tiêu đề, năm phát hành và điểm đánh giá. Để đơn giản, chúng ta chỉ cần sử dụng tên cột trong khai báo bảng – nhờ tính năng gõ linh hoạt của SQLite, việc chỉ định kiểu dữ liệu là tùy chọn. Thực hiện câu lệnh
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]02 bằng cách gọi
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]03
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]5
Chúng tôi có thể xác minh rằng bảng mới đã được tạo bằng cách truy vấn bảng
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]04 được tích hợp sẵn trong SQLite, hiện sẽ chứa một mục nhập cho định nghĩa bảng
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]01 [xem Bảng lược đồ để biết chi tiết]. Thực hiện truy vấn đó bằng cách gọi
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]03, gán kết quả cho
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]07 và gọi
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]08 để lấy hàng kết quả
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]1
Chúng ta có thể thấy rằng bảng đã được tạo, khi truy vấn trả về một
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]09 chứa tên của bảng. Nếu chúng tôi truy vấn
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]04 cho một bảng không tồn tại
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]41, thì
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]08 sẽ trả về
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]43
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]7
Bây giờ, hãy thêm hai hàng dữ liệu được cung cấp dưới dạng ký tự SQL bằng cách thực hiện câu lệnh
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]44, một lần nữa bằng cách gọi
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]03
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]
Câu lệnh
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]44 ngầm mở một giao dịch, giao dịch này cần được cam kết trước khi các thay đổi được lưu vào cơ sở dữ liệu [xem Kiểm soát giao dịch để biết chi tiết]. Gọi
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]47 trên đối tượng kết nối để thực hiện giao dịch.
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]0
Chúng tôi có thể xác minh rằng dữ liệu đã được chèn chính xác bằng cách thực hiện truy vấn
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]48. Sử dụng
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]03 hiện đã quen thuộc để gán kết quả cho
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]07 và gọi
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]71 để trả về tất cả các hàng kết quả
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]4
Kết quả là một
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]72 trong số hai
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]09, mỗi cái một hàng, mỗi cái chứa giá trị
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]74 của hàng đó
Bây giờ, chèn thêm ba hàng bằng cách gọi
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]75
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]7
Lưu ý rằng trình giữ chỗ
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]76 được sử dụng để liên kết
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]77 với truy vấn. Luôn sử dụng trình giữ chỗ thay vì định dạng chuỗi để liên kết các giá trị Python với câu lệnh SQL, nhằm tránh các cuộc tấn công SQL injection [xem Cách thức . for more details].
Chúng tôi có thể xác minh rằng các hàng mới đã được chèn bằng cách thực hiện truy vấn
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]48, lần này lặp lại kết quả của truy vấn
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]7
Mỗi hàng là một
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]09 hai mục của
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]70, khớp với các cột được chọn trong truy vấn
Cuối cùng, xác minh rằng cơ sở dữ liệu đã được ghi vào đĩa bằng cách gọi
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]71 để đóng kết nối hiện có, mở một kết nối mới, tạo con trỏ mới, sau đó truy vấn cơ sở dữ liệu
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]10
Bây giờ bạn đã tạo cơ sở dữ liệu SQLite bằng mô-đun
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]0, đã chèn dữ liệu và truy xuất giá trị từ cơ sở dữ liệu đó theo nhiều cách
See also
Hướng dẫn cách thực hiện để đọc thêm.
Cách sử dụng trình giữ chỗ để liên kết các giá trị trong truy vấn SQL
Cách điều chỉnh các loại Python tùy chỉnh thành các giá trị SQLite
Cách chuyển đổi các giá trị SQLite thành các loại Python tùy chỉnh
Cách sử dụng trình quản lý bối cảnh kết nối
Cách tạo và sử dụng row factory
Giải thích để biết thông tin cơ bản chuyên sâu về kiểm soát giao dịch.
Thẩm quyền giải quyết¶
Chức năng mô-đun¶
sqlite3. kết nối[cơ sở dữ liệu , thời gian chờ . 0=5.0 , detect_types=0 . Kết nối isolation_level='DEFERRED', check_same_thread=True, factory=sqlite3.Connection , cached_statements=128, uri=False]¶Mở kết nối tới cơ sở dữ liệu SQLite
Thông sốcơ sở dữ liệu [ đối tượng dạng đường dẫn ] – Đường dẫn đến tệp cơ sở dữ liệu sẽ được mở. Vượt qua
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]
73 để mở kết nối tới cơ sở dữ liệu trong RAM thay vì trên đĩa.thời gian chờ [float] – Kết nối sẽ đợi bao nhiêu giây trước khi đưa ra một ngoại lệ, nếu cơ sở dữ liệu bị khóa bởi một kết nối khác. Nếu một kết nối khác mở một giao dịch để sửa đổi cơ sở dữ liệu, nó sẽ bị khóa cho đến khi giao dịch đó được thực hiện. Mặc định năm giây
detect_types [int] – Kiểm soát xem và bằng cách nào các loại dữ liệu không được SQLite hỗ trợ được tra cứu để chuyển đổi thành các loại Python, bằng cách sử dụng . Đặt nó thành bất kỳ kết hợp nào [sử dụng
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]
75, bitwise hoặc] củacur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]
76 vàcur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]
77 để kích hoạt tính năng này. Tên cột được ưu tiên hơn các loại đã khai báo nếu cả hai cờ được đặt. Không thể phát hiện các loại cho các trường đã tạo [ví dụ:cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]
78], ngay cả khi tham số detect_types được đặt; . Theo mặc định [cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]
100], tính năng phát hiện loại bị tắt.isolation_level [str. Không có] –
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]
101 của kết nối, kiểm soát liệu các giao dịch có được mở hoàn toàn hay không và bằng cách nào. Có thể làcur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]
102 [mặc định],cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]
103 hoặccur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]
104; . Xem Kiểm soát giao dịch để biết thêm.check_same_thread [bool] – Nếu
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]
106 [mặc định], chỉ thread tạo mới có thể sử dụng kết nối. Nếucur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]
107, kết nối có thể được chia sẻ trên nhiều luồng;nhà máy [Kết nối] – Một lớp con tùy chỉnh của
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]
7 để tạo kết nối với, nếu không phải là lớpcur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]
7 mặc địnhcached_statements [int] – Số câu lệnh mà
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]
0 sẽ lưu vào bộ nhớ đệm nội bộ cho kết nối này, để tránh phân tích cú pháp chi phí. Theo mặc định, 128 câu lệnhuri [bool] – Nếu được đặt thành
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]
106, cơ sở dữ liệu được hiểu là URI với đường dẫn tệp và chuỗi truy vấn tùy chọn. Phần lược đồ phải làcur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]
112 và đường dẫn có thể là tương đối hoặc tuyệt đối. Chuỗi truy vấn cho phép chuyển tham số sang SQLite, cho phép Cách làm việc với URI SQLite khác nhau.
Sự liên quan
Tăng sự kiện kiểm tra
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]113 với đối số
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]114.
Tăng sự kiện kiểm tra
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]115 với đối số
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]116.
Mới trong phiên bản 3. 4. Tham số uri.
Đã thay đổi trong phiên bản 3. 7. cơ sở dữ liệu giờ đây cũng có thể là một đối tượng dạng đường dẫn , không chỉ là một chuỗi.
Mới trong phiên bản 3. 10. Sự kiện kiểm toán
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]115. sqlite3. complete_statement[câu lệnh] ¶
Trả lại
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]106 nếu câu lệnh chuỗi dường như chứa một hoặc nhiều câu lệnh SQL hoàn chỉnh. Không có xác minh cú pháp hoặc phân tích cú pháp dưới bất kỳ hình thức nào được thực hiện, ngoài việc kiểm tra để đảm bảo rằng không có chuỗi ký tự không được đóng dấu và câu lệnh được kết thúc bằng dấu chấm phẩy
Ví dụ
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]11
Chức năng này có thể hữu ích trong quá trình nhập dòng lệnh để xác định xem văn bản đã nhập có phải là một câu lệnh SQL hoàn chỉnh hay không hoặc nếu cần nhập thêm trước khi gọi
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]119sqlite3. enable_callback_tracebacks[flag , /]¶
Bật hoặc tắt theo dõi cuộc gọi lại. Theo mặc định, bạn sẽ không nhận được bất kỳ dấu vết nào trong các chức năng do người dùng xác định, tổng hợp, trình chuyển đổi, lệnh gọi lại của người ủy quyền, v.v. Nếu bạn muốn gỡ lỗi chúng, bạn có thể gọi chức năng này với cờ được đặt thành
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]106. Sau đó, bạn sẽ nhận được dấu vết từ các cuộc gọi lại trên
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]121. Sử dụng
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]107 để tắt lại tính năng này
Đăng ký một
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]123 để có trải nghiệm gỡ lỗi được cải thiện
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]12sqlite3. register_adapter[loại , bộ chuyển đổi, /]¶
Đăng ký bộ điều hợp có thể gọi được để điều chỉnh loại Python thành loại SQLite. Bộ điều hợp được gọi với một đối tượng Python thuộc loại làm đối số duy nhất của nó và phải trả về giá trị của loại mà SQLite vốn hiểu .
sqlite3. register_converter[typename , converter, /]¶Đăng ký trình chuyển đổi có thể gọi được để chuyển đổi các đối tượng SQLite thuộc loại tên thành một đối tượng Python thuộc một loại cụ thể. Trình chuyển đổi được gọi cho tất cả các giá trị SQLite của kiểu tên; . Tham khảo tham số detect_types của
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]125 để biết thông tin về cách hoạt động của tính năng phát hiện loại
Ghi chú. tên loại và tên của loại trong truy vấn của bạn được đối sánh không phân biệt chữ hoa chữ thường
Hằng số mô-đun¶
sqlite3. PARSE_COLNAMES ¶Chuyển giá trị cờ này cho tham số detect_types của
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]125 để tra cứu hàm chuyển đổi bằng cách sử dụng tên loại, được phân tích cú pháp từ tên cột truy vấn, làm khóa từ điển chuyển đổi. Tên loại phải được đặt trong dấu ngoặc vuông [______1127]
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]13
Cờ này có thể được kết hợp với
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]76 bằng cách sử dụng toán tử
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]75 [theo bit hoặc]sqlite3. PARSE_DECLTYPES ¶
Chuyển giá trị cờ này cho tham số detect_types của
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]125 để tra cứu hàm chuyển đổi bằng cách sử dụng các loại đã khai báo cho mỗi cột. Các loại được khai báo khi bảng cơ sở dữ liệu được tạo.
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]0 sẽ tra cứu hàm chuyển đổi bằng cách sử dụng từ đầu tiên của loại được khai báo làm khóa từ điển chuyển đổi. Ví dụ
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]14
Cờ này có thể được kết hợp với
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]77 bằng cách sử dụng toán tử
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]75 [theo bit hoặc]sqlite3. SQLITE_OK ¶ sqlite3. SQLITE_DENY ¶ sqlite3. SQLITE_IGNORE ¶
Các cờ sẽ được trả về bởi khả năng gọi lại ủy quyền_callback được chuyển đến
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]134, để cho biết liệu
Truy cập được cho phép [
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]
135],Câu lệnh SQL sẽ bị hủy bỏ khi có lỗi [
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]
136]Cột phải được coi là giá trị
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]
137 [cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]
138]
Hằng số chuỗi cho biết mức DB-API được hỗ trợ. Yêu cầu bởi DB-API. Mã hóa cứng thành
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]139sqlite3. paramstyle ¶
Hằng số chuỗi cho biết loại định dạng đánh dấu tham số mà mô-đun
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]0 mong đợi. Yêu cầu bởi DB-API. Mã hóa cứng thành
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]141
Ghi chú
Mô-đun
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]0 hỗ trợ các kiểu tham số DB-API
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]143,
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]144 và
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]145, bởi vì đó là những gì mà thư viện SQLite bên dưới hỗ trợ. Tuy nhiên, DB-API không cho phép nhiều giá trị cho thuộc tính
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]146sqlite3. sqlite_version ¶
Số phiên bản của thư viện SQLite thời gian chạy dưới dạng
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]147sqlite3. sqlite_version_info ¶
Số phiên bản của thư viện SQLite thời gian chạy là
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]09 của
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]149sqlite3. an toàn luồng ¶
Hằng số nguyên theo yêu cầu của DB-API 2. 0, cho biết mức độ an toàn của luồng mà mô-đun
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]0 hỗ trợ. Thuộc tính này được đặt dựa trên chế độ phân luồng mặc định mà thư viện SQLite bên dưới được biên dịch với. Các chế độ luồng SQLite là
đơn luồng. Trong chế độ này, tất cả các mutex đều bị vô hiệu hóa và SQLite không an toàn khi sử dụng trong nhiều luồng đơn lẻ cùng một lúc
đa luồng. Trong chế độ này, SQLite có thể được sử dụng an toàn bởi nhiều luồng với điều kiện là không có kết nối cơ sở dữ liệu đơn lẻ nào được sử dụng đồng thời trong hai hoặc nhiều luồng
nối tiếp. Ở chế độ tuần tự hóa, SQLite có thể được nhiều luồng sử dụng một cách an toàn mà không bị hạn chế
Ánh xạ từ các chế độ luồng SQLite sang DB-API 2. 0 mức độ an toàn luồng như sau
Chế độ luồng SQLite
chủ đề an toàn
SQLITE_THREADSAFE
DB-API 2. 0 ý nghĩa
đơn luồng
0
0
Chủ đề có thể không chia sẻ mô-đun
đa luồng
1
2
Chủ đề có thể chia sẻ mô-đun, nhưng không kết nối
nối tiếp
3
1
Chủ đề có thể chia sẻ mô-đun, kết nối và con trỏ
Đã thay đổi trong phiên bản 3. 11. Đặt động luồng an toàn thay vì mã hóa cứng thành
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]151. sqlite3. phiên bản ¶
Số phiên bản của mô-đun này là
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]147. Đây không phải là phiên bản của thư viện SQLitesqlite3. version_info ¶
Số phiên bản của mô-đun này là
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]09 của
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]149. Đây không phải là phiên bản của thư viện SQLite
Đối tượng kết nối¶
lớp sqlite3. Kết nối ¶Mỗi cơ sở dữ liệu SQLite mở được đại diện bởi một đối tượng
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]7, đối tượng này được tạo bằng cách sử dụng
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]5. Mục đích chính của chúng là tạo ra các đối tượng
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]00 và Kiểm soát giao dịch .
See also
Cách sử dụng các phương pháp phím tắt kết nối
Cách sử dụng trình quản lý bối cảnh kết nối
Kết nối cơ sở dữ liệu SQLite có các thuộc tính và phương thức sau
con trỏ[nhà máy=Con trỏ]¶Tạo và trả về một đối tượng
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]00. Phương thức con trỏ chấp nhận một nhà máy tham số tùy chọn duy nhất. Nếu được cung cấp, đây phải là một phiên bản có thể gọi được trả về một phiên bản của
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]00 hoặc các lớp con của nóblobopen[bảng , cột, row, /, *, readonly=False, name='main']¶
Mở tay cầm
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]160 cho BLOB hiện cóThông số
table [str] – Tên của bảng chứa blob
cột [str] – Tên của cột chứa đốm màu
row [str] – Tên của hàng chứa đốm màu
chỉ đọc [bool] – Đặt thành
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]
106 nếu blob được mở mà không có quyền ghi. Mặc định làcur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]
107name [str] – Tên của cơ sở dữ liệu chứa blob. Mặc định là
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]
163
OperationalError - Khi cố gắng mở một đốm màu trong bảng
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]164loại trả lại
Bãi
Ghi chú
Không thể thay đổi kích thước đốm màu bằng lớp
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]160. Sử dụng hàm SQL
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]166 để tạo đốm màu có kích thước cố định
Mới trong phiên bản 3. 11
cam kết[] ¶Cam kết mọi giao dịch đang chờ xử lý vào cơ sở dữ liệu. Nếu không có giao dịch mở, phương pháp này là không hoạt động
rollback[] ¶Quay lại điểm bắt đầu của bất kỳ giao dịch đang chờ xử lý nào. Nếu không có giao dịch mở, phương pháp này là không hoạt động
đóng[] ¶Đóng kết nối cơ sở dữ liệu. Bất kỳ giao dịch đang chờ xử lý nào đều không được cam kết ngầm;
thực thi[sql , tham số=[], /]¶Tạo một đối tượng
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]00 mới và gọi
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]119 trên đó với sql và tham số đã cho. Trả về đối tượng con trỏ mớiexecutemany[sql , tham số, /]¶
Tạo một đối tượng
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]00 mới và gọi
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]171 trên đó với sql và tham số đã cho. Trả về đối tượng con trỏ mớiexecutescript[sql_script , /]¶
Tạo một đối tượng
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]00 mới và gọi
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]173 trên đó với sql_script đã cho. Trả về đối tượng con trỏ mớicreate_function[tên , narg, func, *, deterministic=False]¶
Tạo hoặc xóa hàm SQL do người dùng định nghĩa
Thông sốname [str] – Tên của hàm SQL
narg [int] – Số lượng đối số mà hàm SQL có thể chấp nhận. Nếu
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]
174, nó có thể nhận bất kỳ số đối số nàofunc [ gọi lại . Không có] – Có thể gọi được gọi khi hàm SQL được gọi. Có thể gọi được phải trả về một loại vốn được hỗ trợ bởi SQLite . Đặt thành
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]
43 để xóa hàm SQL hiện có.tất định [bool] – Nếu
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]
106, hàm SQL đã tạo được đánh dấu là tất định, cho phép SQLite thực hiện các tối ưu hóa bổ sung
NotSupportedError - Nếu xác định được sử dụng với các phiên bản SQLite cũ hơn 3. 8. 3
Mới trong phiên bản 3. 8. Tham số xác định.
Ví dụ
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]15create_aggregate[tên , /, n_arg, aggregate_class]¶
Tạo hoặc xóa hàm tổng hợp SQL do người dùng xác định
Thông sốname [str] – Tên của hàm tổng hợp SQL
n_arg [int] – Số lượng đối số mà hàm tổng hợp SQL có thể chấp nhận. Nếu
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]
174, nó có thể nhận bất kỳ số đối số nàolớp_tổng hợp [ lớp . Không có] –
Một lớp phải thực hiện các phương pháp sau
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]
178. Thêm một hàng vào tổng hợp____1179. Trả về kết quả cuối cùng của tổng hợp dưới dạng một loại vốn được SQLite hỗ trợ .
Số lượng đối số mà phương thức
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]
178 phải chấp nhận được kiểm soát bởi n_argĐặt thành
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]
43 để xóa hàm tổng hợp SQL hiện có
Ví dụ
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]16create_window_function[tên , num_params, aggregate_class, /]¶
Tạo hoặc xóa chức năng cửa sổ tổng hợp do người dùng xác định
Thông sốname [str] – Tên của hàm cửa sổ tổng hợp SQL để tạo hoặc xóa
num_params [int] – Số lượng đối số mà hàm cửa sổ tổng hợp SQL có thể chấp nhận. Nếu
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]
174, nó có thể nhận bất kỳ số đối số nàolớp_tổng hợp [ lớp . Không có] –
Một lớp phải thực hiện các phương thức sau
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]
178. Thêm một hàng vào cửa sổ hiện tạicur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]
184. Trả về giá trị hiện tại của tổng hợpcur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]
185. Xóa một hàng khỏi cửa sổ hiện tại____1179. Trả về kết quả cuối cùng của tổng hợp dưới dạng một loại vốn được SQLite hỗ trợ .
Số lượng đối số mà các phương thức
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]
178 vàcur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]
184 phải chấp nhận được kiểm soát bởi num_paramsĐặt thành
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]
43 để xóa chức năng cửa sổ tổng hợp SQL hiện có
NotSupportedError – Nếu được sử dụng với phiên bản SQLite cũ hơn 3. 25. 0, không hỗ trợ các chức năng cửa sổ tổng hợp
Mới trong phiên bản 3. 11
Ví dụ
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]17create_collation[tên , có thể gọi]¶
Tạo một đối chiếu có tên name bằng chức năng đối chiếu có thể gọi được. có thể gọi được thông qua hai đối số
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]147 và nó sẽ trả về một
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]191
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]
151 nếu cái đầu tiên được đặt cao hơn cái thứ haicur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]
174 nếu cái đầu tiên được đặt thấp hơn cái thứ haicur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]
100 nếu chúng được sắp xếp bằng nhau
Ví dụ sau đây cho thấy một đối chiếu sắp xếp ngược lại
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]18
Xóa chức năng đối chiếu bằng cách đặt có thể gọi thành
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]43
Đã thay đổi trong phiên bản 3. 11. Tên đối chiếu có thể chứa bất kỳ ký tự Unicode nào. Trước đó, chỉ các ký tự ASCII được phép.
gián đoạn[] ¶Gọi phương thức này từ một luồng khác để hủy bỏ mọi truy vấn có thể đang thực thi trên kết nối. Các truy vấn bị hủy bỏ sẽ đưa ra một ngoại lệ
set_authorizer[authorizer_callback] ¶Đăng ký ủy quyền có thể gọi được để được gọi cho mỗi lần cố gắng truy cập một cột của bảng trong cơ sở dữ liệu. Cuộc gọi lại phải trả về một trong số
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]135,
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]136 hoặc
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]138 để báo hiệu cách thư viện SQLite bên dưới xử lý quyền truy cập vào cột
Đối số đầu tiên cho cuộc gọi lại biểu thị loại hoạt động nào sẽ được ủy quyền. Đối số thứ hai và thứ ba sẽ là đối số hoặc
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]43 tùy thuộc vào đối số thứ nhất. Đối số thứ 4 là tên của cơ sở dữ liệu [“main”, “temp”, v.v. ] nếu có. Đối số thứ 5 là tên của trình kích hoạt hoặc chế độ xem trong cùng chịu trách nhiệm cho nỗ lực truy cập hoặc
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]43 nếu nỗ lực truy cập này trực tiếp từ mã SQL đầu vào
Vui lòng tham khảo tài liệu SQLite về các giá trị có thể có cho đối số thứ nhất và ý nghĩa của đối số thứ hai và thứ ba tùy thuộc vào đối số thứ nhất. Tất cả các hằng số cần thiết đều có sẵn trong mô-đun
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]0
Chuyển
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]43 dưới dạng ủy quyền_callback sẽ vô hiệu hóa trình ủy quyền
Đã thay đổi trong phiên bản 3. 11. Đã thêm hỗ trợ để tắt trình ủy quyền bằng cách sử dụng
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]43. set_progress_handler[progress_handler , n]¶
Đăng ký process_handler có thể gọi để được gọi cho mỗi n hướng dẫn của máy ảo SQLite. Điều này hữu ích nếu bạn muốn được gọi từ SQLite trong các hoạt động chạy dài, chẳng hạn như để cập nhật GUI
Nếu bạn muốn xóa bất kỳ trình xử lý tiến trình nào đã cài đặt trước đó, hãy gọi phương thức bằng
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]43 cho process_handler
Trả về một giá trị khác 0 từ hàm xử lý sẽ chấm dứt truy vấn hiện đang thực hiện và khiến truy vấn tăng ngoại lệ
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]505set_trace_callback[trace_callback] ¶
Đăng ký dấu vết gọi lại có thể gọi được để được gọi cho mỗi câu lệnh SQL thực sự được thực thi bởi phần phụ trợ SQLite
Đối số duy nhất được truyền cho lệnh gọi lại là câu lệnh [dưới dạng
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]79] đang được thực thi. Giá trị trả về của cuộc gọi lại bị bỏ qua. Lưu ý rằng chương trình phụ trợ không chỉ chạy các câu lệnh được truyền cho các phương thức
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]507. Các nguồn khác bao gồm quản lý giao dịch của mô-đun
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]0 và việc thực thi các trình kích hoạt được xác định trong cơ sở dữ liệu hiện tại.
Vượt qua
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]43 dưới dạng trace_callback sẽ vô hiệu hóa lệnh gọi lại theo dõi
Ghi chú
Các ngoại lệ được đưa ra trong cuộc gọi lại theo dõi không được phổ biến. Là một công cụ hỗ trợ phát triển và gỡ lỗi, hãy sử dụng
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]510 để cho phép in truy nguyên từ các ngoại lệ được đưa ra trong lệnh gọi lại theo dõi
Mới trong phiên bản 3. 3
enable_load_extension[đã bật , /]¶Cho phép công cụ SQLite tải các tiện ích mở rộng SQLite từ thư viện dùng chung nếu được bật là
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]106; . Các tiện ích mở rộng SQLite có thể xác định các chức năng mới, tổng hợp hoặc triển khai bảng ảo hoàn toàn mới. Một phần mở rộng nổi tiếng là phần mở rộng tìm kiếm toàn văn được phân phối với SQLite
Ghi chú
Mô-đun
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]0 không được xây dựng với hỗ trợ tiện ích mở rộng có thể tải theo mặc định, vì một số nền tảng [đặc biệt là macOS] có các thư viện SQLite được biên dịch mà không có tính năng này. Để nhận hỗ trợ tiện ích mở rộng có thể tải, bạn phải vượt qua tùy chọn
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]513 để định cấu hình
Tăng sự kiện kiểm tra
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]514 với các đối số
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]515,
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]516.
Mới trong phiên bản 3. 2
Đã thay đổi trong phiên bản 3. 10. Đã thêm sự kiện kiểm tra
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]514.
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]19load_extension[đường dẫn , /]¶
Tải tiện ích mở rộng SQLite từ thư viện dùng chung có tại đường dẫn. Cho phép tải tiện ích mở rộng bằng
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]518 trước khi gọi phương thức này
Tăng sự kiện kiểm tra
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]519 với các đối số
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]515,
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]521.
Mới trong phiên bản 3. 2
Đã thay đổi trong phiên bản 3. 10. Đã thêm sự kiện kiểm toán
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]519. iterdump[] ¶
Trả về một trình lặp để kết xuất cơ sở dữ liệu dưới dạng mã nguồn SQL. Hữu ích khi lưu cơ sở dữ liệu trong bộ nhớ để phục hồi sau này. Tương tự như lệnh
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]523 trong shell sqlite3.
Ví dụ
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]50
Tạo bản sao lưu của cơ sở dữ liệu SQLite
Hoạt động ngay cả khi cơ sở dữ liệu đang được truy cập bởi các máy khách khác hoặc đồng thời bởi cùng một kết nối
Thông sốđích [Kết nối] – Kết nối cơ sở dữ liệu để lưu bản sao lưu vào
pages [int] – Số trang cần sao chép cùng một lúc. Nếu bằng hoặc nhỏ hơn
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]
100, toàn bộ cơ sở dữ liệu sẽ được sao chép trong một bước. Mặc định làcur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]
174tiến trình [ gọi lại . Không có] – Nếu được đặt thành có thể gọi được, thì nó được gọi với ba đối số nguyên cho mỗi lần lặp sao lưu. trạng thái của lần lặp cuối cùng, số trang còn lại vẫn được sao chép và tổng số trang. Mặc định là
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]
43.name [str] – Tên của cơ sở dữ liệu để sao lưu.
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]
163 [mặc định] cho cơ sở dữ liệu chính,cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]
528 cho cơ sở dữ liệu tạm thời hoặc tên của cơ sở dữ liệu tùy chỉnh như được đính kèm bằng cách sử dụng câu lệnh SQLcur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]
529ngủ [float] – Số giây để ngủ giữa các lần thử liên tiếp để sao lưu các trang còn lại
Ví dụ 1, sao chép một cơ sở dữ liệu hiện có sang một cơ sở dữ liệu khác
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]51
Ví dụ 2, sao chép cơ sở dữ liệu hiện có vào một bản sao tạm thời
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]52
Mới trong phiên bản 3. 7
getlimit[danh mục , /]¶Nhận giới hạn thời gian chạy kết nối
Thông sốdanh mục [int] – Danh mục giới hạn SQLite được truy vấn
loại trả lạiint
tăngLỗi lập trình - Nếu danh mục không được thư viện SQLite bên dưới nhận dạng
Ví dụ, truy vấn độ dài tối đa của câu lệnh SQL cho
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]7
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]8 [mặc định là 1000000000]
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]53
Mới trong phiên bản 3. 11
setlimit[danh mục , giới hạn, /]¶Đặt giới hạn thời gian chạy kết nối. Nỗ lực tăng giới hạn trên giới hạn trên cứng của nó bị cắt ngắn một cách lặng lẽ đến giới hạn trên cứng. Bất kể giới hạn có bị thay đổi hay không, giá trị trước đó của giới hạn được trả về
Thông sốdanh mục [int] – Danh mục giới hạn SQLite sẽ được đặt
giới hạn [int] – Giá trị của giới hạn mới. Nếu âm, giới hạn hiện tại không thay đổi
int
tăngLỗi lập trình - Nếu danh mục không được thư viện SQLite bên dưới nhận dạng
Ví dụ, giới hạn số lượng cơ sở dữ liệu đính kèm là 1 cho
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]7
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]8 [giới hạn mặc định là 10]
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]54
Mới trong phiên bản 3. 11
xếp thứ tự[* , tên='main']¶Tuần tự hóa cơ sở dữ liệu thành một đối tượng
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]124. Đối với một tệp cơ sở dữ liệu trên đĩa thông thường, việc tuần tự hóa chỉ là một bản sao của tệp đĩa. Đối với cơ sở dữ liệu trong bộ nhớ hoặc cơ sở dữ liệu “tạm thời”, tuần tự hóa là cùng một chuỗi byte sẽ được ghi vào đĩa nếu cơ sở dữ liệu đó được sao lưu vào đĩaThông số
name [str] – Tên cơ sở dữ liệu sẽ được sắp xếp theo thứ tự. Mặc định là
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]163loại trả lại
byte
Ghi chú
Phương pháp này chỉ khả dụng nếu thư viện SQLite cơ bản có API tuần tự hóa
Mới trong phiên bản 3. 11
giải tuần tự hóa[dữ liệu , /, *, name='main']¶Giải tuần tự hóa cơ sở dữ liệu
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]536 thành một
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]7. Phương pháp này khiến kết nối cơ sở dữ liệu bị ngắt kết nối khỏi tên cơ sở dữ liệu và mở lại tên dưới dạng cơ sở dữ liệu trong bộ nhớ dựa trên tuần tự hóa có trong dữ liệuThông số
dữ liệu [byte] – Cơ sở dữ liệu tuần tự hóa
name [str] – Tên cơ sở dữ liệu để giải tuần tự hóa thành. Mặc định là
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]
163
OperationalError – Nếu kết nối cơ sở dữ liệu hiện đang tham gia vào một giao dịch đọc hoặc một hoạt động sao lưu
DatabaseError – Nếu dữ liệu không chứa cơ sở dữ liệu SQLite hợp lệ
OverflowError - Nếu
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]
539 lớn hơncur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]
540
Ghi chú
Phương pháp này chỉ khả dụng nếu thư viện SQLite bên dưới có API deserialize
Mới trong phiên bản 3. 11
in_transaction ¶Thuộc tính chỉ đọc này tương ứng với chế độ tự động ký SQLite cấp thấp
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]106 nếu một giao dịch đang hoạt động [có những thay đổi không được cam kết],
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]107 nếu không
Mới trong phiên bản 3. 2
cấp_độ cô lập ¶Thuộc tính này kiểm soát xử lý giao dịch do
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]0 thực hiện. Nếu được đặt thành
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]43, các giao dịch sẽ không bao giờ được mở hoàn toàn. Nếu được đặt thành một trong số
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]102,
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]104 hoặc
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]103, tương ứng với hành vi giao dịch SQLite cơ bản, thì quản lý giao dịch ngầm được thực hiện.
Nếu không bị ghi đè bởi tham số Isolation_level của
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]125, giá trị mặc định là
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]549, là bí danh của
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]102row_factory ¶
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]551 ban đầu cho các đối tượng
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]00 được tạo từ kết nối này. Việc gán cho thuộc tính này không ảnh hưởng đến
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]551 con trỏ hiện có thuộc kết nối này, chỉ những con trỏ mới. Là
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]43 theo mặc định, nghĩa là mỗi hàng được trả về dưới dạng
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]09
Xem Cách tạo và sử dụng các xưởng tạo hàng để biết thêm chi tiết.
text_factory ¶Một khả năng gọi được chấp nhận một tham số
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]124 và trả về một đại diện văn bản của nó. Có thể gọi được gọi cho các giá trị SQLite với kiểu dữ liệu
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]557. Theo mặc định, thuộc tính này được đặt thành
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]79. Thay vào đó, nếu bạn muốn trả lại
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]124, hãy đặt text_factory thành
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]124
Ví dụ
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]55total_changes ¶
Trả về tổng số hàng cơ sở dữ liệu đã được sửa đổi, chèn hoặc xóa kể từ khi kết nối cơ sở dữ liệu được mở
Đối tượng con trỏ¶
lớp sqlite3. Con trỏ ¶Một đối tượng
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]00 đại diện cho một con trỏ cơ sở dữ liệu được sử dụng để thực thi các câu lệnh SQL và quản lý ngữ cảnh của thao tác tìm nạp. Con trỏ được tạo bằng cách sử dụngcur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]562 hoặc bằng cách sử dụng bất kỳ phương thức phím tắt kết nối nào .Đối tượng con trỏ là trình lặp , nghĩa là nếu bạn
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]119 một truy vấncur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]48, bạn có thể chỉ cần lặp lại con trỏ để tìm nạp các hàng kết quả.cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]56
Một phiên bản
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]00 có các thuộc tính và phương thức sauthực thi[sql , tham số=[], /]¶
Thực thi câu lệnh SQL sql. Liên kết các giá trị với câu lệnh bằng cách sử dụng phần giữ chỗ ánh xạ tới trình tự hoặc
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]566 .
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]119 sẽ chỉ thực thi một câu lệnh SQL duy nhất. Nếu bạn cố gắng thực hiện nhiều hơn một câu lệnh với nó, thì nó sẽ tăng
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]568. Sử dụng
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]173 nếu bạn muốn thực thi nhiều câu lệnh SQL với một lệnh gọi
Nếu
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]101 không phải là
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]43, thì sql là một câu lệnh
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]44,
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]573,
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]574 hoặc
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]575 và không có giao dịch mở, một giao dịch được mở hoàn toàn trước khi thực hiện sqlexecutemany[sql , tham số, /]¶
Thực thi được tham số hóa Câu lệnh SQL sql đối với tất cả các trình tự tham số hoặc ánh xạ được tìm thấy trong các tham số trình tự. Cũng có thể sử dụng một iterator cung cấp các tham số thay vì một chuỗi. Sử dụng xử lý giao dịch ngầm giống như
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]119.
Ví dụ
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]57executescript[sql_script , /]¶
Thực thi các câu lệnh SQL trong sql_script. Nếu có một giao dịch đang chờ xử lý, một câu lệnh ngầm
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]577 sẽ được thực hiện trước. Không có kiểm soát giao dịch ngầm nào khác được thực hiện;
sql_script phải là một
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]147
Ví dụ
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]58tìm nạp[] ¶
Nếu
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]551 là
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]43, hãy trả về kết quả truy vấn hàng tiếp theo được đặt là
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]09. Khác, chuyển nó đến nhà máy sản xuất hàng và trả về kết quả của nó. Trả lại
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]43 nếu không có thêm dữ liệufetchmany[size=con trỏ. kích thước mảng] ¶
Trả về nhóm hàng tiếp theo của kết quả truy vấn dưới dạng
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]72. Trả về danh sách trống nếu không còn hàng nào nữa
Số lượng hàng cần tìm nạp cho mỗi cuộc gọi được chỉ định bởi tham số kích thước. Nếu kích thước không được cung cấp,
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]584 sẽ xác định số lượng hàng sẽ được tìm nạp. Nếu có ít hàng hơn kích thước, số hàng có sẵn sẽ được trả về
Lưu ý rằng có những cân nhắc về hiệu suất liên quan đến tham số kích thước. Để có hiệu suất tối ưu, tốt nhất nên sử dụng thuộc tính arraysize. Nếu tham số kích thước được sử dụng, thì tốt nhất là giữ nguyên giá trị từ cuộc gọi
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]585 này sang cuộc gọi tiếp theotìm nạp[] ¶
Trả về tất cả các hàng [còn lại] của kết quả truy vấn dưới dạng
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]72. Trả về danh sách trống nếu không có hàng nào. Lưu ý rằng thuộc tính
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]584 có thể ảnh hưởng đến hiệu suất của thao tác nàyđóng[] ¶
Đóng con trỏ ngay bây giờ [chứ không phải bất cứ khi nào
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]588 được gọi]
Con trỏ sẽ không sử dụng được từ thời điểm này trở đi;
setinputsizes[kích thước , /]¶Yêu cầu bởi DB-API. Không làm gì trong
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]0setoutputsize[kích thước , cột=None, /]¶
Yêu cầu bởi DB-API. Không làm gì trong
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]0kích thước mảng ¶
Thuộc tính đọc/ghi kiểm soát số hàng được trả về bởi
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]585. Giá trị mặc định là 1 có nghĩa là một hàng sẽ được tìm nạp cho mỗi cuộc gọikết nối ¶
Thuộc tính chỉ đọc cung cấp cơ sở dữ liệu SQLite
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]7 thuộc về con trỏ. Đối tượng
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]00 được tạo bằng cách gọi
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]9 sẽ có thuộc tính
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]515 đề cập đến con
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]59mô tả ¶
Thuộc tính chỉ đọc cung cấp tên cột của truy vấn cuối cùng. Để duy trì khả năng tương thích với API Python DB, nó trả về 7-bộ cho mỗi cột trong đó sáu mục cuối cùng của mỗi bộ là
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]43
Nó cũng được đặt cho các câu lệnh
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]48 mà không có bất kỳ hàng nào phù hợplastrowid ¶
Thuộc tính chỉ đọc cung cấp id hàng của hàng được chèn cuối cùng. Nó chỉ được cập nhật sau khi câu lệnh
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]44 hoặc
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]575 thành công sử dụng phương pháp
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]119. Đối với các câu lệnh khác, sau
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]171 hoặc
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]173 hoặc nếu chèn không thành công, giá trị của
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]104 sẽ không thay đổi. Giá trị ban đầu của
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]104 là
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]43
Ghi chú
Chèn vào bảng
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]164 không được ghi lại
Đã thay đổi trong phiên bản 3. 6. Đã thêm hỗ trợ cho câu lệnh
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]575. số hàng ¶
Thuộc tính chỉ đọc cung cấp số lượng hàng đã sửa đổi cho các câu lệnh
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]44,
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]573,
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]574 và
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]575; . Nó chỉ được cập nhật bằng các phương pháp
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]119 và
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]171row_factory ¶
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]00 object and the
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]09 of row values, and returns a custom object representing an SQLite row.
Mặc định là những gì
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]122 đã được đặt khi
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]00 được tạo. Việc gán cho thuộc tính này không ảnh hưởng đến
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]122 của kết nối chính
Xem Cách tạo và sử dụng các xưởng tạo hàng để biết thêm chi tiết.
Đối tượng hàng¶
lớp sqlite3. Hàng ¶Một phiên bản
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]125 đóng vai trò là một
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]551 được tối ưu hóa cao cho các đối tượng
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]7. Nó hỗ trợ phép lặp, kiểm tra đẳng thức, truy cập
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]128 và ánh xạ theo tên cột và chỉ mục.
Hai đối tượng
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]125 so sánh bằng nhau nếu chúng có tên và giá trị cột giống hệt nhau
Xem Cách tạo và sử dụng các xưởng tạo hàng để biết thêm chi tiết.
phím[] ¶Trả lại một
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]72 tên cột là
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]131. Ngay sau một truy vấn, nó là phần tử đầu tiên của mỗi bộ trong
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]132
Đã thay đổi trong phiên bản 3. 5. Đã thêm hỗ trợ cắt lát.
Đối tượng đốm màu¶
Mới trong phiên bản 3. 11
lớp sqlite3. Blob ¶Phiên bản
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]160 là một đối tượng dạng tệp có thể đọc và ghi dữ liệu trong SQLite BLOB. Gọi
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]134 để lấy kích thước [số byte] của đốm màu. Sử dụng các chỉ số và lát để truy cập trực tiếp vào dữ liệu blob.
Sử dụng
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]160 làm trình quản lý ngữ cảnh để đảm bảo rằng tay cầm blob được đóng lại sau khi sử dụng.
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]10đóng[] ¶
Đóng đốm màu
Blob sẽ không sử dụng được từ thời điểm này trở đi. Một ngoại lệ
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]136 [hoặc phân lớp] sẽ được đưa ra nếu có bất kỳ thao tác nào tiếp theo được thực hiện với blobđọc[độ dài=- 1, /]¶
Đọc các byte dữ liệu có độ dài từ đốm màu ở vị trí bù hiện tại. Nếu đạt đến cuối đốm màu, dữ liệu lên tới EOF sẽ được trả về. Khi độ dài không được chỉ định hoặc là số âm,
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]137 sẽ đọc cho đến hết đốm màughi[dữ liệu , /]¶
Ghi dữ liệu vào đốm màu ở phần bù hiện tại. Chức năng này không thể thay đổi độ dài blob. Viết vượt quá phần cuối của đốm màu sẽ tăng
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]138kể[] ¶
Trả về vị trí truy cập hiện tại của blob
tìm kiếm[bù , gốc=os.SEEK_SET , /] ¶Đặt vị trí truy cập hiện tại của blob thành offset. Đối số gốc mặc định là
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]139 [định vị đốm màu tuyệt đối]. Các giá trị khác cho nguồn gốc là
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]140 [tìm kiếm liên quan đến vị trí hiện tại] và
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]141 [tìm kiếm liên quan đến phần cuối của blob]
Chuẩn bị đối tượngProtocol¶
lớp sqlite3. Chuẩn bị giao thức ¶Mục đích duy nhất của loại PrepareProtocol là hoạt động như một giao thức thích ứng kiểu PEP 246 cho các đối tượng có thể tự thích ứng để native SQLite types.
Ngoại lệ¶
Hệ thống phân cấp ngoại lệ được xác định bởi DB-API 2. 0 [PEP 249]
ngoại lệ sqlite3. Cảnh báo ¶Ngoại lệ này hiện không được mô-đun
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]0 đưa ra, nhưng có thể được đưa ra bởi các ứng dụng sử dụng
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]0, ví dụ: nếu hàm do người dùng xác định cắt bớt dữ liệu trong khi chèn.
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]144 là một lớp con của
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]145
Lớp cơ sở của các ngoại lệ khác trong mô-đun này. Sử dụng điều này để bắt tất cả các lỗi với một câu lệnh
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]146 duy nhất.
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]136 is a subclass of
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]145
If the exception originated from within the SQLite library, the following two attributes are added to the exception
sqlite_errorcode ¶The numeric error code from the SQLite API
Mới trong phiên bản 3. 11
sqlite_errorname ¶Tên tượng trưng của mã lỗi số từ API SQLite
Mới trong phiên bản 3. 11
exception sqlite3. InterfaceError ¶Ngoại lệ được đưa ra do lạm dụng API SQLite C cấp thấp. In other words, if this exception is raised, it probably indicates a bug in the
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]0 module.
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]150 là một lớp con của
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]136ngoại lệ sqlite3. DatabaseError ¶
Exception raised for errors that are related to the database. This serves as the base exception for several types of database errors. It is only raised implicitly through the specialised subclasses.
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]152 is a subclass of
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]136exception sqlite3. Lỗi dữ liệu ¶
Exception raised for errors caused by problems with the processed data, like numeric values out of range, and strings which are too long.
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]154 là một lớp con của
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]152exception sqlite3. OperationalError ¶
Exception raised for errors that are related to the database’s operation, and not necessarily under the control of the programmer. For example, the database path is not found, or a transaction could not be processed.
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]505 is a subclass of
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]152exception sqlite3. IntegrityError ¶
Exception raised when the relational integrity of the database is affected, e. g. a foreign key check fails. It is a subclass of
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]152exception sqlite3. InternalError ¶
Exception raised when SQLite encounters an internal error. If this is raised, it may indicate that there is a problem with the runtime SQLite library.
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]159 is a subclass of
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]152ngoại lệ sqlite3. ProgrammingError ¶
Exception raised for
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]0 API programming errors, for example supplying the wrong number of bindings to a query, or trying to operate on a closed
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]7.
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]568 is a subclass of
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]152exception sqlite3. NotSupportedError ¶
Exception raised in case a method or database API is not supported by the underlying SQLite library. For example, setting deterministic to
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]106 in
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]166, if the underlying SQLite library does not support deterministic functions.
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]167 is a subclass of
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]152
SQLite and Python types¶
SQLite natively supports the following types.
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]137,
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]170,
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]171,
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]557,
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]173
The following Python types can thus be sent to SQLite without any problem
Python type
SQLite type
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]43
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]137
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]176
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]170
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]178
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]171
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]79
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]557
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]124
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]173
Đây là cách các loại SQLite được chuyển đổi thành các loại Python theo mặc định
SQLite type
Python type
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]137
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]43
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]170
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]176
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]171
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]178
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]557
phụ thuộc vào
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]191,
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]79 theo mặc định
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]173
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]124
Hệ thống loại của mô-đun
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]0 có thể mở rộng theo hai cách. bạn có thể lưu trữ các loại Python bổ sung trong cơ sở dữ liệu SQLite qua bộ điều hợp đối tượng và bạn có thể để mô-đun
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]0 chuyển đổi các loại SQLite thành các loại Python qua converters.
Bộ điều hợp và bộ chuyển đổi mặc định¶
Có các bộ điều hợp mặc định cho các loại ngày và giờ trong mô-đun ngày giờ. Chúng sẽ được gửi dưới dạng ngày ISO/dấu thời gian ISO tới SQLite
Bộ chuyển đổi mặc định được đăng ký dưới tên “ngày” cho
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]197 và dưới tên “dấu thời gian” cho
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]198
Bằng cách này, bạn có thể sử dụng ngày/dấu thời gian từ Python mà không cần thêm bất kỳ thao tác nào trong hầu hết các trường hợp. Định dạng của bộ điều hợp cũng tương thích với các hàm ngày/giờ SQLite thử nghiệm
Ví dụ sau minh họa điều này
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]11
Nếu dấu thời gian được lưu trữ trong SQLite có phần phân số dài hơn 6 số, thì giá trị của dấu thời gian đó sẽ bị bộ chuyển đổi dấu thời gian cắt ngắn thành độ chính xác micro giây
Ghi chú
Trình chuyển đổi “dấu thời gian” mặc định bỏ qua các giá trị bù UTC trong cơ sở dữ liệu và luôn trả về một đối tượng
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]198 ngây thơ. Để duy trì độ lệch UTC trong dấu thời gian, hãy tắt bộ chuyển đổi hoặc đăng ký bộ chuyển đổi nhận biết độ lệch với
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]74
Làm thế nào để hướng dẫn¶
Cách sử dụng trình giữ chỗ để liên kết các giá trị trong truy vấn SQL¶
Các hoạt động SQL thường cần sử dụng các giá trị từ các biến Python. Tuy nhiên, hãy cẩn thận khi sử dụng các thao tác chuỗi của Python để lắp ráp các truy vấn, vì chúng dễ bị tấn công SQL injection. Ví dụ: kẻ tấn công có thể chỉ cần đóng một trích dẫn và đưa vào
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]701 để chọn tất cả các hàng
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]12
Thay vào đó, hãy sử dụng thay thế tham số của DB-API. Để chèn một biến vào một chuỗi truy vấn, hãy sử dụng một trình giữ chỗ trong chuỗi và thay thế các giá trị thực tế vào truy vấn bằng cách cung cấp chúng dưới dạng một
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]09 giá trị cho đối số thứ hai của phương thức
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]119 của con trỏ. Một câu lệnh SQL có thể sử dụng một trong hai loại trình giữ chỗ. dấu chấm hỏi [kiểu qmark] hoặc trình giữ chỗ được đặt tên [kiểu được đặt tên]. Đối với kiểu qmark,
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]704 phải là một trình tự . Đối với kiểu được đặt tên, nó có thể là một trình tự hoặc phiên bản
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]566. Độ dài của chuỗi phải phù hợp với số lượng phần giữ chỗ, nếu không một số
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]568 sẽ tăng lên. Nếu một
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]566 được cung cấp, nó phải chứa các khóa cho tất cả các tham số được đặt tên. Bất kỳ mục bổ sung được bỏ qua. Đây là một ví dụ về cả hai phong cách.
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]13
Cách điều chỉnh các loại Python tùy chỉnh thành các giá trị SQLite¶
SQLite chỉ hỗ trợ một số loại dữ liệu hạn chế. Để lưu trữ các loại Python tùy chỉnh trong cơ sở dữ liệu SQLite, hãy điều chỉnh chúng theo một trong Các loại Python mà SQLite hiểu được .
Có hai cách để điều chỉnh các đối tượng Python thành các kiểu SQLite. để đối tượng của bạn tự điều chỉnh hoặc sử dụng bộ điều hợp có thể gọi được. Cái sau sẽ được ưu tiên hơn cái trước. Đối với một thư viện xuất một loại tùy chỉnh, có thể hợp lý khi cho phép loại đó tự điều chỉnh. Với tư cách là nhà phát triển ứng dụng, việc kiểm soát trực tiếp bằng cách đăng ký các chức năng của bộ điều hợp tùy chỉnh có thể hợp lý hơn
Cách viết các đối tượng có thể thích ứng¶
Suppose we have a
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]708 class that represents a pair of coordinates,
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]709 and
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]710, in a Cartesian coordinate system. Cặp tọa độ sẽ được lưu trữ dưới dạng chuỗi văn bản trong cơ sở dữ liệu, sử dụng dấu chấm phẩy để phân tách tọa độ. Điều này có thể được thực hiện bằng cách thêm phương thức
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]711 trả về giá trị đã điều chỉnh. The object passed to protocol will be of type
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]712
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]14
How to register adapter 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 với SQLite. This function can then be registered using
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]713
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]15
How to convert SQLite values to custom Python types¶
Writing an adapter lets you convert from custom Python types to SQLite values. To be able to convert from SQLite values to custom Python types, we use converters
Let’s go back to the
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]708 class. We stored the x and y coordinates separated via semicolons as strings in SQLite
First, we’ll define a converter function that accepts the string as a parameter and constructs 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] """]708 object from it
Ghi chú
Converter functions are always passed a
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]124 object, no matter the underlying SQLite data type
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]16
We now need to tell
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 when it should convert a given SQLite value. This is done when connecting to a database, using the detect_types parameter 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] """]125. There are three options
Implicit. set detect_types to
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]
76Explicit. set detect_types to
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]
77Both. set detect_types to
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]
721. Tên cột được ưu tiên hơn các loại đã khai báo
The following example illustrates the implicit and explicit approaches
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]17
Adapter and converter recipes¶
This section shows recipes for common adapters and converters
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]18
How to use connection shortcut methods¶
Using the
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]119,
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]171, and
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]173 methods of the
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]7 class, your code can be written more concisely because you don’t have to create the [often superfluous]
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]00 objects explicitly. Instead, the
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]00 objects are created implicitly and these shortcut methods return the cursor objects. This way, you can execute a
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]48 statement and iterate over it directly using only a single call on the
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]7 object
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]19
How to use the connection context manager¶
A
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]7 object can be used as a context manager that automatically commits or rolls back open transactions when leaving the body of the context manager. If the body of the
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]731 statement finishes without exceptions, the transaction is committed. If this commit fails, or if the body of the
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]731 statement raises an uncaught exception, the transaction is rolled back
If there is no open transaction upon leaving the body of the
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]731 statement, the context manager is a no-op
Ghi chú
The context manager neither implicitly opens a new transaction nor closes the connection
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]70
How to work with SQLite URIs¶
Some useful URI tricks include
Open a database in read-only mode
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]71
Do not implicitly create a new database file if it does not already exist; will raise
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]
505 if unable to create a new file
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]72
Create a shared named in-memory database
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]73
More information about this feature, including a list of parameters, can be found in the SQLite URI documentation
How to create and use row factories¶
By default,
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]0 represents each row as a
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]09. If a
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]09 does not suit your needs, you can use the
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]119 class or a custom
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]551
Mặc dù
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]551 tồn tại như một thuộc tính trên cả
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]00 và
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]7, nhưng bạn nên đặt
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]122, vì vậy tất cả các con trỏ được tạo từ kết nối sẽ sử dụng cùng một hàng.
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]125 provides indexed and case-insensitive named access to columns, with minimal memory overhead and performance impact over a
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]09. Để sử dụng
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]125 làm nhà sản xuất hàng, hãy gán nó cho thuộc tính
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]551
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]74
Queries now return
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]125 objects
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]75
You can create a custom
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]551 that returns each row as a
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]566, with column names mapped to values
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]76
Using it, queries now return a
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]566 instead of a
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]09
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]77
The following row factory returns a named tuple .
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]78
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]753 can be used as follows
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]79
With some adjustments, the above recipe can be adapted to use a
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]754, or any other custom class, instead of a
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]755
Explanation¶
Transaction control¶
The
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]0 module does not adhere to the transaction handling recommended by PEP 249
If the connection attribute
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]101 is not
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]43, new transactions are implicitly opened before
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]119 and
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]171 executes
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]44,
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]573,
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]574, or
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]575 statements; for other statements, no implicit transaction handling is performed. Use the
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]167 and
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]766 methods to respectively commit and roll back pending transactions. Bạn có thể chọn hành vi giao dịch SQLite cơ bản — nghĩa là, liệu và loại câu lệnh
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]767 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 có thực thi ngầm hay không — thông qua thuộc tính
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]101
If
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]101 is set to
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]43, no transactions are implicitly opened at all. This leaves the underlying SQLite library in autocommit mode, but also allows the user to perform their own transaction handling using explicit SQL statements. The underlying SQLite library autocommit mode can be queried using the
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]772 attribute
The
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]173 method implicitly commits any pending transaction before execution of the given SQL script, regardless of the value of
cur.execute[""" INSERT INTO movie VALUES ['Monty Python and the Holy Grail', 1975, 8.2], ['And Now for Something Completely Different', 1971, 7.5] """]101