prosseek
prosseek

Reputation: 190809

What's the least expensive way to get the number of rows (data) in a SQLite DB?

When I need to get the number of row(data) inside a SQLite database, I run the following pseudo code.

cmd = "SELECT Count(*) FROM benchmark"
res = runcommand(cmd)
read res to get result.

But, I'm not sure if it's the best way to go. What would be the optimum way to get the number of data in a SQLite DB? I use python for accessing SQLite.

Upvotes: 0

Views: 188

Answers (1)

Mark Byers
Mark Byers

Reputation: 838236

Your query is correct but I would add an alias to make it easier to refer to the result:

SELECT COUNT(*) AS cnt FROM benchmark

Regarding this line:

count size of res

You don't want to count the number of rows in the result set - there will always be only one row. Just read the result out from the column cnt of the first row.

Upvotes: 1

Related Questions