python sqlite3 如何得到select 语句返回的记录集的行数?

博客围绕Python中SQLite获取行数展开。官方文档的cursor.rowcount通常返回-1,因SQLite事先不知行数,遍历才逐行返回。介绍了用fetchall后取长度或执行select count(*)再fetchone获取行数的方法,还探讨了rowcount在不同数据库的表现。
部署运行你感兴趣的模型镜像

官方文档有cursor.rowcount ,但通常返回值是-1。

查询相关资料,得到答案如下:

没有可以直接得到行数的函数。sqlite事先不知道行数,遍历时才逐行返回。--->遍历完才知道的话,是否意味着并没有一次过将全部数据返回写入内存?

可以用 : result=cursor.fetchall()  然后 count=len(result) 获取。

或者执行一次 select count(*) from x where y 然后 fetchone 获取

 

参考:https://stackoverflow.com/questions/3890517/how-do-i-use-num-rows-function-in-the-mysqldb-api-for-python

 

From the SQLite3 FAQ here

Q) Which func could get the number of rows?

A) There is no function to retrieve the number of rows in a result set. SQLite doesn't know the number in advance, but returns row by row while iterating through the tables. The application can increment a row counter as needed at every successful sqlite3_step() .

Some wrappers are able to collect all rows in a resultset in a in-memory table, so they can return the number of rows.

You can always get the number of rows that a certain SELECT statement would return at the cost of some performance:

BEGIN IMMEDIATE TRANSACTION;
SELECT COUNT(*) FROM x WHERE y;
SELECT a,b,c FROM x WHERE y;
ROLLBACK TRANSACTION;

You have to wrap this in a transaction to prevent other connections from inserting / deleting rows between the two SELECT statements.

-------------

Although the Cursor class of the sqlite3 module implements this attribute, the database engine’s own support for the determination of “rows affected”/”rows selected” is quirky. [官方文档]

在Python实现的DB API中,rowcount属性“当在特定类型数据库接口未实现cursor的rowcount属性时会返回-1”,sqlite就属于这种情况。

------------

rowcount返回的仅仅是受影响的行,select不会影响这些行。所有的SELECT语句都不会有rowcount

-------------

另有不少文章说  cursor执行过fetchall() 一次之后 可以用 cursor.rowcount 获得,但我测试也是-1,那么是不是sqlite不行,但其他数据库可以呢?

 

您可能感兴趣的与本文相关的镜像

Python3.9

Python3.9

Conda
Python

Python 是一种高级、解释型、通用的编程语言,以其简洁易读的语法而闻名,适用于广泛的应用,包括Web开发、数据分析、人工智能和自动化脚本

### 如何在Python中使用sqlite3执行SELECT语句并获取查询结果 为了在 Python 中通过 `sqlite3` 执行 SELECT 查询,需先建立到 SQLite 数据库的连接。这可以通过导入 `sqlite3` 库并调用 `.connect()` 方法来完成[^2]。 一旦建立了数据库连接,下一步是创建一个游标对象用于执行 SQL 语句。此过程涉及调用连接对象上的 `.cursor()` 方法获得一个新的游标实例。 对于简单的全表扫描查询,可利用 `execute()` 函数传递完整的 SQL 语句字符串给游标对象: ```python import sqlite3 conn = sqlite3.connect('example.db') cursor = conn.cursor() cursor.execute("SELECT * FROM users") ``` 当需要根据特定条件筛选记录时,则可以在 SQL 语句内加入 WHERE 子句,并采用参数化查询方式防止SQL注入攻击: ```python user_name = 'Loki' cursor.execute("SELECT * FROM users WHERE name=?", (user_name,)) ``` 取得查询结果有多种方法取决于预期的数据量大小和个人偏好。如果只关心单条匹配项,那么可以直接调用 `.fetchone()` 获取下一个可用行;而对于多行或多列的情况则更适合使用 `.fetchall()` 或者迭代器模式逐行读取数据[^4]: ```python # 取得全部用户列表 all_users = cursor.fetchall() for user in all_users: print(f"ID: {user[0]}, Name: {user[1]}, Email: {user[2]}") # 查找名为'Loki'用户的邮箱地址 data = cursor.fetchone() if data is not None: print(f"{user_name}'s email: {data[2]}") else: print(f"No record found for the username '{user_name}'.") ``` 最后,在完成了所有的数据库交互之后应当记得关闭游标和断开与数据库之间的链接以释放资源。 ```python cursor.close() conn.close() ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值