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

前端 未结 11 2083
生来不讨喜
生来不讨喜 2021-01-11 13:10

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

I\'m querying my database with something like this:

SQLiteCommand cmd = new SQLiteC         


        
相关标签:
11条回答
  • 2021-01-11 13:51

    Do a second query:

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

    Your reader will then contain a single row with one column containing the number of rows in the result set. The count will have been performed on the server, so it should be nicely quick.

    0 讨论(0)
  • 2021-01-11 13:56

    What you request is not feasible -- to quote Igor Tandetnik, my emphasis:

    SQLite produces records one by one, on request, every time you call sqlite3_step. It simply doesn't know how many there are going to be, until on some sqlite3_step call it discovers there are no more.

    (sqlite3_step is the function in SQLite's C API that the C# interface is calling here for each row in the result).

    You could rather do a "SELECT COUNT(*) from myTable where word = '" + word + "';" first, before your "real" query -- that will tell you how many rows you're going to get from the real query.

    0 讨论(0)
  • 2021-01-11 13:57

    You do have to count with select count... from...

    This will make your application slower. However there is an easy way to make your app faster and that way is using parameterized queries.

    See here: How do I get around the "'" problem in sqlite and c#?

    (So besides speed parameterized queries have 2 other advantages too.)

    0 讨论(0)
  • 2021-01-11 14:04

    Here is my full implementation in a static method. You should be able to plug this into your class (replace _STR_DB_FILENAME & STR_TABLE_NAME with your database file name and table name).

        /// <summary>
        /// Returns a count of the number of items in the database.
        /// </summary>
        /// <returns></returns>
        public static int GetNumberOfItemsInDB()
        {
            // Initialize the count variable to be returned
            int count = 0;            
    
            // Start connection to db
            using (SqliteConnection db =
                new SqliteConnection("Filename=" + _STR_DB_FILENAME))
            {
                // open connection
                db.Open();
    
                SqliteCommand queryCommand = new SqliteCommand();
                queryCommand.Connection = db;
    
                // Use parameterized query to prevent SQL injection attacks
                queryCommand.CommandText = "SELECT COUNT(*) FROM " + _STR_TABLE_NAME;
                // Execute command and convert response to Int
                count = Convert.ToInt32(queryCommand.ExecuteScalar());
                // Close connection
                db.Close();
            }
            // return result(count)
            return count;
        }
    

    Note: To improve performance, you can replace '' in "SELECT COUNT()…." with the column name of the primary key in your table for much faster performance on larger datasets.

    0 讨论(0)
  • 2021-01-11 14:05

    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.

    0 讨论(0)
  • 2021-01-11 14:06

    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.

    0 讨论(0)
提交回复
热议问题