Python 数据访问教程

Python 数据访问主页

Python MySQL

Python MySQL 简介 Python MySQL 数据库连接 Python MySQL 创建数据库 Python MySQL 创建表 Python MySQL 插入数据 Python MySQL 选择数据 Python MySQL Where 子句 Python MySQL 排序 Python MySQL 更新表 Python MySQL 删除数据 Python MySQL 删除表 Python MySQL Limit 子句 Python MySQL 连接 Python MySQL 游标对象

Python PostgreSQL

Python PostgreSQL 简介 Python PostgreSQL 数据库连接 Python PostgreSQL 创建数据库 Python PostgreSQL 创建表 Python PostgreSQL 插入数据 Python PostgreSQL 选择数据 Python PostgreSQL Where 子句 Python PostgreSQL 排序 Python PostgreSQL 更新表 Python PostgreSQL 删除数据 Python PostgreSQL 删除表 Python PostgreSQL Limit 子句 Python PostgreSQL 连接 Python PostgreSQL 游标对象

Python SQLite

Python SQLite 简介 Python SQLite 建立连接 Python SQLite 创建表 Python SQLite 插入数据 Python SQLite 选择数据 Python SQLite Where 子句 Python SQLite 排序 Python SQLite 更新表 Python SQLite 删除数据 Python SQLite 删除表 Python SQLite Limit 子句 Python SQLite 连接 Python SQLite 游标对象

Python MongoDB

Python MongoDB 简介 Python MongoDB 创建数据库 Python MongoDB 创建集合 Python MongoDB 插入文档 Python MongoDB 查找 Python MongoDB 查询 Python MongoDB 排序 Python MongoDB 删除文档 Python MongoDB 删除集合 Python MongoDB 更新 Python MongoDB Limit 子句

Python 数据访问资源

Python 数据访问 快速指南 Python 数据访问 有用资源 Python 数据访问 讨论


Python SQLite - Cursor 对象

sqlite3.Cursor 类是一个实例,您可以使用它来调用执行 SQLite 语句的方法,从查询的结果集中获取数据。您可以使用 Connection 对象/类的 cursor() 方法创建 Cursor 对象。

示例

import sqlite3

#连接到 sqlite
conn = sqlite3.connect('example.db')

#使用 cursor() 方法创建游标对象
cursor = conn.cursor()

方法

以下是 Cursor 类/对象提供的各种方法。

Sr.No 方法和说明
1

execute()

此例程执行 SQL 语句。SQL 语句可能被参数化(即占位符而不是 SQL 文字)。 psycopg2 模块支持使用 %s 符号的占位符

例如:cursor.execute("insert into people values (%s, %s)", (who, age))

2

executemany()

此例程针对序列 sql 中找到的所有参数序列或映射执行 SQL 命令。

3

fetchone()

此方法获取查询结果集的下一行,返回单个序列,如果没有更多可用数据则返回 None。

4

fetchmany()

此例程获取查询结果的下一组行,并返回一个列表。当没有更多行可用时,将返回一个空列表。该方法尝试获取 size 参数指示的尽可能多的行。

5

fetchall()

此例程获取查询结果的所有(剩余)行,并返回一个列表。当没有行可用时,将返回一个空列表。

属性

以下是 Cursor 类的属性 −

Sr.No 方法 &描述
1

arraySize

这是一个读/写属性,您可以设置 fetchmany() 方法返回的行数。

2

description

这是一个只读属性,它返回包含结果集中列描述的列表。

3

lastrowid

这是一个只读属性,如果表中有任何自动递增的列,它将返回上次 INSERT 或 UPDATE 中为该列生成的值操作。

4

rowcount

这将返回 SELECT 和 UPDATE 操作中返回/更新的行数。

5

connection

此只读属性提供 Cursor 对象使用的 SQLite 数据库连接。