
- Python SQLite 教程
- Python SQLite - 首頁
- Python SQLite - 簡介
- Python SQLite - 建立連線
- Python SQLite - 建立表
- Python SQLite - 插入資料
- Python SQLite - 選擇資料
- Python SQLite - Where 子句
- Python SQLite - Order By
- Python SQLite - 更新表
- Python SQLite - 刪除資料
- Python SQLite - 刪除表
- Python SQLite - Limit
- Python SQLite - Join
- Python SQLite - 遊標物件
- Python SQLite 有用資源
- Python SQLite - 快速指南
- Python SQLite - 有用資源
- Python SQLite - 討論
Python SQLite - 遊標物件
sqlite3.Cursor 類是一個例項,您可以使用它來呼叫執行 SQLite 語句的方法,並從查詢的結果集中獲取資料。您可以使用 Connection 物件/類的 cursor() 方法建立 Cursor 物件。
示例
import sqlite3 #Connecting to sqlite conn = sqlite3.connect('example.db') #Creating a cursor object using the cursor() method cursor = conn.cursor()
方法
以下是 Cursor 類/物件提供的各種方法。
方法 | 描述 |
---|---|
execute() |
此例程執行 SQL 語句。SQL 語句可以是引數化的(即,使用佔位符而不是 SQL 字面量)。psycopg2 模組支援使用 %s 符號進行佔位符。 例如:cursor.execute("insert into people values (%s, %s)", (who, age)) |
executemany() |
此例程對序列 sql 中找到的所有引數序列或對映執行 SQL 命令。 |
fetchone() |
此方法獲取查詢結果集的下一行,返回單個序列,或者在沒有更多資料可用時返回 None。 |
fetchmany() |
此例程獲取查詢結果的下一組行,返回一個列表。當沒有更多行可用時,返回一個空列表。該方法嘗試獲取與 size 引數指示的相同數量的行。 |
fetchall() |
此例程獲取查詢結果的所有(剩餘)行,返回一個列表。當沒有行可用時,返回一個空列表。 |
屬性
以下是 Cursor 類的屬性。
方法 | 描述 |
---|---|
arraySize |
這是一個讀寫屬性,您可以設定 fetchmany() 方法返回的行數。 |
description |
這是一個只讀屬性,它返回一個包含結果集中列的描述的列表。 |
lastrowid |
這是一個只讀屬性,如果表中存在任何自動遞增列,則返回在上次 INSERT 或 UPDATE 操作中為該列生成的值。 |
rowcount |
在 SELECT 和 UPDATE 操作的情況下,這將返回返回/更新的行數。 |
connection |
此只讀屬性提供 Cursor 物件使用的 SQLite 資料庫連線。 |
廣告