Trong một bài viết trước mình đã hướng dẫn các bạn kết nối đến SQLite 3 sử dụng ngôn ngữ lập trình PHP. Ở bài viết này, mình sẽ hướng dẫn tiếp các bạn sử dụng ngôn ngữ Python để kết nối đến SQLite một cách đơn giản nhất. Show
Đầu tiên chúng ta import thư viện sqlite3 import sqlite3 Bây giờ thì thực hiện kết nối đến db, và tạo con trỏ. # Connect to DB and create a cursor sqliteConnection = sqlite3.connect('vinasupport.db') cursor = sqliteConnection.cursor() print('DB Init') Chú ý, SQLite sử dụng một file để làm database. Sau khi khởi tạo bạn đã có thể thực hiện các câu lệnh SQL VD: Lấy version của SQLite # Write a query and execute it with cursor query = 'select sqlite_version();' cursor.execute(query) # Fetch and output result result = cursor.fetchall() print('SQLite Version is {}'.format(result)) In dữ liệu và đóng con trỏ # Fetch and output result result = cursor.fetchall() print('SQLite Version is {}'.format(result)) # Close the cursor cursor.close() Cuôi cùng là đóng kết nối tới database if sqliteConnection: sqliteConnection.close() print('SQLite Connection closed') Cuối cùng, chúng ta có đoạn code như sau: import sqlite3 try: # Connect to DB and create a cursor sqliteConnection = sqlite3.connect('sql.db') cursor = sqliteConnection.cursor() print('DB Init') # Write a query and execute it with cursor query = 'select sqlite_version();' cursor.execute(query) # Fetch and output result result = cursor.fetchall() print('SQLite Version is {}'.format(result)) # Close the cursor cursor.close() # Handle errors except sqlite3.Error as error: print('Error occurred - ', error) # Close DB Connection irrespective of success # or failure finally: if sqliteConnection: sqliteConnection.close() print('SQLite Connection closed') Kết quả sau khi chạy: Mã nguồn: lib/sqlite3/ Lib/sqlite3/ SQLite là một thư viện C cung cấp cơ sở dữ liệu dựa trên đĩa nhẹ không yêu cầu một quy trình máy chủ riêng biệt và cho phép truy cập cơ sở dữ liệu bằng biến thể không đạt tiêu chuẩn của ngôn ngữ truy vấn SQL. Một số ứng dụng có thể sử dụng SQLite để lưu trữ dữ liệu nội bộ. Nó cũng có thể tạo mẫu cho một ứng dụng bằng SQLite và sau đó chuyển mã sang cơ sở dữ liệu lớn hơn như PostgreSQL hoặc Oracle. Mô -đun import sqlite3 try: # Connect to DB and create a cursor sqliteConnection = sqlite3.connect('sql.db') cursor = sqliteConnection.cursor() print('DB Init') # Write a query and execute it with cursor query = 'select sqlite_version();' cursor.execute(query) # Fetch and output result result = cursor.fetchall() print('SQLite Version is {}'.format(result)) # Close the cursor cursor.close() # Handle errors except sqlite3.Error as error: print('Error occurred - ', error) # Close DB Connection irrespective of success # or failure finally: if sqliteConnection: sqliteConnection.close() print('SQLite Connection closed')0 được viết bởi Gerhard Häring. Nó cung cấp một giao diện SQL tuân thủ đặc tả DB-API 2.0 được mô tả bởi PEP 249 và yêu cầu SQLite 3.7.15 hoặc mới hơn.PEP 249, and requires SQLite 3.7.15 or newer. Tài liệu này bao gồm bốn phần chính:
Xem thêm https://www.sqlite.orgTrang web sqlite; Tài liệu mô tả cú pháp và các loại dữ liệu có sẵn cho phương ngữ SQL được hỗ trợ. https://www.w3schools.com/sql/Hướng dẫn, tham khảo và ví dụ cho việc học cú pháp SQL. PEP 249 - Thông số kỹ thuật API cơ sở dữ liệu 2.0 - Database API Specification 2.0Pep được viết bởi Marc-André Lemburg. Hướng dẫn¶Trong hướng dẫn này, bạn sẽ tạo một cơ sở dữ liệu về các bộ phim Monty Python bằng cách sử dụng chức năng import sqlite3 try: # Connect to DB and create a cursor sqliteConnection = sqlite3.connect('sql.db') cursor = sqliteConnection.cursor() print('DB Init') # Write a query and execute it with cursor query = 'select sqlite_version();' cursor.execute(query) # Fetch and output result result = cursor.fetchall() print('SQLite Version is {}'.format(result)) # Close the cursor cursor.close() # Handle errors except sqlite3.Error as error: print('Error occurred - ', error) # Close DB Connection irrespective of success # or failure finally: if sqliteConnection: sqliteConnection.close() print('SQLite Connection closed')0 cơ bản. Nó giả định một sự hiểu biết cơ bản về các khái niệm cơ sở dữ liệu, bao gồm con trỏ và giao dịch. Đầu tiên, chúng ta cần tạo cơ sở dữ liệu mới và mở kết nối cơ sở dữ liệu để cho phép import sqlite3 try: # Connect to DB and create a cursor sqliteConnection = sqlite3.connect('sql.db') cursor = sqliteConnection.cursor() print('DB Init') # Write a query and execute it with cursor query = 'select sqlite_version();' cursor.execute(query) # Fetch and output result result = cursor.fetchall() print('SQLite Version is {}'.format(result)) # Close the cursor cursor.close() # Handle errors except sqlite3.Error as error: print('Error occurred - ', error) # Close DB Connection irrespective of success # or failure finally: if sqliteConnection: sqliteConnection.close() print('SQLite Connection closed')0 hoạt động với nó. Gọi import sqlite3 try: # Connect to DB and create a cursor sqliteConnection = sqlite3.connect('sql.db') cursor = sqliteConnection.cursor() print('DB Init') # Write a query and execute it with cursor query = 'select sqlite_version();' cursor.execute(query) # Fetch and output result result = cursor.fetchall() print('SQLite Version is {}'.format(result)) # Close the cursor cursor.close() # Handle errors except sqlite3.Error as error: print('Error occurred - ', error) # Close DB Connection irrespective of success # or failure finally: if sqliteConnection: sqliteConnection.close() print('SQLite Connection closed')4 để tạo kết nối với cơ sở dữ liệu import sqlite3 try: # Connect to DB and create a cursor sqliteConnection = sqlite3.connect('sql.db') cursor = sqliteConnection.cursor() print('DB Init') # Write a query and execute it with cursor query = 'select sqlite_version();' cursor.execute(query) # Fetch and output result result = cursor.fetchall() print('SQLite Version is {}'.format(result)) # Close the cursor cursor.close() # Handle errors except sqlite3.Error as error: print('Error occurred - ', error) # Close DB Connection irrespective of success # or failure finally: if sqliteConnection: sqliteConnection.close() print('SQLite Connection closed')5 trong thư mục làm việc hiện tại, hoàn toàn tạo ra nó nếu nó không tồn tại: import sqlite3 con = sqlite3.connect("tutorial.db") Đối tượng import sqlite3 try: # Connect to DB and create a cursor sqliteConnection = sqlite3.connect('sql.db') cursor = sqliteConnection.cursor() print('DB Init') # Write a query and execute it with cursor query = 'select sqlite_version();' cursor.execute(query) # Fetch and output result result = cursor.fetchall() print('SQLite Version is {}'.format(result)) # Close the cursor cursor.close() # Handle errors except sqlite3.Error as error: print('Error occurred - ', error) # Close DB Connection irrespective of success # or failure finally: if sqliteConnection: sqliteConnection.close() print('SQLite Connection closed')6 đã trả lại import sqlite3 try: # Connect to DB and create a cursor sqliteConnection = sqlite3.connect('sql.db') cursor = sqliteConnection.cursor() print('DB Init') # Write a query and execute it with cursor query = 'select sqlite_version();' cursor.execute(query) # Fetch and output result result = cursor.fetchall() print('SQLite Version is {}'.format(result)) # Close the cursor cursor.close() # Handle errors except sqlite3.Error as error: print('Error occurred - ', error) # Close DB Connection irrespective of success # or failure finally: if sqliteConnection: sqliteConnection.close() print('SQLite Connection closed')7 đại diện cho kết nối với cơ sở dữ liệu trên đĩa. Để thực hiện các câu lệnh SQL và tìm nạp kết quả từ các truy vấn SQL, chúng ta sẽ cần sử dụng con trỏ cơ sở dữ liệu. Gọi import sqlite3 try: # Connect to DB and create a cursor sqliteConnection = sqlite3.connect('sql.db') cursor = sqliteConnection.cursor() print('DB Init') # Write a query and execute it with cursor query = 'select sqlite_version();' cursor.execute(query) # Fetch and output result result = cursor.fetchall() print('SQLite Version is {}'.format(result)) # Close the cursor cursor.close() # Handle errors except sqlite3.Error as error: print('Error occurred - ', error) # Close DB Connection irrespective of success # or failure finally: if sqliteConnection: sqliteConnection.close() print('SQLite Connection closed')8 để tạo import sqlite3 try: # Connect to DB and create a cursor sqliteConnection = sqlite3.connect('sql.db') cursor = sqliteConnection.cursor() print('DB Init') # Write a query and execute it with cursor query = 'select sqlite_version();' cursor.execute(query) # Fetch and output result result = cursor.fetchall() print('SQLite Version is {}'.format(result)) # Close the cursor cursor.close() # Handle errors except sqlite3.Error as error: print('Error occurred - ', error) # Close DB Connection irrespective of success # or failure finally: if sqliteConnection: sqliteConnection.close() print('SQLite Connection closed')9: Bây giờ chúng tôi đã có kết nối cơ sở dữ liệu và con trỏ, chúng tôi có thể tạo bảng cơ sở dữ liệu import sqlite3 con = sqlite3.connect("tutorial.db")0 với các cột cho tiêu đề, năm phát hành và điểm đánh giá. Để đơn giản, chúng ta chỉ có thể sử dụng tên cột trong khai báo bảng - nhờ tính năng gõ linh hoạt của SQLite, chỉ định các loại dữ liệu là tùy chọn. Thực hiện câu lệnh import sqlite3 con = sqlite3.connect("tutorial.db")1 bằng cách gọi import sqlite3 con = sqlite3.connect("tutorial.db")2: cur.execute("CREATE TABLE movie(title, year, score)") Chúng tôi có thể xác minh rằng bảng mới đã được tạo bằng cách truy vấn bảng import sqlite3 con = sqlite3.connect("tutorial.db")3 tích hợp cho SQLite, giờ đây sẽ chứa một mục nhập cho định nghĩa bảng import sqlite3 con = sqlite3.connect("tutorial.db")0 (xem bảng lược đồ để biết chi tiết). Thực hiện truy vấn đó bằng cách gọi import sqlite3 con = sqlite3.connect("tutorial.db")2, gán kết quả cho import sqlite3 con = sqlite3.connect("tutorial.db")6 và gọi import sqlite3 con = sqlite3.connect("tutorial.db")7 để tìm nạp hàng kết quả: >>> res = cur.execute("SELECT name FROM sqlite_master") >>> res.fetchone() ('movie',) Chúng ta có thể thấy rằng bảng đã được tạo, khi truy vấn trả về một import sqlite3 con = sqlite3.connect("tutorial.db")8 chứa tên bảng. Nếu chúng tôi truy vấn import sqlite3 con = sqlite3.connect("tutorial.db")3 cho bảng không tồn tại cur.execute("CREATE TABLE movie(title, year, score)")0, import sqlite3 con = sqlite3.connect("tutorial.db")7 sẽ trả về cur.execute("CREATE TABLE movie(title, year, score)")2: >>> res = cur.execute("SELECT name FROM sqlite_master WHERE name='spam'") >>> res.fetchone() is None True Bây giờ, hãy thêm hai hàng dữ liệu được cung cấp dưới dạng SQL Biết bằng cách thực thi câu lệnh cur.execute("CREATE TABLE movie(title, year, score)")3, một lần nữa bằng cách gọi import sqlite3 con = sqlite3.connect("tutorial.db")2: # Connect to DB and create a cursor sqliteConnection = sqlite3.connect('vinasupport.db') cursor = sqliteConnection.cursor() print('DB Init')0 Tuyên bố cur.execute("CREATE TABLE movie(title, year, score)")3 ngầm mở một giao dịch, cần phải được cam kết trước khi các thay đổi được lưu trong cơ sở dữ liệu (xem kiểm soát giao dịch để biết chi tiết). Gọi cur.execute("CREATE TABLE movie(title, year, score)")6 trên đối tượng kết nối để thực hiện giao dịch:Transaction control for details). Call cur.execute("CREATE TABLE movie(title, year, score)")6 on the connection object to commit the transaction: Chúng tôi có thể xác minh rằng dữ liệu được chèn chính xác bằng cách thực thi truy vấn cur.execute("CREATE TABLE movie(title, year, score)")7. Sử dụng import sqlite3 con = sqlite3.connect("tutorial.db")2 quen thuộc để gán kết quả cho import sqlite3 con = sqlite3.connect("tutorial.db")6 và gọi >>> res = cur.execute("SELECT name FROM sqlite_master") >>> res.fetchone() ('movie',)0 để trả về tất cả các hàng kết quả: # Connect to DB and create a cursor sqliteConnection = sqlite3.connect('vinasupport.db') cursor = sqliteConnection.cursor() print('DB Init')1 Kết quả là >>> res = cur.execute("SELECT name FROM sqlite_master") >>> res.fetchone() ('movie',)1 của hai import sqlite3 con = sqlite3.connect("tutorial.db")8S, một mỗi hàng, mỗi hàng có chứa giá trị hàng >>> res = cur.execute("SELECT name FROM sqlite_master") >>> res.fetchone() ('movie',)3 đó. Bây giờ, chèn thêm ba hàng bằng cách gọi >>> res = cur.execute("SELECT name FROM sqlite_master") >>> res.fetchone() ('movie',)4: # Connect to DB and create a cursor sqliteConnection = sqlite3.connect('vinasupport.db') cursor = sqliteConnection.cursor() print('DB Init')2 Lưu ý rằng >>> res = cur.execute("SELECT name FROM sqlite_master") >>> res.fetchone() ('movie',)5 Người giữ chỗ được sử dụng để liên kết >>> res = cur.execute("SELECT name FROM sqlite_master") >>> res.fetchone() ('movie',)6 với truy vấn. Luôn sử dụng các trình giữ chỗ thay vì định dạng chuỗi để liên kết các giá trị python với các câu lệnh SQL, để tránh các cuộc tấn công tiêm SQL (xem cách sử dụng trình giữ chỗ để liên kết các giá trị trong các truy vấn SQL để biết thêm chi tiết).string formatting to bind Python values to SQL statements, to avoid SQL injection attacks (see How to use placeholders to bind values in SQL queries for more details). Chúng tôi có thể xác minh rằng các hàng mới đã được chèn bằng cách thực thi truy vấn cur.execute("CREATE TABLE movie(title, year, score)")7, lần này lặp lại kết quả của truy vấn: # Connect to DB and create a cursor sqliteConnection = sqlite3.connect('vinasupport.db') cursor = sqliteConnection.cursor() print('DB Init')3 Mỗi hàng là một import sqlite3 con = sqlite3.connect("tutorial.db")8 của >>> res = cur.execute("SELECT name FROM sqlite_master") >>> res.fetchone() ('movie',)9, khớp các cột được chọn trong truy vấn. Cuối cùng, xác minh rằng cơ sở dữ liệu đã được ghi vào đĩa bằng cách gọi >>> res = cur.execute("SELECT name FROM sqlite_master WHERE name='spam'") >>> res.fetchone() is None True0 để đóng kết nối hiện có, mở một cái mới, tạo một con trỏ mới, sau đó truy vấn cơ sở dữ liệu: # Connect to DB and create a cursor sqliteConnection = sqlite3.connect('vinasupport.db') cursor = sqliteConnection.cursor() print('DB Init')4 Bây giờ bạn đã tạo một cơ sở dữ liệu SQLite bằng mô -đun import sqlite3 try: # Connect to DB and create a cursor sqliteConnection = sqlite3.connect('sql.db') cursor = sqliteConnection.cursor() print('DB Init') # Write a query and execute it with cursor query = 'select sqlite_version();' cursor.execute(query) # Fetch and output result result = cursor.fetchall() print('SQLite Version is {}'.format(result)) # Close the cursor cursor.close() # Handle errors except sqlite3.Error as error: print('Error occurred - ', error) # Close DB Connection irrespective of success # or failure finally: if sqliteConnection: sqliteConnection.close() print('SQLite Connection closed')0, chèn dữ liệu và lấy các giá trị từ nó theo nhiều cách. Xem thêm
Tài liệu tham khảo¶Chức năng mô -đunsqlite3.connect (cơ sở dữ liệu, thời gian chờ = 5.0, detect_types = 0, sleam_level = 'referred', kiểm tra_same_thread = true, factory = sqlite3.connectconnect(database, timeout=5.0, detect_types=0, isolation_level='DEFERRED', check_same_thread=True, factory=sqlite3.Connection, cached_statements=128, uri=False)¶Mở kết nối với cơ sở dữ liệu SQLite. Thông số
Sự liên quan Tăng một sự kiện kiểm toán # Connect to DB and create a cursor sqliteConnection = sqlite3.connect('vinasupport.db') cursor = sqliteConnection.cursor() print('DB Init')12 với đối số # Connect to DB and create a cursor sqliteConnection = sqlite3.connect('vinasupport.db') cursor = sqliteConnection.cursor() print('DB Init')13.auditing event # Connect to DB and create a cursor sqliteConnection = sqlite3.connect('vinasupport.db') cursor = sqliteConnection.cursor() print('DB Init')12 with argument # Connect to DB and create a cursor sqliteConnection = sqlite3.connect('vinasupport.db') cursor = sqliteConnection.cursor() print('DB Init')13. Tăng một sự kiện kiểm toán # Connect to DB and create a cursor sqliteConnection = sqlite3.connect('vinasupport.db') cursor = sqliteConnection.cursor() print('DB Init')14 với đối số # Connect to DB and create a cursor sqliteConnection = sqlite3.connect('vinasupport.db') cursor = sqliteConnection.cursor() print('DB Init')15.auditing event # Connect to DB and create a cursor sqliteConnection = sqlite3.connect('vinasupport.db') cursor = sqliteConnection.cursor() print('DB Init')14 with argument # Connect to DB and create a cursor sqliteConnection = sqlite3.connect('vinasupport.db') cursor = sqliteConnection.cursor() print('DB Init')15. Mới trong phiên bản 3.4: Tham số URI.The uri parameter. Đã thay đổi trong phiên bản 3.7: Cơ sở dữ liệu giờ đây cũng có thể là một đối tượng giống như đường dẫn, không chỉ là một chuỗi.database can now also be a path-like object, not only a string. Mới trong phiên bản 3.10: Sự kiện kiểm toán # Connect to DB and create a cursor sqliteConnection = sqlite3.connect('vinasupport.db') cursor = sqliteConnection.cursor() print('DB Init')14 auditing event. sqlite3.complete_statement (câu lệnh) ¶complete_statement(statement)¶ Trả về # Connect to DB and create a cursor sqliteConnection = sqlite3.connect('vinasupport.db') cursor = sqliteConnection.cursor() print('DB Init')05 Nếu câu lệnh String dường như chứa một hoặc nhiều câu lệnh SQL hoàn chỉnh. Không có xác minh cú pháp hoặc phân tích cú pháp dưới bất kỳ hình thức nào được thực hiện, ngoài việc kiểm tra xem không có chữ viết nào không được giải thích và tuyên bố bị chấm dứt bởi một dấu chấm phẩy. Ví dụ: # Connect to DB and create a cursor sqliteConnection = sqlite3.connect('vinasupport.db') cursor = sqliteConnection.cursor() print('DB Init')5 Hàm này có thể hữu ích trong đầu vào dòng lệnh để xác định xem văn bản đã nhập dường như tạo thành một câu lệnh SQL hoàn chỉnh hay nếu cần đầu vào bổ sung trước khi gọi # Connect to DB and create a cursor sqliteConnection = sqlite3.connect('vinasupport.db') cursor = sqliteConnection.cursor() print('DB Init')18. sqlite3.enable_callback_traceBacks (cờ, /) ¶enable_callback_tracebacks(flag, /)¶ Bật hoặc vô hiệu hóa các dấu vết gọi lại. Theo mặc định, bạn sẽ không nhận được bất kỳ dấu vết nào trong các chức năng, tập hợp, bộ chuyển đổi, trình gọi ủy quyền do người dùng xác định, v.v. Nếu bạn muốn gỡ lỗi chúng, bạn có thể gọi chức năng này bằng cờ được đặt thành # Connect to DB and create a cursor sqliteConnection = sqlite3.connect('vinasupport.db') cursor = sqliteConnection.cursor() print('DB Init')05. Sau đó, bạn sẽ nhận được dấu vết từ các cuộc gọi lại trên # Connect to DB and create a cursor sqliteConnection = sqlite3.connect('vinasupport.db') cursor = sqliteConnection.cursor() print('DB Init')20. Sử dụng # Connect to DB and create a cursor sqliteConnection = sqlite3.connect('vinasupport.db') cursor = sqliteConnection.cursor() print('DB Init')06 để vô hiệu hóa lại tính năng. Đăng ký # Connect to DB and create a cursor sqliteConnection = sqlite3.connect('vinasupport.db') cursor = sqliteConnection.cursor() print('DB Init')22 để có trải nghiệm gỡ lỗi được cải thiện: # Connect to DB and create a cursor sqliteConnection = sqlite3.connect('vinasupport.db') cursor = sqliteConnection.cursor() print('DB Init')6 sqlite3.register_ad CHƯƠNG (loại, bộ điều hợp, /) ¶register_adapter(type, adapter, /)¶ Đăng ký một bộ chuyển đổi có thể gọi được để điều chỉnh loại Python thành loại SQLite. Bộ điều hợp được gọi với một đối tượng Python thuộc loại là đối số duy nhất của nó và phải trả về giá trị của một loại mà SQLite tự hiểu.type that SQLite natively understands. sqlite3.register_converter (kiểu chữ, bộ chuyển đổi, /) ¶register_converter(typename, converter, /)¶Đăng ký bộ chuyển đổi có thể gọi để chuyển đổi các đối tượng sqlite của loại tên kiểu thành một đối tượng Python thuộc loại cụ thể. Bộ chuyển đổi được gọi cho tất cả các giá trị sqlite của loại kiểu loại; Nó được truyền một đối tượng # Connect to DB and create a cursor sqliteConnection = sqlite3.connect('vinasupport.db') cursor = sqliteConnection.cursor() print('DB Init')23 và sẽ trả về một đối tượng thuộc loại python mong muốn. Tham khảo ý kiến tham số Detect_types của # Connect to DB and create a cursor sqliteConnection = sqlite3.connect('vinasupport.db') cursor = sqliteConnection.cursor() print('DB Init')24 để biết thông tin về cách phát hiện loại hoạt động. LƯU Ý: Tên kiểu và tên của loại trong truy vấn của bạn được khớp với trường hợp không nhạy cảm. Hằng số mô -đunsqlite3.parse_colnames¶PARSE_COLNAMES¶Chuyển giá trị cờ này cho tham số Detect_Types của # Connect to DB and create a cursor sqliteConnection = sqlite3.connect('vinasupport.db') cursor = sqliteConnection.cursor() print('DB Init')24 để tra cứu chức năng chuyển đổi bằng cách sử dụng tên loại, được phân tích cú pháp từ tên cột Query, làm phím từ điển chuyển đổi. Tên loại phải được bọc trong ngoặc vuông ( # Connect to DB and create a cursor sqliteConnection = sqlite3.connect('vinasupport.db') cursor = sqliteConnection.cursor() print('DB Init')26). # Connect to DB and create a cursor sqliteConnection = sqlite3.connect('vinasupport.db') cursor = sqliteConnection.cursor() print('DB Init')7 Cờ này có thể được kết hợp với >>> res = cur.execute("SELECT name FROM sqlite_master WHERE name='spam'") >>> res.fetchone() is None True5 bằng toán tử >>> res = cur.execute("SELECT name FROM sqlite_master WHERE name='spam'") >>> res.fetchone() is None True4 (bitwise hoặc). sqlite3.parse_decltypes¶PARSE_DECLTYPES¶ Chuyển giá trị cờ này cho tham số Detect_Types của # Connect to DB and create a cursor sqliteConnection = sqlite3.connect('vinasupport.db') cursor = sqliteConnection.cursor() print('DB Init')24 để tra cứu chức năng chuyển đổi bằng cách sử dụng các loại được khai báo cho mỗi cột. Các loại được khai báo khi bảng cơ sở dữ liệu được tạo. import sqlite3 try: # Connect to DB and create a cursor sqliteConnection = sqlite3.connect('sql.db') cursor = sqliteConnection.cursor() print('DB Init') # Write a query and execute it with cursor query = 'select sqlite_version();' cursor.execute(query) # Fetch and output result result = cursor.fetchall() print('SQLite Version is {}'.format(result)) # Close the cursor cursor.close() # Handle errors except sqlite3.Error as error: print('Error occurred - ', error) # Close DB Connection irrespective of success # or failure finally: if sqliteConnection: sqliteConnection.close() print('SQLite Connection closed')0 sẽ tra cứu chức năng chuyển đổi bằng cách sử dụng từ đầu tiên của loại được khai báo làm khóa từ điển chuyển đổi. Ví dụ: # Connect to DB and create a cursor sqliteConnection = sqlite3.connect('vinasupport.db') cursor = sqliteConnection.cursor() print('DB Init')8 Cờ này có thể được kết hợp với >>> res = cur.execute("SELECT name FROM sqlite_master WHERE name='spam'") >>> res.fetchone() is None True6 bằng toán tử >>> res = cur.execute("SELECT name FROM sqlite_master WHERE name='spam'") >>> res.fetchone() is None True4 (bitwise hoặc). sqlite3.sqlite_ok¶ sqlite3.sqlite_deny¶ sqlite3.sqlite_ignore¶SQLITE_OK¶ sqlite3.SQLITE_DENY¶ sqlite3.SQLITE_IGNORE¶ Những lá cờ nên được trả về bởi Authorizer_callback có thể gọi được chuyển đến # Connect to DB and create a cursor sqliteConnection = sqlite3.connect('vinasupport.db') cursor = sqliteConnection.cursor() print('DB Init')33, để cho biết liệu:
Chuỗi không đổi nêu rõ mức DB-API được hỗ trợ. Yêu cầu bởi DB-API. Mã hóa cứng đến # Connect to DB and create a cursor sqliteConnection = sqlite3.connect('vinasupport.db') cursor = sqliteConnection.cursor() print('DB Init')38. sqlite3.paramstyle¶paramstyle¶ Chuỗi hằng số nêu rõ loại định dạng đánh dấu tham số được mong đợi bởi mô -đun import sqlite3 try: # Connect to DB and create a cursor sqliteConnection = sqlite3.connect('sql.db') cursor = sqliteConnection.cursor() print('DB Init') # Write a query and execute it with cursor query = 'select sqlite_version();' cursor.execute(query) # Fetch and output result result = cursor.fetchall() print('SQLite Version is {}'.format(result)) # Close the cursor cursor.close() # Handle errors except sqlite3.Error as error: print('Error occurred - ', error) # Close DB Connection irrespective of success # or failure finally: if sqliteConnection: sqliteConnection.close() print('SQLite Connection closed')0. Yêu cầu bởi DB-API. Mã hóa cứng đến # Connect to DB and create a cursor sqliteConnection = sqlite3.connect('vinasupport.db') cursor = sqliteConnection.cursor() print('DB Init')40. Ghi chú Mô-đun import sqlite3 try: # Connect to DB and create a cursor sqliteConnection = sqlite3.connect('sql.db') cursor = sqliteConnection.cursor() print('DB Init') # Write a query and execute it with cursor query = 'select sqlite_version();' cursor.execute(query) # Fetch and output result result = cursor.fetchall() print('SQLite Version is {}'.format(result)) # Close the cursor cursor.close() # Handle errors except sqlite3.Error as error: print('Error occurred - ', error) # Close DB Connection irrespective of success # or failure finally: if sqliteConnection: sqliteConnection.close() print('SQLite Connection closed')0 hỗ trợ cả các kiểu tham số # Connect to DB and create a cursor sqliteConnection = sqlite3.connect('vinasupport.db') cursor = sqliteConnection.cursor() print('DB Init')42 và # Connect to DB and create a cursor sqliteConnection = sqlite3.connect('vinasupport.db') cursor = sqliteConnection.cursor() print('DB Init')43 DB-API, bởi vì đó là những gì thư viện SQLite cơ bản hỗ trợ. Tuy nhiên, DB-API không cho phép nhiều giá trị cho thuộc tính # Connect to DB and create a cursor sqliteConnection = sqlite3.connect('vinasupport.db') cursor = sqliteConnection.cursor() print('DB Init')44. sqlite3.sqlite_version¶sqlite_version¶ Số phiên bản của thư viện sqlite thời gian chạy dưới dạng # Connect to DB and create a cursor sqliteConnection = sqlite3.connect('vinasupport.db') cursor = sqliteConnection.cursor() print('DB Init')45. sqlite3.sqlite_version_info¶sqlite_version_info¶ Số phiên bản của thư viện sqlite thời gian chạy dưới dạng import sqlite3 con = sqlite3.connect("tutorial.db")8 của # Connect to DB and create a cursor sqliteConnection = sqlite3.connect('vinasupport.db') cursor = sqliteConnection.cursor() print('DB Init')47. sqlite3.threadsafety¶threadsafety¶ Hằng số số nguyên theo yêu cầu của DB-API 2.0, nêu rõ mức độ an toàn của luồng mà mô-đun import sqlite3 try: # Connect to DB and create a cursor sqliteConnection = sqlite3.connect('sql.db') cursor = sqliteConnection.cursor() print('DB Init') # Write a query and execute it with cursor query = 'select sqlite_version();' cursor.execute(query) # Fetch and output result result = cursor.fetchall() print('SQLite Version is {}'.format(result)) # Close the cursor cursor.close() # Handle errors except sqlite3.Error as error: print('Error occurred - ', error) # Close DB Connection irrespective of success # or failure finally: if sqliteConnection: sqliteConnection.close() print('SQLite Connection closed')0 hỗ trợ. Thuộc tính này được đặt dựa trên chế độ luồng mặc định Thư viện SQLite bên dưới được biên dịch. Các chế độ luồng sqlite là:
Các ánh xạ từ các chế độ luồng SQLite đến các cấp độ an toàn của DB-API 2.0 như sau:
Đã thay đổi trong phiên bản 3.11: Đặt ThreadSafety an toàn thay vì mã hóa cứng thành # Connect to DB and create a cursor sqliteConnection = sqlite3.connect('vinasupport.db') cursor = sqliteConnection.cursor() print('DB Init')49. sqlite3.version¶version¶ Số phiên bản của mô -đun này là # Connect to DB and create a cursor sqliteConnection = sqlite3.connect('vinasupport.db') cursor = sqliteConnection.cursor() print('DB Init')45. Đây không phải là phiên bản của thư viện SQLite. sqlite3.version_info¶version_info¶ Số phiên bản của mô -đun này là import sqlite3 con = sqlite3.connect("tutorial.db")8 của # Connect to DB and create a cursor sqliteConnection = sqlite3.connect('vinasupport.db') cursor = sqliteConnection.cursor() print('DB Init')47. Đây không phải là phiên bản của thư viện SQLite. Đối tượng kết nốiClassSQLite3.connection¶sqlite3.Connection¶Mỗi cơ sở dữ liệu SQLite mở được biểu thị bằng một đối tượng import sqlite3 try: # Connect to DB and create a cursor sqliteConnection = sqlite3.connect('sql.db') cursor = sqliteConnection.cursor() print('DB Init') # Write a query and execute it with cursor query = 'select sqlite_version();' cursor.execute(query) # Fetch and output result result = cursor.fetchall() print('SQLite Version is {}'.format(result)) # Close the cursor cursor.close() # Handle errors except sqlite3.Error as error: print('Error occurred - ', error) # Close DB Connection irrespective of success # or failure finally: if sqliteConnection: sqliteConnection.close() print('SQLite Connection closed')6, được tạo bằng import sqlite3 try: # Connect to DB and create a cursor sqliteConnection = sqlite3.connect('sql.db') cursor = sqliteConnection.cursor() print('DB Init') # Write a query and execute it with cursor query = 'select sqlite_version();' cursor.execute(query) # Fetch and output result result = cursor.fetchall() print('SQLite Version is {}'.format(result)) # Close the cursor cursor.close() # Handle errors except sqlite3.Error as error: print('Error occurred - ', error) # Close DB Connection irrespective of success # or failure finally: if sqliteConnection: sqliteConnection.close() print('SQLite Connection closed')4. Mục đích chính của họ là tạo các đối tượng import sqlite3 try: # Connect to DB and create a cursor sqliteConnection = sqlite3.connect('sql.db') cursor = sqliteConnection.cursor() print('DB Init') # Write a query and execute it with cursor query = 'select sqlite_version();' cursor.execute(query) # Fetch and output result result = cursor.fetchall() print('SQLite Version is {}'.format(result)) # Close the cursor cursor.close() # Handle errors except sqlite3.Error as error: print('Error occurred - ', error) # Close DB Connection irrespective of success # or failure finally: if sqliteConnection: sqliteConnection.close() print('SQLite Connection closed')9 và kiểm soát giao dịch.Transaction control. Kết nối cơ sở dữ liệu SQLite có các thuộc tính và phương thức sau: con trỏ (nhà máy = con trỏ) ¶(factory=Cursor)¶Tạo và trả về một đối tượng import sqlite3 try: # Connect to DB and create a cursor sqliteConnection = sqlite3.connect('sql.db') cursor = sqliteConnection.cursor() print('DB Init') # Write a query and execute it with cursor query = 'select sqlite_version();' cursor.execute(query) # Fetch and output result result = cursor.fetchall() print('SQLite Version is {}'.format(result)) # Close the cursor cursor.close() # Handle errors except sqlite3.Error as error: print('Error occurred - ', error) # Close DB Connection irrespective of success # or failure finally: if sqliteConnection: sqliteConnection.close() print('SQLite Connection closed')9. Phương thức con trỏ chấp nhận một nhà máy tham số tùy chọn duy nhất. Nếu được cung cấp, đây phải là một phiên bản có thể gọi được là một ví dụ là import sqlite3 try: # Connect to DB and create a cursor sqliteConnection = sqlite3.connect('sql.db') cursor = sqliteConnection.cursor() print('DB Init') # Write a query and execute it with cursor query = 'select sqlite_version();' cursor.execute(query) # Fetch and output result result = cursor.fetchall() print('SQLite Version is {}'.format(result)) # Close the cursor cursor.close() # Handle errors except sqlite3.Error as error: print('Error occurred - ', error) # Close DB Connection irrespective of success # or failure finally: if sqliteConnection: sqliteConnection.close() print('SQLite Connection closed')9 hoặc các lớp con của nó. blobopen (bảng, cột, hàng, /, *, readOnly = false, name = 'main') ¶(table, column, row, /, *, readonly=False, name='main')¶ Mở tay cầm # Connect to DB and create a cursor sqliteConnection = sqlite3.connect('vinasupport.db') cursor = sqliteConnection.cursor() print('DB Init')58 cho một đốm màu hiện có.Thông số
OperationalError - Khi cố gắng mở một đốm màu trong bảng # Connect to DB and create a cursor sqliteConnection = sqlite3.connect('vinasupport.db') cursor = sqliteConnection.cursor() print('DB Init')62 table.Loại trở lại Bãi Ghi chú Kích thước blob không thể được thay đổi bằng lớp # Connect to DB and create a cursor sqliteConnection = sqlite3.connect('vinasupport.db') cursor = sqliteConnection.cursor() print('DB Init')58. Sử dụng hàm SQL # Connect to DB and create a cursor sqliteConnection = sqlite3.connect('vinasupport.db') cursor = sqliteConnection.cursor() print('DB Init')64 để tạo một blob với kích thước cố định. Mới trong phiên bản 3.11. làm()¶()¶Cam kết bất kỳ giao dịch đang chờ xử lý vào cơ sở dữ liệu. Nếu không có giao dịch mở, phương pháp này là không có op. rollback () ¶()¶Quay lại để bắt đầu bất kỳ giao dịch đang chờ xử lý. Nếu không có giao dịch mở, phương pháp này là không có op. gần()¶()¶Đóng kết nối cơ sở dữ liệu. Bất kỳ giao dịch đang chờ xử lý không được cam kết ngầm; Đảm bảo # Connect to DB and create a cursor sqliteConnection = sqlite3.connect('vinasupport.db') cursor = sqliteConnection.cursor() print('DB Init')65 trước khi đóng để tránh mất những thay đổi đang chờ xử lý. thực thi (SQL, tham số = (), /) ¶(sql, parameters=(), /)¶ Tạo một đối tượng import sqlite3 try: # Connect to DB and create a cursor sqliteConnection = sqlite3.connect('sql.db') cursor = sqliteConnection.cursor() print('DB Init') # Write a query and execute it with cursor query = 'select sqlite_version();' cursor.execute(query) # Fetch and output result result = cursor.fetchall() print('SQLite Version is {}'.format(result)) # Close the cursor cursor.close() # Handle errors except sqlite3.Error as error: print('Error occurred - ', error) # Close DB Connection irrespective of success # or failure finally: if sqliteConnection: sqliteConnection.close() print('SQLite Connection closed')9 mới và gọi # Connect to DB and create a cursor sqliteConnection = sqlite3.connect('vinasupport.db') cursor = sqliteConnection.cursor() print('DB Init')18 trên nó với SQL và tham số đã cho. Trả về đối tượng con trỏ mới. EXECUTEMANY (SQL, tham số, /) ¶(sql, parameters, /)¶ Tạo một đối tượng import sqlite3 try: # Connect to DB and create a cursor sqliteConnection = sqlite3.connect('sql.db') cursor = sqliteConnection.cursor() print('DB Init') # Write a query and execute it with cursor query = 'select sqlite_version();' cursor.execute(query) # Fetch and output result result = cursor.fetchall() print('SQLite Version is {}'.format(result)) # Close the cursor cursor.close() # Handle errors except sqlite3.Error as error: print('Error occurred - ', error) # Close DB Connection irrespective of success # or failure finally: if sqliteConnection: sqliteConnection.close() print('SQLite Connection closed')9 mới và gọi # Connect to DB and create a cursor sqliteConnection = sqlite3.connect('vinasupport.db') cursor = sqliteConnection.cursor() print('DB Init')69 trên nó với SQL và tham số đã cho. Trả về đối tượng con trỏ mới. Executescript (sql_script, /) ¶(sql_script, /)¶ Tạo một đối tượng import sqlite3 try: # Connect to DB and create a cursor sqliteConnection = sqlite3.connect('sql.db') cursor = sqliteConnection.cursor() print('DB Init') # Write a query and execute it with cursor query = 'select sqlite_version();' cursor.execute(query) # Fetch and output result result = cursor.fetchall() print('SQLite Version is {}'.format(result)) # Close the cursor cursor.close() # Handle errors except sqlite3.Error as error: print('Error occurred - ', error) # Close DB Connection irrespective of success # or failure finally: if sqliteConnection: sqliteConnection.close() print('SQLite Connection closed')9 mới và gọi # Connect to DB and create a cursor sqliteConnection = sqlite3.connect('vinasupport.db') cursor = sqliteConnection.cursor() print('DB Init')71 trên đó với SQL_Script đã cho. Trả về đối tượng con trỏ mới. created_function (tên, narg, func, *, xác định = sai) ¶(name, narg, func, *, deterministic=False)¶ Tạo hoặc xóa chức năng SQL do người dùng xác định. Thông số
NotSupportedError - Nếu xác định được sử dụng với các phiên bản SQLite cũ hơn 3,8.3. – If deterministic is used with SQLite versions older than 3.8.3. Mới trong phiên bản 3.8: Tham số xác định.The deterministic parameter. Example: # Connect to DB and create a cursor sqliteConnection = sqlite3.connect('vinasupport.db') cursor = sqliteConnection.cursor() print('DB Init')9 created_aggregate (tên, /, n_arg, tổng hợp_class) ¶(name, /, n_arg, aggregate_class)¶ Tạo hoặc xóa chức năng tổng hợp SQL do người dùng xác định. Thông số
Example: # Write a query and execute it with cursor query = 'select sqlite_version();' cursor.execute(query) # Fetch and output result result = cursor.fetchall() print('SQLite Version is {}'.format(result))0 created_window_function (tên, num_params, tổng hợp_class, /) ¶(name, 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ố
created_window_function (tên, num_params, tổng hợp_class, /) ¶ – If used with a version of SQLite older than 3.25.0, which does not support aggregate window functions. 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. Tên (str) - Tên của hàm cửa sổ tổng hợp SQL để tạo hoặc xóa. # Write a query and execute it with cursor query = 'select sqlite_version();' cursor.execute(query) # Fetch and output result result = cursor.fetchall() print('SQLite Version is {}'.format(result))1 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 # Connect to DB and create a cursor sqliteConnection = sqlite3.connect('vinasupport.db') cursor = sqliteConnection.cursor() print('DB Init')72, nó có thể lấy bất kỳ số lượng đối số nào.(name, callable)¶ Một lớp phải thực hiện các phương pháp sau:
Số lượng đối số mà các phương thức # Connect to DB and create a cursor sqliteConnection = sqlite3.connect('vinasupport.db') cursor = sqliteConnection.cursor() print('DB Init')76 và # Connect to DB and create a cursor sqliteConnection = sqlite3.connect('vinasupport.db') cursor = sqliteConnection.cursor() print('DB Init')82 phải chấp nhận được kiểm soát bởi num_params. # Write a query and execute it with cursor query = 'select sqlite_version();' cursor.execute(query) # Fetch and output result result = cursor.fetchall() print('SQLite Version is {}'.format(result))2 Đặt thành cur.execute("CREATE TABLE movie(title, year, score)")2 để 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 hàm cửa sổ tổng hợp.The collation name can contain any Unicode character. Earlier, only ASCII characters were allowed. Mới trong phiên bản 3.11.()¶Thí dụ: created_collation (tên, có thể gọi) ¶ Tạo tên đối chiếu tên bằng cách sử dụng hàm đối chiếu có thể gọi được. Có thể gọi được được thông qua hai đối số # Connect to DB and create a cursor sqliteConnection = sqlite3.connect('vinasupport.db') cursor = sqliteConnection.cursor() print('DB Init')45 và nó sẽ trả về một # Connect to DB and create a cursor sqliteConnection = sqlite3.connect('vinasupport.db') cursor = sqliteConnection.cursor() print('DB Init')89: # Connect to DB and create a cursor sqliteConnection = sqlite3.connect('vinasupport.db') cursor = sqliteConnection.cursor() print('DB Init')49 nếu thứ nhất được đặt hàng cao hơn thứ hai # Connect to DB and create a cursor sqliteConnection = sqlite3.connect('vinasupport.db') cursor = sqliteConnection.cursor() print('DB Init')72 Nếu thứ nhất được đặt hàng thấp hơn thứ hai
cur.execute("CREATE TABLE movie(title, year, score)")2. Ví dụ sau đây cho thấy một đối chiếu phân loại ngược:(progress_handler, n)¶ Xóa chức năng đối chiếu bằng cách đặt cuộc gọi có thể gọi thành cur.execute("CREATE TABLE movie(title, year, score)")2. Đã 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 phép các ký tự ASCII. ngắt()¶ Gọi phương thức này từ một luồng khác để hủy bỏ bất kỳ truy vấn nào có thể được thực thi trên kết nối. Các truy vấn bị hủy bỏ sẽ tăng một ngoại lệ.(trace_callback)¶Đăng ký có thể gọi Authorizer_callback để được gọi cho mỗi lần thử truy cập một cột của bảng trong cơ sở dữ liệu. Cuộc gọi lại sẽ trả về một trong # Connect to DB and create a cursor sqliteConnection = sqlite3.connect('vinasupport.db') cursor = sqliteConnection.cursor() print('DB Init')34, # Connect to DB and create a cursor sqliteConnection = sqlite3.connect('vinasupport.db') cursor = sqliteConnection.cursor() print('DB Init')35 hoặc # Connect to DB and create a cursor sqliteConnection = sqlite3.connect('vinasupport.db') cursor = sqliteConnection.cursor() print('DB Init')37 để báo hiệu cách truy cập vào cột nên được xử lý bởi thư viện SQLite bên dưới. Đối số đầu tiên cho cuộc gọi lại biểu thị loại hoạt động nào được ủy quyền. Đối số thứ hai và thứ ba sẽ là đối số hoặc cur.execute("CREATE TABLE movie(title, year, score)")2 tùy thuộc vào đối số thứ nhất. Đối số thứ 4 là tên của cơ sở dữ liệu (Hồi giáo chính, Hồi giáo, v.v.) nếu có. Đối số thứ 5 là tên của bộ kích hoạt bên trong hoặc chế độ xem chịu trách nhiệm cho nỗ lực truy cập hoặc cur.execute("CREATE TABLE movie(title, year, score)")2 nếu lần thử truy cập này trực tiếp từ mã SQL đầu vào.transaction management of the import sqlite3 try: # Connect to DB and create a cursor sqliteConnection = sqlite3.connect('sql.db') cursor = sqliteConnection.cursor() print('DB Init') # Write a query and execute it with cursor query = 'select sqlite_version();' cursor.execute(query) # Fetch and output result result = cursor.fetchall() print('SQLite Version is {}'.format(result)) # Close the cursor cursor.close() # Handle errors except sqlite3.Error as error: print('Error occurred - ', error) # Close DB Connection irrespective of success # or failure finally: if sqliteConnection: sqliteConnection.close() print('SQLite Connection closed')0 module and the execution of triggers defined in the current database. Vượt qua cur.execute("CREATE TABLE movie(title, year, score)")2 vì Trace_Callback sẽ vô hiệu hóa cuộc gọi lại theo dõi. Ghi chú Các ngoại lệ được nêu trong cuộc gọi lại dấu vết không được truyền bá. Là một hỗ trợ phát triển và gỡ lỗi, sử dụng # Write a query and execute it with cursor query = 'select sqlite_version();' cursor.execute(query) # Fetch and output result result = cursor.fetchall() print('SQLite Version is {}'.format(result))08 để cho phép in dấu vết từ các trường hợp ngoại lệ được nêu trong cuộc gọi lại theo dõi. Mới trong phiên bản 3.3. enable_load_extension (đã bật, /) ¶(enabled, /)¶Bật công cụ SQLite tải các tiện ích mở rộng SQLite từ các thư viện được chia sẻ nếu được bật là ____105; khác, không cho phép tải các phần mở rộng sqlite. Các tiện ích mở rộng SQLite có thể xác định các chức năng mới, tập hợp hoặc triển khai bảng ảo hoàn toàn mới. Một tiện ích mở rộng nổi tiếng là phần mở rộng FullText-search được phân phối với sqlite. Ghi chú Các ngoại lệ được nêu trong cuộc gọi lại dấu vết không được truyền bá. Là một hỗ trợ phát triển và gỡ lỗi, sử dụng # Write a query and execute it with cursor query = 'select sqlite_version();' cursor.execute(query) # Fetch and output result result = cursor.fetchall() print('SQLite Version is {}'.format(result))08 để cho phép in dấu vết từ các trường hợp ngoại lệ được nêu trong cuộc gọi lại theo dõi.configure. Mới trong phiên bản 3.3.auditing event # Write a query and execute it with cursor query = 'select sqlite_version();' cursor.execute(query) # Fetch and output result result = cursor.fetchall() print('SQLite Version is {}'.format(result))12 with arguments # Write a query and execute it with cursor query = 'select sqlite_version();' cursor.execute(query) # Fetch and output result result = cursor.fetchall() print('SQLite Version is {}'.format(result))13, # Write a query and execute it with cursor query = 'select sqlite_version();' cursor.execute(query) # Fetch and output result result = cursor.fetchall() print('SQLite Version is {}'.format(result))14. enable_load_extension (đã bật, /) ¶ Bật công cụ SQLite tải các tiện ích mở rộng SQLite từ các thư viện được chia sẻ nếu được bật là ____105; khác, không cho phép tải các phần mở rộng sqlite. Các tiện ích mở rộng SQLite có thể xác định các chức năng mới, tập hợp hoặc triển khai bảng ảo hoàn toàn mới. Một tiện ích mở rộng nổi tiếng là phần mở rộng FullText-search được phân phối với sqlite.Added the # Write a query and execute it with cursor query = 'select sqlite_version();' cursor.execute(query) # Fetch and output result result = cursor.fetchall() print('SQLite Version is {}'.format(result))12 auditing event. # Write a query and execute it with cursor query = 'select sqlite_version();' cursor.execute(query) # Fetch and output result result = cursor.fetchall() print('SQLite Version is {}'.format(result))3 Mô -đun import sqlite3 try: # Connect to DB and create a cursor sqliteConnection = sqlite3.connect('sql.db') cursor = sqliteConnection.cursor() print('DB Init') # Write a query and execute it with cursor query = 'select sqlite_version();' cursor.execute(query) # Fetch and output result result = cursor.fetchall() print('SQLite Version is {}'.format(result)) # Close the cursor cursor.close() # Handle errors except sqlite3.Error as error: print('Error occurred - ', error) # Close DB Connection irrespective of success # or failure finally: if sqliteConnection: sqliteConnection.close() print('SQLite Connection closed')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, bởi vì một số nền tảng (đáng chú ý là macOS) có các thư viện SQLite được biên dịch mà không có tính năng này. Để có được hỗ trợ tiện ích mở rộng có thể tải, bạn phải vượt qua tùy chọn # Write a query and execute it with cursor query = 'select sqlite_version();' cursor.execute(query) # Fetch and output result result = cursor.fetchall() print('SQLite Version is {}'.format(result))11 để định cấu hình.(path, /)¶ Tăng một sự kiện kiểm toán # Write a query and execute it with cursor query = 'select sqlite_version();' cursor.execute(query) # Fetch and output result result = cursor.fetchall() print('SQLite Version is {}'.format(result))12 với các đối số # Write a query and execute it with cursor query = 'select sqlite_version();' cursor.execute(query) # Fetch and output result result = cursor.fetchall() print('SQLite Version is {}'.format(result))13, # Write a query and execute it with cursor query = 'select sqlite_version();' cursor.execute(query) # Fetch and output result result = cursor.fetchall() print('SQLite Version is {}'.format(result))14. Mới trong phiên bản 3.2.auditing event # Write a query and execute it with cursor query = 'select sqlite_version();' cursor.execute(query) # Fetch and output result result = cursor.fetchall() print('SQLite Version is {}'.format(result))17 with arguments # Write a query and execute it with cursor query = 'select sqlite_version();' cursor.execute(query) # Fetch and output result result = cursor.fetchall() print('SQLite Version is {}'.format(result))13, # Write a query and execute it with cursor query = 'select sqlite_version();' cursor.execute(query) # Fetch and output result result = cursor.fetchall() print('SQLite Version is {}'.format(result))19. enable_load_extension (đã bật, /) ¶ Bật công cụ SQLite tải các tiện ích mở rộng SQLite từ các thư viện được chia sẻ nếu được bật là ____105; khác, không cho phép tải các phần mở rộng sqlite. Các tiện ích mở rộng SQLite có thể xác định các chức năng mới, tập hợp hoặc triển khai bảng ảo hoàn toàn mới. Một tiện ích mở rộng nổi tiếng là phần mở rộng FullText-search được phân phối với sqlite.Added the # Write a query and execute it with cursor query = 'select sqlite_version();' cursor.execute(query) # Fetch and output result result = cursor.fetchall() print('SQLite Version is {}'.format(result))17 auditing event. Mô -đun import sqlite3 try: # Connect to DB and create a cursor sqliteConnection = sqlite3.connect('sql.db') cursor = sqliteConnection.cursor() print('DB Init') # Write a query and execute it with cursor query = 'select sqlite_version();' cursor.execute(query) # Fetch and output result result = cursor.fetchall() print('SQLite Version is {}'.format(result)) # Close the cursor cursor.close() # Handle errors except sqlite3.Error as error: print('Error occurred - ', error) # Close DB Connection irrespective of success # or failure finally: if sqliteConnection: sqliteConnection.close() print('SQLite Connection closed')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, bởi vì một số nền tảng (đáng chú ý là macOS) có các thư viện SQLite được biên dịch mà không có tính năng này. Để có được hỗ trợ tiện ích mở rộng có thể tải, bạn phải vượt qua tùy chọn # Write a query and execute it with cursor query = 'select sqlite_version();' cursor.execute(query) # Fetch and output result result = cursor.fetchall() print('SQLite Version is {}'.format(result))11 để định cấu hình.()¶ Tăng một sự kiện kiểm toán # Write a query and execute it with cursor query = 'select sqlite_version();' cursor.execute(query) # Fetch and output result result = cursor.fetchall() print('SQLite Version is {}'.format(result))12 với các đối số # Write a query and execute it with cursor query = 'select sqlite_version();' cursor.execute(query) # Fetch and output result result = cursor.fetchall() print('SQLite Version is {}'.format(result))13, # Write a query and execute it with cursor query = 'select sqlite_version();' cursor.execute(query) # Fetch and output result result = cursor.fetchall() print('SQLite Version is {}'.format(result))14.iterator to dump the database as SQL source code. Useful when saving an in-memory database for later restoration. Similar to the # Write a query and execute it with cursor query = 'select sqlite_version();' cursor.execute(query) # Fetch and output result result = cursor.fetchall() print('SQLite Version is {}'.format(result))21 command in the sqlite3 shell. Example: # Write a query and execute it with cursor query = 'select sqlite_version();' cursor.execute(query) # Fetch and output result result = cursor.fetchall() print('SQLite Version is {}'.format(result))4 Mới trong phiên bản 3.2.(target, *, pages=- 1, progress=None, name='main', sleep=0.250)¶ Đã thay đổi trong phiên bản 3.10: Đã thêm sự kiện kiểm toán # Write a query and execute it with cursor query = 'select sqlite_version();' cursor.execute(query) # Fetch and output result result = cursor.fetchall() print('SQLite Version is {}'.format(result))12. load_extension (đường dẫn, /) ¶ Tải một phần mở rộng SQLite từ một thư viện được chia sẻ tại Path. Bật tải mở rộng với# Write a query and execute it with cursor query = 'select sqlite_version();' cursor.execute(query) # Fetch and output result result = cursor.fetchall() print('SQLite Version is {}'.format(result))16 trước khi gọi phương thức này.
Tạo bản sao lưu của cơ sở dữ liệu SQLite. # Write a query and execute it with cursor query = 'select sqlite_version();' cursor.execute(query) # Fetch and output result result = cursor.fetchall() print('SQLite Version is {}'.format(result))5 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. # Write a query and execute it with cursor query = 'select sqlite_version();' cursor.execute(query) # Fetch and output result result = cursor.fetchall() print('SQLite Version is {}'.format(result))6 Thông số Target (Kết nối) - Kết nối cơ sở dữ liệu để lưu bản sao lưu vào.(category, /)¶Trang (int) - Số lượng trang để sao chép cùng một lúc. Nếu bằng hoặc nhỏ hơn >>> res = cur.execute("SELECT name FROM sqlite_master WHERE name='spam'") >>> res.fetchone() is None True9, toàn bộ cơ sở dữ liệu được sao chép trong một bước duy nhất. Mặc định là # Connect to DB and create a cursor sqliteConnection = sqlite3.connect('vinasupport.db') cursor = sqliteConnection.cursor() print('DB Init')72.Tải một phần mở rộng SQLite từ một thư viện được chia sẻ tại Path. Bật tải mở rộng với # Write a query and execute it with cursor query = 'select sqlite_version();' cursor.execute(query) # Fetch and output result result = cursor.fetchall() print('SQLite Version is {}'.format(result))16 trước khi gọi phương thức này. Tăng một sự kiện kiểm toán # Write a query and execute it with cursor query = 'select sqlite_version();' cursor.execute(query) # Fetch and output result result = cursor.fetchall() print('SQLite Version is {}'.format(result))17. int iterdump ()Trả về một trình lặp để đổ cơ sở dữ liệu dưới dạng mã nguồn SQL. Hữu ích khi lưu cơ sở dữ liệu trong bộ nhớ để phục hồi sau này. Tương tự như lệnh Sao lưu (Target, *, Pages = -1, Progress = none, name = 'Main', Sleep = 0,250) ¶ # Write a query and execute it with cursor query = 'select sqlite_version();' cursor.execute(query) # Fetch and output result result = cursor.fetchall() print('SQLite Version is {}'.format(result))7 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.(category, limit, /)¶Thông số Tải một phần mở rộng SQLite từ một thư viện được chia sẻ tại Path. Bật tải mở rộng với# Write a query and execute it with cursor query = 'select sqlite_version();' cursor.execute(query) # Fetch and output result result = cursor.fetchall() print('SQLite Version is {}'.format(result))16 trước khi gọi phương thức này.
# Write a query and execute it with cursor query = 'select sqlite_version();' cursor.execute(query) # Fetch and output result result = cursor.fetchall() print('SQLite Version is {}'.format(result))17. int iterdump ()Trả về một trình lặp để đổ cơ sở dữ liệu dưới dạng mã nguồn SQL. Hữu ích khi lưu cơ sở dữ liệu trong bộ nhớ để phục hồi sau này. Tương tự như lệnh Sao lưu (Target, *, Pages = -1, Progress = none, name = 'Main', Sleep = 0,250) ¶ # Write a query and execute it with cursor query = 'select sqlite_version();' cursor.execute(query) # Fetch and output result result = cursor.fetchall() print('SQLite Version is {}'.format(result))8 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.(*, name='main')¶Thông số Tải một phần mở rộng SQLite từ một thư viện được chia sẻ tại Path. Bật tải mở rộng với# Write a query and execute it with cursor query = 'select sqlite_version();' cursor.execute(query) # Fetch and output result result = cursor.fetchall() print('SQLite Version is {}'.format(result))16 trước khi gọi phương thức này. Tăng một sự kiện kiểm toán # Connect to DB and create a cursor sqliteConnection = sqlite3.connect('vinasupport.db') cursor = sqliteConnection.cursor() print('DB Init')61.Đã thay đổi trong phiên bản 3.10: Đã thêm sự kiện kiểm toán # Write a query and execute it with cursor query = 'select sqlite_version();' cursor.execute(query) # Fetch and output result result = cursor.fetchall() print('SQLite Version is {}'.format(result))17. iterdump () Ghi chú Trả về một trình lặp để đổ cơ sở dữ liệu dưới dạng mã nguồn SQL. Hữu ích khi lưu cơ sở dữ liệu trong bộ nhớ để phục hồi sau này. Tương tự như lệnh # Write a query and execute it with cursor query = 'select sqlite_version();' cursor.execute(query) # Fetch and output result result = cursor.fetchall() print('SQLite Version is {}'.format(result))21 trong shell sqlite3. 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.(data, /, *, name='main')¶Thông số Thông số
Ghi chú Phương pháp này chỉ khả dụng nếu thư viện SQLite cơ bản 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ế độ AutoCommit SQLite cấp thấp. # Connect to DB and create a cursor sqliteConnection = sqlite3.connect('vinasupport.db') cursor = sqliteConnection.cursor() print('DB Init')05 Nếu một giao dịch đang hoạt động (có những thay đổi không cam kết), # Connect to DB and create a cursor sqliteConnection = sqlite3.connect('vinasupport.db') cursor = sqliteConnection.cursor() print('DB Init')06 nếu không. Mới trong phiên bản 3.2. cô lập_level¶¶Thuộc tính này kiểm soát việc xử lý giao dịch được thực hiện bởi import sqlite3 try: # Connect to DB and create a cursor sqliteConnection = sqlite3.connect('sql.db') cursor = sqliteConnection.cursor() print('DB Init') # Write a query and execute it with cursor query = 'select sqlite_version();' cursor.execute(query) # Fetch and output result result = cursor.fetchall() print('SQLite Version is {}'.format(result)) # Close the cursor cursor.close() # Handle errors except sqlite3.Error as error: print('Error occurred - ', error) # Close DB Connection irrespective of success # or failure finally: if sqliteConnection: sqliteConnection.close() print('SQLite Connection closed')0. Nếu được đặt thành cur.execute("CREATE TABLE movie(title, year, score)")2, các giao dịch không bao giờ được mở ngầm. Nếu được đặt thành một trong # Connect to DB and create a cursor sqliteConnection = sqlite3.connect('vinasupport.db') cursor = sqliteConnection.cursor() print('DB Init')01, # Connect to DB and create a cursor sqliteConnection = sqlite3.connect('vinasupport.db') cursor = sqliteConnection.cursor() print('DB Init')03 hoặc # Connect to DB and create a cursor sqliteConnection = sqlite3.connect('vinasupport.db') cursor = sqliteConnection.cursor() print('DB Init')02, tương ứng với hành vi giao dịch SQLite cơ bản, quản lý giao dịch ngầm sẽ được thực hiện.transaction handling performed by import sqlite3 try: # Connect to DB and create a cursor sqliteConnection = sqlite3.connect('sql.db') cursor = sqliteConnection.cursor() print('DB Init') # Write a query and execute it with cursor query = 'select sqlite_version();' cursor.execute(query) # Fetch and output result result = cursor.fetchall() print('SQLite Version is {}'.format(result)) # Close the cursor cursor.close() # Handle errors except sqlite3.Error as error: print('Error occurred - ', error) # Close DB Connection irrespective of success # or failure finally: if sqliteConnection: sqliteConnection.close() print('SQLite Connection closed')0. If set to cur.execute("CREATE TABLE movie(title, year, score)")2, transactions are never implicitly opened. If set to one of # Connect to DB and create a cursor sqliteConnection = sqlite3.connect('vinasupport.db') cursor = sqliteConnection.cursor() print('DB Init')01, # Connect to DB and create a cursor sqliteConnection = sqlite3.connect('vinasupport.db') cursor = sqliteConnection.cursor() print('DB Init')03, or # Connect to DB and create a cursor sqliteConnection = sqlite3.connect('vinasupport.db') cursor = sqliteConnection.cursor() print('DB Init')02, corresponding to the underlying SQLite transaction behaviour, implicit transaction management is performed. Nếu không được ghi đè bởi tham số slecation_level của # Connect to DB and create a cursor sqliteConnection = sqlite3.connect('vinasupport.db') cursor = sqliteConnection.cursor() print('DB Init')24, mặc định là # Write a query and execute it with cursor query = 'select sqlite_version();' cursor.execute(query) # Fetch and output result result = cursor.fetchall() print('SQLite Version is {}'.format(result))47, đây là bí danh cho # Connect to DB and create a cursor sqliteConnection = sqlite3.connect('vinasupport.db') cursor = sqliteConnection.cursor() print('DB Init')01. Row_Factory¶¶ Một cuộc gọi có thể gọi được chấp nhận hai đối số, đối tượng import sqlite3 try: # Connect to DB and create a cursor sqliteConnection = sqlite3.connect('sql.db') cursor = sqliteConnection.cursor() print('DB Init') # Write a query and execute it with cursor query = 'select sqlite_version();' cursor.execute(query) # Fetch and output result result = cursor.fetchall() print('SQLite Version is {}'.format(result)) # Close the cursor cursor.close() # Handle errors except sqlite3.Error as error: print('Error occurred - ', error) # Close DB Connection irrespective of success # or failure finally: if sqliteConnection: sqliteConnection.close() print('SQLite Connection closed')9 và hàng thô kết quả là import sqlite3 con = sqlite3.connect("tutorial.db")8 và trả về một đối tượng tùy chỉnh đại diện cho một hàng sqlite. Example: # Write a query and execute it with cursor query = 'select sqlite_version();' cursor.execute(query) # Fetch and output result result = cursor.fetchall() print('SQLite Version is {}'.format(result))9 Nếu trả lại một tuple không đủ và bạn muốn truy cập dựa trên tên vào các cột, bạn nên xem xét cài đặt # Write a query and execute it with cursor query = 'select sqlite_version();' cursor.execute(query) # Fetch and output result result = cursor.fetchall() print('SQLite Version is {}'.format(result))51 thành loại # Write a query and execute it with cursor query = 'select sqlite_version();' cursor.execute(query) # Fetch and output result result = cursor.fetchall() print('SQLite Version is {}'.format(result))52 được tối ưu hóa cao. # Write a query and execute it with cursor query = 'select sqlite_version();' cursor.execute(query) # Fetch and output result result = cursor.fetchall() print('SQLite Version is {}'.format(result))53 cung cấp cả quyền truy cập dựa trên tên dựa trên chỉ số và trường hợp phụ thuộc vào các cột mà hầu như không có bộ nhớ. Nó có thể sẽ tốt hơn cách tiếp cận dựa trên từ điển tùy chỉnh của riêng bạn hoặc thậm chí là một giải pháp dựa trên DB_ROW. Text_Factory¶¶ Một cuộc gọi có thể gọi được chấp nhận tham số # Connect to DB and create a cursor sqliteConnection = sqlite3.connect('vinasupport.db') cursor = sqliteConnection.cursor() print('DB Init')23 và trả về một biểu 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 # Write a query and execute it with cursor query = 'select sqlite_version();' cursor.execute(query) # Fetch and output result result = cursor.fetchall() print('SQLite Version is {}'.format(result))55. Theo mặc định, thuộc tính này được đặt thành >>> res = cur.execute("SELECT name FROM sqlite_master WHERE name='spam'") >>> res.fetchone() is None True8. Nếu bạn muốn trả lại # Connect to DB and create a cursor sqliteConnection = sqlite3.connect('vinasupport.db') cursor = sqliteConnection.cursor() print('DB Init')23 thay thế, hãy đặt text_factory thành # Connect to DB and create a cursor sqliteConnection = sqlite3.connect('vinasupport.db') cursor = sqliteConnection.cursor() print('DB Init')23. Example: # Fetch and output result result = cursor.fetchall() print('SQLite Version is {}'.format(result)) # Close the cursor cursor.close()0 Total_Changes¶¶ Trả về tổng số hàng cơ sở dữ liệu đã được sửa đổi, chèn hoặc xóa kể từ khi kết nối cơ sở dữ liệu được mở. Đối tượng con trỏClassSqlite3.Cursor¶sqlite3.Cursor¶ Một ví dụ import sqlite3 try: # Connect to DB and create a cursor sqliteConnection = sqlite3.connect('sql.db') cursor = sqliteConnection.cursor() print('DB Init') # Write a query and execute it with cursor query = 'select sqlite_version();' cursor.execute(query) # Fetch and output result result = cursor.fetchall() print('SQLite Version is {}'.format(result)) # Close the cursor cursor.close() # Handle errors except sqlite3.Error as error: print('Error occurred - ', error) # Close DB Connection irrespective of success # or failure finally: if sqliteConnection: sqliteConnection.close() print('SQLite Connection closed')9 có các thuộc tính và phương thức sau. thực thi (SQL, tham số = (), /) ¶(sql, parameters=(), /)¶ 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 trình giữ chỗ ánh xạ theo trình tự hoặc tham số # Write a query and execute it with cursor query = 'select sqlite_version();' cursor.execute(query) # Fetch and output result result = cursor.fetchall() print('SQLite Version is {}'.format(result))64.placeholders that map to the sequence or # Write a query and execute it with cursor query = 'select sqlite_version();' cursor.execute(query) # Fetch and output result result = cursor.fetchall() print('SQLite Version is {}'.format(result))64 parameters. # Connect to DB and create a cursor sqliteConnection = sqlite3.connect('vinasupport.db') cursor = sqliteConnection.cursor() print('DB Init')18 sẽ chỉ thực thi một câu lệnh SQL duy nhất. Nếu bạn cố gắng thực thi nhiều hơn một tuyên bố với nó, nó sẽ tăng # Write a query and execute it with cursor query = 'select sqlite_version();' cursor.execute(query) # Fetch and output result result = cursor.fetchall() print('SQLite Version is {}'.format(result))66. Sử dụng # Connect to DB and create a cursor sqliteConnection = sqlite3.connect('vinasupport.db') cursor = sqliteConnection.cursor() print('DB Init')71 Nếu bạn muốn thực thi nhiều câu lệnh SQL với một cuộc gọi. Nếu # Connect to DB and create a cursor sqliteConnection = sqlite3.connect('vinasupport.db') cursor = sqliteConnection.cursor() print('DB Init')00 không phải là cur.execute("CREATE TABLE movie(title, year, score)")2, SQL là một câu lệnh cur.execute("CREATE TABLE movie(title, year, score)")3, # Write a query and execute it with cursor query = 'select sqlite_version();' cursor.execute(query) # Fetch and output result result = cursor.fetchall() print('SQLite Version is {}'.format(result))71, # Write a query and execute it with cursor query = 'select sqlite_version();' cursor.execute(query) # Fetch and output result result = cursor.fetchall() print('SQLite Version is {}'.format(result))72 hoặc # Write a query and execute it with cursor query = 'select sqlite_version();' cursor.execute(query) # Fetch and output result result = cursor.fetchall() print('SQLite Version is {}'.format(result))73 và không có giao dịch mở, một giao dịch được mở ra trước khi thực hiện SQL. EXECUTEMANY (SQL, tham số, /) ¶(sql, parameters, /)¶ Thực thi SQL câu lệnh SQL được tham số hóa đối với tất cả các chuỗi tham số hoặc ánh xạ được tìm thấy trong các tham số chuỗi. Cũng có thể sử dụng các tham số năng suất vòng lặp thay vì một chuỗi. Sử dụng xử lý giao dịch ngầm tương tự như # Connect to DB and create a cursor sqliteConnection = sqlite3.connect('vinasupport.db') cursor = sqliteConnection.cursor() print('DB Init')18.parameterized SQL statement sql against all parameter sequences or mappings found in the sequence parameters. It is also possible to use an iterator yielding parameters instead of a sequence. Uses the same implicit transaction handling as # Connect to DB and create a cursor sqliteConnection = sqlite3.connect('vinasupport.db') cursor = sqliteConnection.cursor() print('DB Init')18. Example: # Fetch and output result result = cursor.fetchall() print('SQLite Version is {}'.format(result)) # Close the cursor cursor.close()2 Executescript (sql_script, /) ¶(sql_script, /)¶ Thực hiện các câu lệnh SQL trong SQL_Script. Nếu có một giao dịch đang chờ xử lý, một tuyên bố # Write a query and execute it with cursor query = 'select sqlite_version();' cursor.execute(query) # Fetch and output result result = cursor.fetchall() print('SQLite Version is {}'.format(result))75 ngầm được thực thi trước tiên. Không có kiểm soát giao dịch ngầm nào khác được thực hiện; Bất kỳ điều khiển giao dịch phải được thêm vào SQL_Script. SQL_Script phải là # Connect to DB and create a cursor sqliteConnection = sqlite3.connect('vinasupport.db') cursor = sqliteConnection.cursor() print('DB Init')45. Example: # Fetch and output result result = cursor.fetchall() print('SQLite Version is {}'.format(result)) # Close the cursor cursor.close()3 fetchone () ¶()¶ Nếu # Write a query and execute it with cursor query = 'select sqlite_version();' cursor.execute(query) # Fetch and output result result = cursor.fetchall() print('SQLite Version is {}'.format(result))51 là cur.execute("CREATE TABLE movie(title, year, score)")2, hãy trả về kết quả truy vấn hàng tiếp theo được đặt là import sqlite3 con = sqlite3.connect("tutorial.db")8. Khác, chuyển nó cho nhà máy hàng và trả về kết quả của nó. Trả về cur.execute("CREATE TABLE movie(title, year, score)")2 nếu không có thêm dữ liệu. fetchmany (size = con trỏ.arraysize) ¶(size=cursor.arraysize)¶ Trả về bộ hàng tiếp theo của kết quả truy vấn là >>> res = cur.execute("SELECT name FROM sqlite_master") >>> res.fetchone() ('movie',)1. Trả lại một danh sách trống nếu không có thêm hàng. Số lượng hàng để tìm nạp trên mỗi cuộc gọi được chỉ định bởi tham số kích thước. Nếu kích thước không được đưa ra, # Write a query and execute it with cursor query = 'select sqlite_version();' cursor.execute(query) # Fetch and output result result = cursor.fetchall() print('SQLite Version is {}'.format(result))82 xác định số lượng hàng sẽ được tìm nạp. Nếu có ít hơn các hàng kích thước, càng nhiều hàng có sẵn được trả về. Lưu ý Có những cân nhắc về hiệu suất liên quan đến tham số kích thước. Để thực hiện tối ưu, tốt nhất là sử dụng thuộc tính mảng. Nếu tham số kích thước được sử dụng, thì tốt nhất là giữ lại cùng một giá trị từ một cuộc gọi # Write a query and execute it with cursor query = 'select sqlite_version();' cursor.execute(query) # Fetch and output result result = cursor.fetchall() print('SQLite Version is {}'.format(result))83 sang cuộc gọi tiếp theo. Fetchall ()()¶ Trả về tất cả (còn lại) các hàng của một truy vấn kết quả là >>> res = cur.execute("SELECT name FROM sqlite_master") >>> res.fetchone() ('movie',)1. Trả lại một danh sách trống nếu không có hàng. Lưu ý rằng thuộc tính # Write a query and execute it with cursor query = 'select sqlite_version();' cursor.execute(query) # Fetch and output result result = cursor.fetchall() print('SQLite Version is {}'.format(result))82 có thể ảnh hưởng đến hiệu suất của hoạt động này. gần()¶()¶ Đóng con trỏ ngay bây giờ (thay vì bất cứ khi nào # Write a query and execute it with cursor query = 'select sqlite_version();' cursor.execute(query) # Fetch and output result result = cursor.fetchall() print('SQLite Version is {}'.format(result))86 được gọi). Con trỏ sẽ không thể sử dụng được từ thời điểm này trở đi; Một ngoại lệ # Write a query and execute it with cursor query = 'select sqlite_version();' cursor.execute(query) # Fetch and output result result = cursor.fetchall() print('SQLite Version is {}'.format(result))66 sẽ được nâng lên nếu có bất kỳ hoạt động nào được thử với con trỏ. setInputSizes (kích thước, /) ¶(sizes, /)¶ Yêu cầu bởi DB-API. Không làm gì trong import sqlite3 try: # Connect to DB and create a cursor sqliteConnection = sqlite3.connect('sql.db') cursor = sqliteConnection.cursor() print('DB Init') # Write a query and execute it with cursor query = 'select sqlite_version();' cursor.execute(query) # Fetch and output result result = cursor.fetchall() print('SQLite Version is {}'.format(result)) # Close the cursor cursor.close() # Handle errors except sqlite3.Error as error: print('Error occurred - ', error) # Close DB Connection irrespective of success # or failure finally: if sqliteConnection: sqliteConnection.close() print('SQLite Connection closed')0. setOutputSize (kích thước, cột = none, /) ¶(size, column=None, /)¶ Yêu cầu bởi DB-API. Không làm gì trong import sqlite3 try: # Connect to DB and create a cursor sqliteConnection = sqlite3.connect('sql.db') cursor = sqliteConnection.cursor() print('DB Init') # Write a query and execute it with cursor query = 'select sqlite_version();' cursor.execute(query) # Fetch and output result result = cursor.fetchall() print('SQLite Version is {}'.format(result)) # Close the cursor cursor.close() # Handle errors except sqlite3.Error as error: print('Error occurred - ', error) # Close DB Connection irrespective of success # or failure finally: if sqliteConnection: sqliteConnection.close() print('SQLite Connection closed')0. setOutputSize (kích thước, cột = none, /) ¶¶ Mảng Thuộc tính đọc/ghi kiểm soát số lượng hàng được trả về bởi# Write a query and execute it with cursor query = 'select sqlite_version();' cursor.execute(query) # Fetch and output result result = cursor.fetchall() print('SQLite Version is {}'.format(result))83. Giá trị mặc định là 1 có nghĩa là một hàng sẽ được tìm nạp cho mỗi cuộc gọi.¶ sự liên quan¶ # Fetch and output result result = cursor.fetchall() print('SQLite Version is {}'.format(result)) # Close the cursor cursor.close()4 Thuộc tính chỉ đọc cung cấp cơ sở dữ liệu SQLite import sqlite3 try: # Connect to DB and create a cursor sqliteConnection = sqlite3.connect('sql.db') cursor = sqliteConnection.cursor() print('DB Init') # Write a query and execute it with cursor query = 'select sqlite_version();' cursor.execute(query) # Fetch and output result result = cursor.fetchall() print('SQLite Version is {}'.format(result)) # Close the cursor cursor.close() # Handle errors except sqlite3.Error as error: print('Error occurred - ', error) # Close DB Connection irrespective of success # or failure finally: if sqliteConnection: sqliteConnection.close() print('SQLite Connection closed')6 thuộc về con trỏ. Một đối tượng import sqlite3 try: # Connect to DB and create a cursor sqliteConnection = sqlite3.connect('sql.db') cursor = sqliteConnection.cursor() print('DB Init') # Write a query and execute it with cursor query = 'select sqlite_version();' cursor.execute(query) # Fetch and output result result = cursor.fetchall() print('SQLite Version is {}'.format(result)) # Close the cursor cursor.close() # Handle errors except sqlite3.Error as error: print('Error occurred - ', error) # Close DB Connection irrespective of success # or failure finally: if sqliteConnection: sqliteConnection.close() print('SQLite Connection closed')9 được tạo bằng cách gọi import sqlite3 try: # Connect to DB and create a cursor sqliteConnection = sqlite3.connect('sql.db') cursor = sqliteConnection.cursor() print('DB Init') # Write a query and execute it with cursor query = 'select sqlite_version();' cursor.execute(query) # Fetch and output result result = cursor.fetchall() print('SQLite Version is {}'.format(result)) # Close the cursor cursor.close() # Handle errors except sqlite3.Error as error: print('Error occurred - ', error) # Close DB Connection irrespective of success # or failure finally: if sqliteConnection: sqliteConnection.close() print('SQLite Connection closed')8 sẽ có thuộc tính # Write a query and execute it with cursor query = 'select sqlite_version();' cursor.execute(query) # Fetch and output result result = cursor.fetchall() print('SQLite Version is {}'.format(result))13 đề cập đến Con:¶ sự mô tả¶ Thuộc tính chỉ đọc cung cấp tên cột của truy vấn cuối cùng. Để duy trì tương thích với API Python DB, nó sẽ trả về 7-tuple cho mỗi cột trong đó sáu mục cuối cùng của mỗi tuple là cur.execute("CREATE TABLE movie(title, year, score)")2. Nó được đặt cho các câu lệnh cur.execute("CREATE TABLE movie(title, year, score)")7 mà không có bất kỳ hàng phù hợp nào.¶ lastrowid¶ Thuộc tính chỉ đọc cung cấp ID hàng của hàng được chèn cuối cùng. Nó chỉ được cập nhật sau khi các câu lệnh cur.execute("CREATE TABLE movie(title, year, score)")3 hoặc # Write a query and execute it with cursor query = 'select sqlite_version();' cursor.execute(query) # Fetch and output result result = cursor.fetchall() print('SQLite Version is {}'.format(result))73 thành công bằng phương pháp # Connect to DB and create a cursor sqliteConnection = sqlite3.connect('vinasupport.db') cursor = sqliteConnection.cursor() print('DB Init')18. Đối với các tuyên bố khác, sau # Connect to DB and create a cursor sqliteConnection = sqlite3.connect('vinasupport.db') cursor = sqliteConnection.cursor() print('DB Init')69 hoặc # Connect to DB and create a cursor sqliteConnection = sqlite3.connect('vinasupport.db') cursor = sqliteConnection.cursor() print('DB Init')71 hoặc nếu chèn không thành công, giá trị của # Fetch and output result result = cursor.fetchall() print('SQLite Version is {}'.format(result)) # Close the cursor cursor.close()02 không thay đổi. Giá trị ban đầu của # Fetch and output result result = cursor.fetchall() print('SQLite Version is {}'.format(result)) # Close the cursor cursor.close()02 là cur.execute("CREATE TABLE movie(title, year, score)")2. Ghi chú Chèn vào bảng # Write a query and execute it with cursor query = 'select sqlite_version();' cursor.execute(query) # Fetch and output result result = cursor.fetchall() print('SQLite Version is {}'.format(result))73 statement. Đã thay đổi trong phiên bản 3.6: Đã thêm hỗ trợ cho câu lệnh # Write a query and execute it with cursor query = 'select sqlite_version();' cursor.execute(query) # Fetch and output result result = cursor.fetchall() print('SQLite Version is {}'.format(result))73.¶ đếm 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("CREATE TABLE movie(title, year, score)") 3, # Write a query and execute it with cursor query = 'select sqlite_version();' cursor.execute(query) # Fetch and output result result = cursor.fetchall() print('SQLite Version is {}'.format(result))71, # Write a query and execute it with cursor query = 'select sqlite_version();' cursor.execute(query) # Fetch and output result result = cursor.fetchall() print('SQLite Version is {}'.format(result))72 và # Write a query and execute it with cursor query = 'select sqlite_version();' cursor.execute(query) # Fetch and output result result = cursor.fetchall() print('SQLite Version is {}'.format(result))73; là # Connect to DB and create a cursor sqliteConnection = sqlite3.connect('vinasupport.db') cursor = sqliteConnection.cursor() print('DB Init')72 cho các câu lệnh khác, bao gồm các truy vấn CTE. Nó chỉ được cập nhật bởi các phương thức # Connect to DB and create a cursor sqliteConnection = sqlite3.connect('vinasupport.db') cursor = sqliteConnection.cursor() print('DB Init')18 và # Connect to DB and create a cursor sqliteConnection = sqlite3.connect('vinasupport.db') cursor = sqliteConnection.cursor() print('DB Init')69.Đối tượng hàng sqlite3.Row¶ClassSqlite3.Row¶mapping access by column name and index. Một ví dụ # Write a query and execute it with cursor query = 'select sqlite_version();' cursor.execute(query) # Fetch and output result result = cursor.fetchall() print('SQLite Version is {}'.format(result))53 đóng vai trò là một # Write a query and execute it with cursor query = 'select sqlite_version();' cursor.execute(query) # Fetch and output result result = cursor.fetchall() print('SQLite Version is {}'.format(result))51 được tối ưu hóa cao cho các đối tượng import sqlite3 try: # Connect to DB and create a cursor sqliteConnection = sqlite3.connect('sql.db') cursor = sqliteConnection.cursor() print('DB Init') # Write a query and execute it with cursor query = 'select sqlite_version();' cursor.execute(query) # Fetch and output result result = cursor.fetchall() print('SQLite Version is {}'.format(result)) # Close the cursor cursor.close() # Handle errors except sqlite3.Error as error: print('Error occurred - ', error) # Close DB Connection irrespective of success # or failure finally: if sqliteConnection: sqliteConnection.close() print('SQLite Connection closed')6. Nó hỗ trợ lặp lại, kiểm tra bình đẳng, # Fetch and output result result = cursor.fetchall() print('SQLite Version is {}'.format(result)) # Close the cursor cursor.close()17 và truy cập ánh xạ theo tên cột và chỉ mục. Hai đối tượng hàng so sánh bằng nhau nếu có các cột bằng nhau và thành viên bằng nhau.()¶ chìa khóa () ¶ Trả về Example: # Fetch and output result result = cursor.fetchall() print('SQLite Version is {}'.format(result)) # Close the cursor cursor.close()5 Thay đổi trong phiên bản 3.5: Thêm hỗ trợ cắt lát.Đối tượng Blob Mới trong phiên bản 3.11.sqlite3.Blob¶ClassSqlite3.Blob¶file-like object that can read and write data in an SQLite BLOB. Call # Fetch and output result result = cursor.fetchall() print('SQLite Version is {}'.format(result)) # Close the cursor cursor.close()22 to get the size (number of bytes) of the blob. Use indices and slices for direct access to the blob data. Một ví dụ # Connect to DB and create a cursor sqliteConnection = sqlite3.connect('vinasupport.db') cursor = sqliteConnection.cursor() print('DB Init')58 là một đối tượng giống như tệp có thể đọc và ghi dữ liệu trong một blob sqlite. Gọi # Fetch and output result result = cursor.fetchall() print('SQLite Version is {}'.format(result)) # Close the cursor cursor.close()22 để có được kích thước (số byte) của blob. Sử dụng các chỉ số và lát cắt để truy cập trực tiếp vào dữ liệu Blob.context manager to ensure that the blob handle is closed after use. # Fetch and output result result = cursor.fetchall() print('SQLite Version is {}'.format(result)) # Close the cursor cursor.close()6 gần()¶()¶ Sử dụng # Connect to DB and create a cursor sqliteConnection = sqlite3.connect('vinasupport.db') cursor = sqliteConnection.cursor() print('DB Init')58 làm trình quản lý bối cảnh để đảm bảo rằng tay cầm blob được đóng sau khi sử dụng. Đóng Blob. Blob sẽ không thể sử dụng được từ thời điểm này trở đi. Một ngoại lệ# Fetch and output result result = cursor.fetchall() print('SQLite Version is {}'.format(result)) # Close the cursor cursor.close()24 (hoặc lớp con) sẽ được nâng lên nếu có bất kỳ hoạt động nào được thực hiện với Blob.(length=- 1, /)¶ đọc (length = -1, /) ¶ Đọc các byte dữ liệu độ dài từ BLOB tại vị trí bù hiện tại. Nếu kết thúc của Blob đạt được, dữ liệu lên đến EOF sẽ được trả về. Khi độ dài không được chỉ định, hoặc là âm,# Fetch and output result result = cursor.fetchall() print('SQLite Version is {}'.format(result)) # Close the cursor cursor.close()25 sẽ đọc cho đến khi kết thúc của BLOB.(data, /)¶ viết (dữ liệu, /) ¶ Viết dữ liệu vào BLOB tại 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á cuối của Blob sẽ tăng# Fetch and output result result = cursor.fetchall() print('SQLite Version is {}'.format(result)) # Close the cursor cursor.close()26.()¶ kể()¶ Trả về vị trí truy cập hiện tại của blob.(offset, origin=os.SEEK_SET, /)¶Tìm kiếm (Offset, Origin = 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à # Fetch and output result result = cursor.fetchall() print('SQLite Version is {}'.format(result)) # Close the cursor cursor.close()27 (định vị blob tuyệt đối). Các giá trị khác cho nguồn gốc là # Fetch and output result result = cursor.fetchall() print('SQLite Version is {}'.format(result)) # Close the cursor cursor.close()28 (tìm kiếm liên quan đến vị trí hiện tại) và # Fetch and output result result = cursor.fetchall() print('SQLite Version is {}'.format(result)) # Close the cursor cursor.close()29 (tìm kiếm liên quan đến kết thúc Blob Blob).Đối tượng chuẩn bị sqlite3.PrepareProtocol¶LỚPSQLITE3.PREPEPROTOCOL¶PEP 246 style adaption protocol for objects that can adapt themselves to native SQLite types. Mục đích duy nhất của PrepareProtocol loại là hoạt động như một giao thức thích ứng kiểu PEP 246 cho các đối tượng có thể tự thích nghi với các loại SQLite gốc.Ngoại lệ haPEP 249). Hệ thống phân cấp ngoại lệ được xác định bởi DB-API 2.0 (PEP 249).sqlite3.Warning¶Ngoại lệSQLITE3.warning¶ Ngoại lệ này hiện không được nâng lên bởi mô-đunimport sqlite3 try: # Connect to DB and create a cursor sqliteConnection = sqlite3.connect('sql.db') cursor = sqliteConnection.cursor() print('DB Init') # Write a query and execute it with cursor query = 'select sqlite_version();' cursor.execute(query) # Fetch and output result result = cursor.fetchall() print('SQLite Version is {}'.format(result)) # Close the cursor cursor.close() # Handle errors except sqlite3.Error as error: print('Error occurred - ', error) # Close DB Connection irrespective of success # or failure finally: if sqliteConnection: sqliteConnection.close() print('SQLite Connection closed')0, nhưng có thể được tăng lên bởi các ứng dụng sử dụng import sqlite3 try: # Connect to DB and create a cursor sqliteConnection = sqlite3.connect('sql.db') cursor = sqliteConnection.cursor() print('DB Init') # Write a query and execute it with cursor query = 'select sqlite_version();' cursor.execute(query) # Fetch and output result result = cursor.fetchall() print('SQLite Version is {}'.format(result)) # Close the cursor cursor.close() # Handle errors except sqlite3.Error as error: print('Error occurred - ', error) # Close DB Connection irrespective of success # or failure finally: if sqliteConnection: sqliteConnection.close() print('SQLite Connection closed')0, ví dụ nếu chức năng do người dùng xác định cắt ngắn dữ liệu trong khi chèn. # Fetch and output result result = cursor.fetchall() print('SQLite Version is {}'.format(result)) # Close the cursor cursor.close()32 là một lớp con của # Fetch and output result result = cursor.fetchall() print('SQLite Version is {}'.format(result)) # Close the cursor cursor.close()33. sqlite3.Error¶ Ngoại lệSQLITE3.Error¶ Nếu ngoại lệ bắt nguồn từ bên trong thư viện SQLite, hai thuộc tính sau đây sẽ được thêm vào ngoại lệ: sqlite_errorcode¶¶Mã lỗi số từ API SQLite Mới trong phiên bản 3.11. sqlite_errorname¶¶Tên tượng trưng của mã lỗi số từ API SQLite Mới trong phiên bản 3.11. sqlite_errorname¶sqlite3.InterfaceError¶Tên tượng trưng của mã lỗi số từ API SQLite Ngoại lệSQLite3.InterfaceError¶ sqlite3.DatabaseError¶Ngoại lệ được nâng lên để lạm dụng API SQLite C cấp thấp. Nói cách khác, nếu ngoại lệ này được nâng lên, nó có thể chỉ ra một lỗi trong mô -đun import sqlite3 try: # Connect to DB and create a cursor sqliteConnection = sqlite3.connect('sql.db') cursor = sqliteConnection.cursor() print('DB Init') # Write a query and execute it with cursor query = 'select sqlite_version();' cursor.execute(query) # Fetch and output result result = cursor.fetchall() print('SQLite Version is {}'.format(result)) # Close the cursor cursor.close() # Handle errors except sqlite3.Error as error: print('Error occurred - ', error) # Close DB Connection irrespective of success # or failure finally: if sqliteConnection: sqliteConnection.close() print('SQLite Connection closed')0. # Fetch and output result result = cursor.fetchall() print('SQLite Version is {}'.format(result)) # Close the cursor cursor.close()38 là một lớp con của # Fetch and output result result = cursor.fetchall() print('SQLite Version is {}'.format(result)) # Close the cursor cursor.close()24. Ngoại lệSQLITE3.databaseErrorror¶sqlite3.DataError¶ Ngoại lệ được nêu ra cho các lỗi có liên quan đến cơ sở dữ liệu. Điều này đóng vai trò là ngoại lệ cơ sở cho một số loại lỗi cơ sở dữ liệu. Nó chỉ được nuôi dưỡng ngầm thông qua các lớp con chuyên ngành. # Fetch and output result result = cursor.fetchall() print('SQLite Version is {}'.format(result)) # Close the cursor cursor.close()40 là một lớp con của # Fetch and output result result = cursor.fetchall() print('SQLite Version is {}'.format(result)) # Close the cursor cursor.close()24. Ngoại lệSQLITE3.DataError¶ sqlite3.OperationalError¶ Ngoại lệ được nêu ra cho các lỗi gây ra bởi các vấn đề với dữ liệu được xử lý, như các giá trị số ngoài phạm vi và các chuỗi quá dài. # Fetch and output result result = cursor.fetchall() print('SQLite Version is {}'.format(result)) # Close the cursor cursor.close()42 là một lớp con của # Fetch and output result result = cursor.fetchall() print('SQLite Version is {}'.format(result)) # Close the cursor cursor.close()40. Ngoại lệSQLite3.Operationalerrorror¶sqlite3.IntegrityError¶ Ngoại lệ được nêu ra cho các lỗi có liên quan đến hoạt động của cơ sở dữ liệu và không nhất thiết phải dưới sự kiểm soát của lập trình viên. Ví dụ: đường dẫn cơ sở dữ liệu không được tìm thấy hoặc không thể xử lý giao dịch. # Write a query and execute it with cursor query = 'select sqlite_version();' cursor.execute(query) # Fetch and output result result = cursor.fetchall() print('SQLite Version is {}'.format(result))03 là một lớp con của # Fetch and output result result = cursor.fetchall() print('SQLite Version is {}'.format(result)) # Close the cursor cursor.close()40. Ngoại lệSQLite3.IntegrityErrorror¶ sqlite3.InternalError¶ Ngoại lệ được nâng lên khi tính toàn vẹn quan hệ của cơ sở dữ liệu bị ảnh hưởng, ví dụ: Một kiểm tra quan trọng nước ngoài không thành công. Nó là một lớp con của # Fetch and output result result = cursor.fetchall() print('SQLite Version is {}'.format(result)) # Close the cursor cursor.close()40. Ngoại lệSqlite3sqlite3.ProgrammingError¶ Ngoại lệ được nêu ra khi SQLite gặp phải lỗi nội bộ. Nếu điều này được nâng lên, nó có thể chỉ ra rằng có một vấn đề với thư viện sqlite thời gian chạy. # Fetch and output result result = cursor.fetchall() print('SQLite Version is {}'.format(result)) # Close the cursor cursor.close()47 là một lớp con của # Fetch and output result result = cursor.fetchall() print('SQLite Version is {}'.format(result)) # Close the cursor cursor.close()40. Ngoại lệSQLITE3. sqlite3.NotSupportedError¶ Ngoại lệ được nêu ra cho các lỗi lập trình API import sqlite3 try: # Connect to DB and create a cursor sqliteConnection = sqlite3.connect('sql.db') cursor = sqliteConnection.cursor() print('DB Init') # Write a query and execute it with cursor query = 'select sqlite_version();' cursor.execute(query) # Fetch and output result result = cursor.fetchall() print('SQLite Version is {}'.format(result)) # Close the cursor cursor.close() # Handle errors except sqlite3.Error as error: print('Error occurred - ', error) # Close DB Connection irrespective of success # or failure finally: if sqliteConnection: sqliteConnection.close() print('SQLite Connection closed')0, ví dụ như cung cấp sai số lượng ràng buộc sai cho truy vấn hoặc cố gắng hoạt động trên import sqlite3 try: # Connect to DB and create a cursor sqliteConnection = sqlite3.connect('sql.db') cursor = sqliteConnection.cursor() print('DB Init') # Write a query and execute it with cursor query = 'select sqlite_version();' cursor.execute(query) # Fetch and output result result = cursor.fetchall() print('SQLite Version is {}'.format(result)) # Close the cursor cursor.close() # Handle errors except sqlite3.Error as error: print('Error occurred - ', error) # Close DB Connection irrespective of success # or failure finally: if sqliteConnection: sqliteConnection.close() print('SQLite Connection closed')6 đã đóng. # Write a query and execute it with cursor query = 'select sqlite_version();' cursor.execute(query) # Fetch and output result result = cursor.fetchall() print('SQLite Version is {}'.format(result))66 là một lớp con của # Fetch and output result result = cursor.fetchall() print('SQLite Version is {}'.format(result)) # Close the cursor cursor.close()40. ngoại lệNgoại lệ được nêu trong trường hợp API phương thức hoặc cơ sở dữ liệu không được hỗ trợ bởi thư viện SQLite cơ bản. Ví dụ: cài đặt xác định thành # Connect to DB and create a cursor sqliteConnection = sqlite3.connect('vinasupport.db') cursor = sqliteConnection.cursor() print('DB Init')05 trong # Fetch and output result result = cursor.fetchall() print('SQLite Version is {}'.format(result)) # Close the cursor cursor.close()54, nếu thư viện SQLite cơ bản không hỗ trợ các chức năng xác định. # Fetch and output result result = cursor.fetchall() print('SQLite Version is {}'.format(result)) # Close the cursor cursor.close()55 là một lớp con của # Fetch and output result result = cursor.fetchall() print('SQLite Version is {}'.format(result)) # Close the cursor cursor.close()40. Các loại sqlite và python
Loại Python
Loại Pythonobject adapters, and you can let the import sqlite3 try: # Connect to DB and create a cursor sqliteConnection = sqlite3.connect('sql.db') cursor = sqliteConnection.cursor() print('DB Init') # Write a query and execute it with cursor query = 'select sqlite_version();' cursor.execute(query) # Fetch and output result result = cursor.fetchall() print('SQLite Version is {}'.format(result)) # Close the cursor cursor.close() # Handle errors except sqlite3.Error as error: print('Error occurred - ', error) # Close DB Connection irrespective of success # or failure finally: if sqliteConnection: sqliteConnection.close() print('SQLite Connection closed')0 module convert SQLite types to Python types via converters. Loại sqliteĐây là cách các loại SQLite được chuyển đổi thành các loại Python theo mặc định: phụ thuộc vào # Fetch and output result result = cursor.fetchall() print('SQLite Version is {}'.format(result)) # Close the cursor cursor.close()79, >>> res = cur.execute("SELECT name FROM sqlite_master WHERE name='spam'") >>> res.fetchone() is None True8 theo mặc định Hệ thống loại của mô -đun import sqlite3 try: # Connect to DB and create a cursor sqliteConnection = sqlite3.connect('sql.db') cursor = sqliteConnection.cursor() print('DB Init') # Write a query and execute it with cursor query = 'select sqlite_version();' cursor.execute(query) # Fetch and output result result = cursor.fetchall() print('SQLite Version is {}'.format(result)) # Close the cursor cursor.close() # Handle errors except sqlite3.Error as error: print('Error occurred - ', error) # Close DB Connection irrespective of success # or failure finally: if sqliteConnection: sqliteConnection.close() print('SQLite Connection closed')0 có thể mở rộng theo hai cách: bạn có thể lưu trữ các loại Python bổ sung trong cơ sở dữ liệu SQLite thông qua các bộ điều hợp đối tượng và bạn có thể để mô -đun import sqlite3 try: # Connect to DB and create a cursor sqliteConnection = sqlite3.connect('sql.db') cursor = sqliteConnection.cursor() print('DB Init') # Write a query and execute it with cursor query = 'select sqlite_version();' cursor.execute(query) # Fetch and output result result = cursor.fetchall() print('SQLite Version is {}'.format(result)) # Close the cursor cursor.close() # Handle errors except sqlite3.Error as error: print('Error occurred - ', error) # Close DB Connection irrespective of success # or failure finally: if sqliteConnection: sqliteConnection.close() print('SQLite Connection closed')0 chuyển đổi các loại SQLite thành loại Python thông qua bộ chuyển đổi. Bộ điều hợp và bộ chuyển đổi mặc định # Fetch and output result result = cursor.fetchall() print('SQLite Version is {}'.format(result)) # Close the cursor cursor.close()7 Có bộ điều hợp mặc định cho các loại ngày và DateTime trong mô -đun DateTime. Chúng sẽ được gửi dưới dạng dấu thời gian ISO/ISO đến sqlite. Các bộ chuyển đổi mặc định được đăng ký dưới tên là Ngày Ngày cho # Fetch and output result result = cursor.fetchall() print('SQLite Version is {}'.format(result)) # Close the cursor cursor.close()85 và dưới tên là Tim TimeStamp cho # Fetch and output result result = cursor.fetchall() print('SQLite Version is {}'.format(result)) # Close the cursor cursor.close()86. Bằng cách này, bạn có thể sử dụng ngày/dấu thời gian từ Python mà không cần thêm bất kỳ sự lo lắng nào trong hầu hết các trường hợp. Định dạng của các bộ điều hợp cũng tương thích với các hàm ngày/thời gian SQLite thử nghiệm. Ví dụ sau đây chứng minh điều này.Nếu dấu thời gian được lưu trữ trong sqlite có phần phân số dài hơn 6 số, giá trị của nó sẽ bị cắt theo độ chính xác micro giây bởi bộ chuyển đổi dấu thời gian.Ghi chú # Fetch and output result result = cursor.fetchall() print('SQLite Version is {}'.format(result)) # Close the cursor cursor.close()8 Bộ chuyển đổi thời gian thời gian mặc định bỏ qua các độ lệch UTC trong cơ sở dữ liệu và luôn trả về một đối tượng # Fetch and output result result = cursor.fetchall() print('SQLite Version is {}'.format(result)) # Close the cursor cursor.close()86 ngây thơ. Để bảo tồn độ lệch của UTC trong dấu thời gian, rời khỏi bộ chuyển đổi bị vô hiệu hóa hoặc đăng ký bộ chuyển đổi nhận biết bù với >>> res = cur.execute("SELECT name FROM sqlite_master WHERE name='spam'") >>> res.fetchone() is None True3.sequence. For the named style, it can be either a sequence or # Write a query and execute it with cursor query = 'select sqlite_version();' cursor.execute(query) # Fetch and output result result = cursor.fetchall() print('SQLite Version is {}'.format(result))64 instance. The length of the sequence must match the number of placeholders, or a # Write a query and execute it with cursor query = 'select sqlite_version();' cursor.execute(query) # Fetch and output result result = cursor.fetchall() print('SQLite Version is {}'.format(result))66 is raised. If a # Write a query and execute it with cursor query = 'select sqlite_version();' cursor.execute(query) # Fetch and output result result = cursor.fetchall() print('SQLite Version is {}'.format(result))64 is given, it must contain keys for all named parameters. Any extra items are ignored. Here’s an example of both styles: # Fetch and output result result = cursor.fetchall() print('SQLite Version is {}'.format(result)) # Close the cursor cursor.close()9 Cách điều chỉnh các loại python tùy chỉnh thành các giá trị sqliteSQLite chỉ hỗ trợ một tập hợp các loại dữ liệu giới hạn. Để lưu trữ các loại Python tùy chỉnh trong cơ sở dữ liệu SQLite, hãy điều chỉnh chúng với một trong những loại Python SQLite tự hiểu.Python types SQLite natively understands. Có hai cách để điều chỉnh các đối tượng Python với các loại SQLite: Để đối tượng của bạn tự thích nghi hoặc sử dụng bộ chuyển đổi có thể gọi được. Sau này sẽ được ưu tiên hơn cái trước. Đối với một thư viện xuất một loại tùy chỉnh, có thể có ý nghĩa để kích hoạt loại đó để tự thích nghi. Là một nhà phát triển ứng dụng, có thể có ý nghĩa hơn khi kiểm soát trực tiếp bằng cách đăng ký các chức năng bộ điều hợp tùy chỉnh. Cách viết các đối tượng thích ứngGiả sử chúng ta có một lớp # Fetch and output result result = cursor.fetchall() print('SQLite Version is {}'.format(result)) # Close the cursor cursor.close()95 đại diện cho một cặp tọa độ, # Fetch and output result result = cursor.fetchall() print('SQLite Version is {}'.format(result)) # Close the cursor cursor.close()96 và # Fetch and output result result = cursor.fetchall() print('SQLite Version is {}'.format(result)) # Close the cursor cursor.close()97, trong một hệ tọa độ Cartesian. Cặp tọa độ sẽ được lưu trữ dưới dạng chuỗi văn bản trong cơ sở dữ liệu, sử dụng dấu chấm phẩy để tách tọa độ. Điều này có thể được thực hiện bằng cách thêm phương thức # Fetch and output result result = cursor.fetchall() print('SQLite Version is {}'.format(result)) # Close the cursor cursor.close()98 trả về giá trị thích nghi. Đối tượng được chuyển đến giao thức sẽ thuộc loại # Fetch and output result result = cursor.fetchall() print('SQLite Version is {}'.format(result)) # Close the cursor cursor.close()99. if sqliteConnection: sqliteConnection.close() print('SQLite Connection closed')0 Cách đăng ký Bộ điều hợp Callables¶Khả năng khác là tạo một hàm chuyển đổi đối tượng Python thành loại tương thích SQLite. Hàm này sau đó có thể được đăng ký bằng if sqliteConnection: sqliteConnection.close() print('SQLite Connection closed')00. if sqliteConnection: sqliteConnection.close() print('SQLite Connection closed')1 Cách chuyển đổi các giá trị SQLite thành các loại Python tùy chỉnhViết một bộ chuyển đổi cho phép bạn chuyển đổi từ các loại python tùy chỉnh sang các giá trị sqlite. Để có thể chuyển đổi từ các giá trị SQLite sang các loại python tùy chỉnh, chúng tôi sử dụng các bộ chuyển đổi. Hãy để Lùi trở lại lớp # Fetch and output result result = cursor.fetchall() print('SQLite Version is {}'.format(result)) # Close the cursor cursor.close()95. Chúng tôi đã lưu trữ các tọa độ X và Y được phân tách qua các dấu chấm phẩy dưới dạng chuỗi trong sqlite. Đầu tiên, chúng tôi sẽ xác định một hàm bộ chuyển đổi chấp nhận chuỗi là một tham số và xây dựng một đối tượng # Fetch and output result result = cursor.fetchall() print('SQLite Version is {}'.format(result)) # Close the cursor cursor.close()95 từ nó. Ghi chú Các hàm chuyển đổi luôn được truyền một đối tượng # Connect to DB and create a cursor sqliteConnection = sqlite3.connect('vinasupport.db') cursor = sqliteConnection.cursor() print('DB Init')23, bất kể kiểu dữ liệu SQLite cơ bản.always passed a # Connect to DB and create a cursor sqliteConnection = sqlite3.connect('vinasupport.db') cursor = sqliteConnection.cursor() print('DB Init')23 object, no matter the underlying SQLite data type. if sqliteConnection: sqliteConnection.close() print('SQLite Connection closed')2 Bây giờ chúng ta cần nói import sqlite3 try: # Connect to DB and create a cursor sqliteConnection = sqlite3.connect('sql.db') cursor = sqliteConnection.cursor() print('DB Init') # Write a query and execute it with cursor query = 'select sqlite_version();' cursor.execute(query) # Fetch and output result result = cursor.fetchall() print('SQLite Version is {}'.format(result)) # Close the cursor cursor.close() # Handle errors except sqlite3.Error as error: print('Error occurred - ', error) # Close DB Connection irrespective of success # or failure finally: if sqliteConnection: sqliteConnection.close() print('SQLite Connection closed')0 khi nó nên chuyển đổi một giá trị sqlite nhất định. Điều này được thực hiện khi kết nối với cơ sở dữ liệu, sử dụng tham số Detect_Types là # Connect to DB and create a cursor sqliteConnection = sqlite3.connect('vinasupport.db') cursor = sqliteConnection.cursor() print('DB Init')24. Có ba tùy chọn:
Ví dụ sau đây minh họa các cách tiếp cận ngầm và rõ ràng: if sqliteConnection: sqliteConnection.close() print('SQLite Connection closed')3 Bộ điều hợp và công thức chuyển đổi công thứcPhần này cho thấy công thức nấu ăn cho bộ điều hợp và bộ chuyển đổi chung. if sqliteConnection: sqliteConnection.close() print('SQLite Connection closed')4 Cách sử dụng các phương thức tắt kết nốiSử dụng các phương thức # Connect to DB and create a cursor sqliteConnection = sqlite3.connect('vinasupport.db') cursor = sqliteConnection.cursor() print('DB Init')18, # Connect to DB and create a cursor sqliteConnection = sqlite3.connect('vinasupport.db') cursor = sqliteConnection.cursor() print('DB Init')69 và # Connect to DB and create a cursor sqliteConnection = sqlite3.connect('vinasupport.db') cursor = sqliteConnection.cursor() print('DB Init')71 của lớp import sqlite3 try: # Connect to DB and create a cursor sqliteConnection = sqlite3.connect('sql.db') cursor = sqliteConnection.cursor() print('DB Init') # Write a query and execute it with cursor query = 'select sqlite_version();' cursor.execute(query) # Fetch and output result result = cursor.fetchall() print('SQLite Version is {}'.format(result)) # Close the cursor cursor.close() # Handle errors except sqlite3.Error as error: print('Error occurred - ', error) # Close DB Connection irrespective of success # or failure finally: if sqliteConnection: sqliteConnection.close() print('SQLite Connection closed')6, mã của bạn có thể được viết chính xác hơn vì bạn không phải tạo ra các đối tượng (thường là thừa) import sqlite3 try: # Connect to DB and create a cursor sqliteConnection = sqlite3.connect('sql.db') cursor = sqliteConnection.cursor() print('DB Init') # Write a query and execute it with cursor query = 'select sqlite_version();' cursor.execute(query) # Fetch and output result result = cursor.fetchall() print('SQLite Version is {}'.format(result)) # Close the cursor cursor.close() # Handle errors except sqlite3.Error as error: print('Error occurred - ', error) # Close DB Connection irrespective of success # or failure finally: if sqliteConnection: sqliteConnection.close() print('SQLite Connection closed')9. Thay vào đó, các đối tượng import sqlite3 try: # Connect to DB and create a cursor sqliteConnection = sqlite3.connect('sql.db') cursor = sqliteConnection.cursor() print('DB Init') # Write a query and execute it with cursor query = 'select sqlite_version();' cursor.execute(query) # Fetch and output result result = cursor.fetchall() print('SQLite Version is {}'.format(result)) # Close the cursor cursor.close() # Handle errors except sqlite3.Error as error: print('Error occurred - ', error) # Close DB Connection irrespective of success # or failure finally: if sqliteConnection: sqliteConnection.close() print('SQLite Connection closed')9 được tạo ngầm và các phương thức phím tắt này trả về các đối tượng con trỏ. Bằng cách này, bạn có thể thực thi câu lệnh cur.execute("CREATE TABLE movie(title, year, score)")7 và lặp lại trực tiếp bằng một cuộc gọi trên đối tượng import sqlite3 try: # Connect to DB and create a cursor sqliteConnection = sqlite3.connect('sql.db') cursor = sqliteConnection.cursor() print('DB Init') # Write a query and execute it with cursor query = 'select sqlite_version();' cursor.execute(query) # Fetch and output result result = cursor.fetchall() print('SQLite Version is {}'.format(result)) # Close the cursor cursor.close() # Handle errors except sqlite3.Error as error: print('Error occurred - ', error) # Close DB Connection irrespective of success # or failure finally: if sqliteConnection: sqliteConnection.close() print('SQLite Connection closed')6. if sqliteConnection: sqliteConnection.close() print('SQLite Connection closed')5 Cách sử dụng Trình quản lý bối cảnh kết nốiMột đối tượng import sqlite3 try: # Connect to DB and create a cursor sqliteConnection = sqlite3.connect('sql.db') cursor = sqliteConnection.cursor() print('DB Init') # Write a query and execute it with cursor query = 'select sqlite_version();' cursor.execute(query) # Fetch and output result result = cursor.fetchall() print('SQLite Version is {}'.format(result)) # Close the cursor cursor.close() # Handle errors except sqlite3.Error as error: print('Error occurred - ', error) # Close DB Connection irrespective of success # or failure finally: if sqliteConnection: sqliteConnection.close() print('SQLite Connection closed')6 có thể được sử dụng như một trình quản lý ngữ cảnh tự động cam kết hoặc quay lại các giao dịch mở khi rời khỏi phần thân của trình quản lý bối cảnh. Nếu phần thân của câu lệnh if sqliteConnection: sqliteConnection.close() print('SQLite Connection closed')18 kết thúc mà không có ngoại lệ, giao dịch sẽ được thực hiện. Nếu cam kết này thất bại, hoặc nếu phần thân của tuyên bố if sqliteConnection: sqliteConnection.close() print('SQLite Connection closed')18 sẽ tăng một ngoại lệ không bị bắt, giao dịch sẽ được quay lại. Nếu không có giao dịch mở khi rời khỏi phần thân của tuyên bố if sqliteConnection: sqliteConnection.close() print('SQLite Connection closed')18, Trình quản lý bối cảnh là không có. Ghi chú Các hàm chuyển đổi luôn được truyền một đối tượng # Connect to DB and create a cursor sqliteConnection = sqlite3.connect('vinasupport.db') cursor = sqliteConnection.cursor() print('DB Init')23, bất kể kiểu dữ liệu SQLite cơ bản. if sqliteConnection: sqliteConnection.close() print('SQLite Connection closed')6 Bây giờ chúng ta cần nói import sqlite3 try: # Connect to DB and create a cursor sqliteConnection = sqlite3.connect('sql.db') cursor = sqliteConnection.cursor() print('DB Init') # Write a query and execute it with cursor query = 'select sqlite_version();' cursor.execute(query) # Fetch and output result result = cursor.fetchall() print('SQLite Version is {}'.format(result)) # Close the cursor cursor.close() # Handle errors except sqlite3.Error as error: print('Error occurred - ', error) # Close DB Connection irrespective of success # or failure finally: if sqliteConnection: sqliteConnection.close() print('SQLite Connection closed')0 khi nó nên chuyển đổi một giá trị sqlite nhất định. Điều này được thực hiện khi kết nối với cơ sở dữ liệu, sử dụng tham số Detect_Types là # Connect to DB and create a cursor sqliteConnection = sqlite3.connect('vinasupport.db') cursor = sqliteConnection.cursor() print('DB Init')24. Có ba tùy chọn:Insplict: Đặt Detect_Types thành >>> res = cur.execute("SELECT name FROM sqlite_master WHERE name='spam'") >>> res.fetchone() is None True5
if sqliteConnection: sqliteConnection.close() print('SQLite Connection closed')7
if sqliteConnection: sqliteConnection.close() print('SQLite Connection closed')8
if sqliteConnection: sqliteConnection.close() print('SQLite Connection closed')9 Bộ điều hợp và công thức chuyển đổi công thức Phần này cho thấy công thức nấu ăn cho bộ điều hợp và bộ chuyển đổi chung.Cách sử dụng các phương thức tắt kết nốiSử dụng các phương thức # Connect to DB and create a cursor sqliteConnection = sqlite3.connect('vinasupport.db') cursor = sqliteConnection.cursor() print('DB Init')18, # Connect to DB and create a cursor sqliteConnection = sqlite3.connect('vinasupport.db') cursor = sqliteConnection.cursor() print('DB Init')69 và # Connect to DB and create a cursor sqliteConnection = sqlite3.connect('vinasupport.db') cursor = sqliteConnection.cursor() print('DB Init')71 của lớp import sqlite3 try: # Connect to DB and create a cursor sqliteConnection = sqlite3.connect('sql.db') cursor = sqliteConnection.cursor() print('DB Init') # Write a query and execute it with cursor query = 'select sqlite_version();' cursor.execute(query) # Fetch and output result result = cursor.fetchall() print('SQLite Version is {}'.format(result)) # Close the cursor cursor.close() # Handle errors except sqlite3.Error as error: print('Error occurred - ', error) # Close DB Connection irrespective of success # or failure finally: if sqliteConnection: sqliteConnection.close() print('SQLite Connection closed')6, mã của bạn có thể được viết chính xác hơn vì bạn không phải tạo ra các đối tượng (thường là thừa) import sqlite3 try: # Connect to DB and create a cursor sqliteConnection = sqlite3.connect('sql.db') cursor = sqliteConnection.cursor() print('DB Init') # Write a query and execute it with cursor query = 'select sqlite_version();' cursor.execute(query) # Fetch and output result result = cursor.fetchall() print('SQLite Version is {}'.format(result)) # Close the cursor cursor.close() # Handle errors except sqlite3.Error as error: print('Error occurred - ', error) # Close DB Connection irrespective of success # or failure finally: if sqliteConnection: sqliteConnection.close() print('SQLite Connection closed')9. Thay vào đó, các đối tượng import sqlite3 try: # Connect to DB and create a cursor sqliteConnection = sqlite3.connect('sql.db') cursor = sqliteConnection.cursor() print('DB Init') # Write a query and execute it with cursor query = 'select sqlite_version();' cursor.execute(query) # Fetch and output result result = cursor.fetchall() print('SQLite Version is {}'.format(result)) # Close the cursor cursor.close() # Handle errors except sqlite3.Error as error: print('Error occurred - ', error) # Close DB Connection irrespective of success # or failure finally: if sqliteConnection: sqliteConnection.close() print('SQLite Connection closed')9 được tạo ngầm và các phương thức phím tắt này trả về các đối tượng con trỏ. Bằng cách này, bạn có thể thực thi câu lệnh cur.execute("CREATE TABLE movie(title, year, score)")7 và lặp lại trực tiếp bằng một cuộc gọi trên đối tượng import sqlite3 try: # Connect to DB and create a cursor sqliteConnection = sqlite3.connect('sql.db') cursor = sqliteConnection.cursor() print('DB Init') # Write a query and execute it with cursor query = 'select sqlite_version();' cursor.execute(query) # Fetch and output result result = cursor.fetchall() print('SQLite Version is {}'.format(result)) # Close the cursor cursor.close() # Handle errors except sqlite3.Error as error: print('Error occurred - ', error) # Close DB Connection irrespective of success # or failure finally: if sqliteConnection: sqliteConnection.close() print('SQLite Connection closed')6.PEP 249. Cách sử dụng Trình quản lý bối cảnh kết nối Nếu # Connect to DB and create a cursor sqliteConnection = sqlite3.connect('vinasupport.db') cursor = sqliteConnection.cursor() print('DB Init')00 được đặt thành cur.execute("CREATE TABLE movie(title, year, score)")2, không có giao dịch nào được mở hoàn toàn.Điều này để lại thư viện SQLite cơ bản trong chế độ AutoCommit, nhưng cũng cho phép người dùng thực hiện xử lý giao dịch của riêng họ bằng cách sử dụng các câu lệnh SQL rõ ràng.Chế độ AutoCommit thư viện SQLite cơ bản có thể được truy vấn bằng thuộc tính if sqliteConnection: sqliteConnection.close() print('SQLite Connection closed')38. Phương thức # Connect to DB and create a cursor sqliteConnection = sqlite3.connect('vinasupport.db') cursor = sqliteConnection.cursor() print('DB Init')71 hoàn toàn thực hiện bất kỳ giao dịch đang chờ xử lý nào trước khi thực hiện tập lệnh SQL đã cho, bất kể giá trị của # Connect to DB and create a cursor sqliteConnection = sqlite3.connect('vinasupport.db') cursor = sqliteConnection.cursor() print('DB Init')00. Đã thay đổi trong phiên bản 3.6: import sqlite3 try: # Connect to DB and create a cursor sqliteConnection = sqlite3.connect('sql.db') cursor = sqliteConnection.cursor() print('DB Init') # Write a query and execute it with cursor query = 'select sqlite_version();' cursor.execute(query) # Fetch and output result result = cursor.fetchall() print('SQLite Version is {}'.format(result)) # Close the cursor cursor.close() # Handle errors except sqlite3.Error as error: print('Error occurred - ', error) # Close DB Connection irrespective of success # or failure finally: if sqliteConnection: sqliteConnection.close() print('SQLite Connection closed')0 used to implicitly commit an open transaction before DDL statements. This is no longer the case. |