while (sqlite3_step(statement) == SQLITE_ROW) never executed?
while (sqlite3_step(statement) == SQLITE_ROW) loop is not executing can anybody help ?
NSLog(@" %d, %d ",sqlite3_step(statement),SQLITE_ROW);
the output is always 21 100
sqlite3_stmt *statement;
sqlite3 *database;
if (sqlite3_open("myDB.sql", &database) == SQLITE_OK) {
NSLog(@"1 %d",SQLITE_OK);
NSLog(@" %d, %d ",sqlite3_step(statement),SQLITE_ROW);
const char *sql = "SELECT myName FROM myTable";
if (sqlite3_prepare_v2(database, sql, -1, &statement, NULL) == SQLITE_OK) {
while (sqlite3_step(statement) == SQLITE_ROW) {
int primaryKey = sqlite开发者_Python百科3_column_int(statement, 0);
}
}
if (statement) {
sqlite3_finalize(statement);
}
}
Try This,
sqlite3_stmt *statement;
sqlite3 *database;
NSString *sqLiteDb = [[NSBundle mainBundle] pathForResource:@"myDB"
ofType:@"sql"];
if (sqlite3_open([sqLiteDb UTF8String], &database) != SQLITE_OK) {
NSLog(@"Failed to open database!");
}
NSLog(@" %d, %d ",sqlite3_step(statement),SQLITE_ROW);
const char *sql = "SELECT myName FROM myTable";
if (sqlite3_prepare_v2(database, sql, -1, &statement, NULL) == SQLITE_OK) {
while (sqlite3_step(statement) == SQLITE_ROW) {
int primaryKey = sqlite3_column_int(statement, 0);
}
}
else {
sqlite3_close(database);
NSLog(@"Failed to open database with message '%s'.", sqlite3_errmsg(database));
}
if (statement) {
sqlite3_finalize(statement);
}
}
Here some links for reference.
http://dblog.com.au/iphone-development-tutorials/iphone-sdk-tutorial-reading-data-from-a-sqlite-database/
http://www.scribd.com/doc/11524402/iPhone-Programming-with-SQLite
http://www.raywenderlich.com/913/sqlite-101-for-iphone-developers-making-our-app
sqlite3 *database;
if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK)
{
NSString *sqlStatement = [NSString stringWithFormat:@""SELECT myName FROM myTable"];
sqlite3_stmt *compiledStatement;
if(sqlite3_prepare_v2(database, [sqlStatement cStringUsingEncoding:NSUTF8StringEncoding],-1,&compiledStatement, NULL) == SQLITE_OK)
{
NSLog(@"%@",sqlStatement);
if (sqlite3_step(compiledStatement) == SQLITE_ROW)
{
int primaryKey = [[NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement,0 )]intValue] ;
}
}
sqlite3_finalize(compiledStatement);
sqlite3_close(database);
}
精彩评论