On 20 Jul 2010, at 7:42pm, dmsmsm wrote: > Iam new to sqlite3. I want to get the number of rows in a table. I am using > sqlite_exec(), but not sure, this is the correct function to get the number > of rows. > Please help me!!!! > my code is > > NSString *rowcount = [NSString stringWithFormat:@"SELECT COUNT(*) FROM > DB;"]; > > char *err; > if (sqlite3_exec(db, [rowcount UTF8String], NULL, NULL, &err) != SQLITE_OK) > { > sqlite3_close(db); > NSAssert(0, @"Failed to get Number of Rows"); > }
First, you should know that most people here don't know Objective-C or Cocoa so they won't understand your code. You are writing in a language which only some Mac or iPhone programmers will recognise. Second, I see no reason why your code wouldn't work. Did you try it ? What happened ? Below I'm just suggesting some changes. > NSString *rowcount = [NSString stringWithFormat:@"SELECT COUNT(*) FROM DB;"]; You do not need the semi-colon since the SQL operating accepts only whole commands. It should be faster to name a specific column rather than use '*': NSString *rowcount = [NSString stringWithFormat:@"SELECT COUNT(rowid) FROM DB"]; > char *err; > if (sqlite3_exec(db, [rowcount UTF8String], NULL, NULL, &err) != SQLITE_OK) > { > sqlite3_close(db); > NSAssert(0, @"Failed to get Number of Rows"); > } No need to close the database unless your application is immediately quitting. Even if your application quits entirely without closing the database your data will still be okay. It will be useful to report all available error information as part of your error message: char *sqlErrorMsg = NULL; int sqlResult = sqlite3_exec(db, [rowcount UTF8String], NULL, NULL, &sqlErrorMsg); NSAssert2(sqlResult == SQLITE_OK, @"Get Number of Rows failed with error %i: %s.", sqlResult, sqlErrorMsg); Simon. _______________________________________________ sqlite-users mailing list sqlite-users@sqlite.org http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users