Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I count the number of rows returned in my SQLite reader in C#?

Tags:

c#

sqlite

I'm working in Microsoft Visual C# 2008 Express and with SQLite.

I'm querying my database with something like this:

SQLiteCommand cmd = new SQLiteCommand(conn);

cmd.CommandText = "select id from myTable where word = '" + word + "';";
cmd.CommandType = CommandType.Text;
SQLiteDataReader reader = cmd.ExecuteReader();

Then I do something like this:

if (reader.HasRows == true) {
    while (reader.Read()) {
        // I do stuff here
    }
}

What I want to do is count the number of rows before I do "reader.Read()" since the number returned will affect what I want/need to do. I know I can add a count within the while statement, but I really need to know the count before.

Any suggestions?

like image 351
adeena Avatar asked Jun 07 '09 14:06

adeena


People also ask

How do I count rows in SQLite?

SQLite Count(*) Function In SQLite the Count(*) function will return total number of rows available in a table, including the rows which contain NULL values. The Count(*) will not take any parameters other than the asterisk symbol (*).

How many rows is too many for SQLite?

The theoretical maximum number of rows in a table is 264 (18446744073709551616 or about 1.8e+19). This limit is unreachable since the maximum database size of 281 terabytes will be reached first.

How do I count the number of columns in SQLite?

If you are using SQLite, you can use PRAGMA functions to find this info. See here for documentation. To find the number of columns for a table (let's call this table tracks ) you would use this query: SELECT COUNT(*) FROM pragma_table_info('tracks');

How many entries can SQLite handle?

A SQLite database can have maximum 2147483646 pages. Hence the maximum number of tables in a schema cannot reach more than 2147483646. The maximum number of rows in a table is 264. The maximum number of columns is 32767 in a table.


2 Answers

The DataReader runs lazily, so it doesn't pick up the entirety of the rowset before beginning. This leaves you with two choices:

  1. Iterate through and count
  2. Count in the SQL statement.

Because I'm more of a SQL guy, I'll do the count in the SQL statement:

cmd.CommandText = "select count(id) from myTable where word = '" + word + "';";
cmd.CommandType = CommandType.Text;
int RowCount = 0;

RowCount = Convert.ToInt32(cmd.ExecuteScalar());

cmd.CommandText = "select id from myTable where word = '" + word + "';";
SQLiteDataReader reader = cmd.ExecuteReader();

//...

Note how I counted *, not id in the beginning. This is because count(id) will ignore id's, while count(*) will only ignore completely null rows. If you have no null id's, then use count(id) (it's a tad bit faster, depending on your table size).

Update: Changed to ExecuteScalar, and also count(id) based on comments.

like image 51
Eric Avatar answered Oct 16 '22 06:10

Eric


Try this,

SQLiteCommand cmd = new SQLiteCommand(conn);

cmd.CommandText = "select id from myTable where word = '" + word + "';";

SQLiteDataReader reader = cmd.ExecuteReader();

while (reader.HasRows)

     reader.Read();

int total_rows_in_resultset = reader.StepCount;

total_rows_in_resultset gives you the number of rows in resultset after processing query

remember that if you wanna use the same reader then close this reader and start it again.

like image 26
Anokha Ladla Avatar answered Oct 16 '22 08:10

Anokha Ladla