📜  Python SQLite-游标对象

📅  最后修改于: 2020-11-07 08:59:14             🧑  作者: Mango


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类/对象提供的各种方法。

Sr.No Method & Description
1

execute()

This routine executes an SQL statement. The SQL statement may be parameterized (i.e., placeholders instead of SQL literals). The psycopg2 module supports placeholder using %s sign

For example:cursor.execute(“insert into people values (%s, %s)”, (who, age))

2

executemany()

This routine executes an SQL command against all parameter sequences or mappings found in the sequence sql.

3

fetchone()

This method fetches the next row of a query result set, returning a single sequence, or None when no more data is available.

4

fetchmany()

This routine fetches the next set of rows of a query result, returning a list. An empty list is returned when no more rows are available. The method tries to fetch as many rows as indicated by the size parameter.

5

fetchall()

This routine fetches all (remaining) rows of a query result, returning a list. An empty list is returned when no rows are available.

物产

以下是Cursor类的属性-

Sr.No Method & Description
1

arraySize

This is a read/write property you can set the number of rows returned by the fetchmany() method.

2

description

This is a read only property which returns the list containing the description of columns in a result-set.

3

lastrowid

This is a read only property, if there are any auto-incremented columns in the table, this returns the value generated for that column in the last INSERT or, UPDATE operation.

4

rowcount

This returns the number of rows returned/updated in case of SELECT and UPDATE operations.

5

connection

This read-only attribute provides the SQLite database Connection used by the Cursor object.