Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Count Number of Rows in a SQLite Database

I'm trying the following code to count the number of rows in my SQLite database table, but it throws an exception. Is these a simpler way to do this?

- (void) countRecords {
    int rows = 0;
    @try {
        NSString *dbPath = [self getDBPath];

        if (sqlite3_open([dbPath UTF8String], &database) == SQLITE_OK) {

            NSString *strSQL;
            strSQL = @"SELECT COUNT(*) FROM MYTABLE";
            const char *sql = (const char *) [strSQL UTF8String];
            sqlite3_stmt *stmt;

            if (sqlite3_prepare_v2(database, sql, -1, &stmt, NULL) == SQLITE_OK) {

                // THIS IS WHERE IT FAILS:
                if (SQLITE_DONE!=sqlite3_step(stmt) ) {

                    NSAssert1(0,@"Error when counting rows  %s",sqlite3_errmsg(database));

                } else {
                    rows = sqlite3_column_int(stmt, 0);
                    NSLog(@"SQLite Rows: %i", rows);
                }
                sqlite3_finalize(stmt);
            }
            sqlite3_close(database);
        }
    }
    @catch (NSException * e) {
        NSLog(@"Error Counting");
    }
}
like image 584
Lauren Quantrell Avatar asked May 12 '11 14:05

Lauren Quantrell


People also ask

How do I count rows in DB?

Use the COUNT aggregate function to count the number of rows in a table. This function takes the name of the column as its argument (e.g., id ) and returns the number of rows for this particular column in the table (e.g., 5).

How many rows can a SQLite table have?

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 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');

What is the most performant way to get the total number of records from a table?

With the help of the SQL count statement, you can get the number of records stored in a table.


2 Answers

I came across a solution, using my code above, just replacing the step statement with the code below:

if (sqlite3_step(stmt) == SQLITE_ERROR) {
    NSAssert1(0,@"Error when counting rows  %s",sqlite3_errmsg(database));
} else {
    rows = sqlite3_column_int(stmt, 0);
    NSLog(@"SQLite Rows: %i", rows);
}
like image 126
Lauren Quantrell Avatar answered Sep 19 '22 14:09

Lauren Quantrell


This usually works for me

- (NSInteger )numberRecordsForTable:(NSString *)table {
NSInteger numTableRecords = -1;
if (sqlite3_open([self.dbPath UTF8String], &database) == SQLITE_OK) {
    NSString *sqlStatement = [NSString stringWithFormat: @"select count(*) from %@", table];
    const char *sql = [sqlStatement cStringUsingEncoding:NSUTF8StringEncoding];
    if(sqlite3_prepare_v2(database, sql, -1, &sqlClause, NULL) == SQLITE_OK) {          
        while(sqlite3_step(sqlClause) == SQLITE_ROW) {
            numTableRecords = sqlite3_column_int(sqlClause, 0);
        }
    }
    else {
        printf("could not prepare statement: %s\n", sqlite3_errmsg(database));
    }
}
else {
    NSLog(@"Error in Opening Database File");
}
sqlite3_close(database);
return numTableRecords; 

}

HTH

like image 20
Jeremy Bassett Avatar answered Sep 20 '22 14:09

Jeremy Bassett