【发布时间】:2011-04-10 21:16:49
【问题描述】:
我正在使用 Python/SQLite 访问数据库。运行查询并获得结果后,我想知道查询结果和数据库中的行数、列数和列名。
例如,如果我运行“SELECT * from table”,我得到
身份证号码 -------------------- 约翰一书 10 2 周杰伦 20我能知道我有2行3列,列数是id/name/number?
添加
根据 Rafael SDM Sierra 的回答,我可以得到如下信息。
description = self.cursor.description
qr.numberOfCol = len(description) <-- # of column
for item in description:
qr.names.append(item[0]) <-- Names of column
count = 0
for row in self.cursor:
count += 1
qr.result.append(row)
qr.numberOfRow = count <-- # of row
【问题讨论】:
-
你在使用 Python ORM 吗?像 Django 或 SQLAlchemy?这些 API 可以帮助您获得所需的信息。
-
@rubayeet :不,我只是使用 python/SQLite 作为 'from sqlite3 import dbapi2 as sqlite3'
标签: sqlite