While (sqlite3_step (statement) == SQLITE_ROW) loops never execute

hi everyone I have some problems using sqlite in iOS. I have a selection of data from a database, and then I want to save this data in a variable. But when I use the while(sqlite3_step(statement) == SQLITE_ROW) , the code never executes.

here is my code:

 -(void)retrieveProjectNameFromDb:(NSString *)segmenId{ NSString *query; NSString *nameProjectStr; NSString *dbPath = [[NSBundle mainBundle] pathForResource:@"database" ofType:@"sqlite"]; if (sqlite3_open([dbPath UTF8String], &db) != SQLITE_OK) { sqlite3_close(db); NSAssert(0, @"Database failed to open."); }else { query = [NSString stringWithFormat:@"SELECT remote_projectname, remote_base_uri FROM REMOTE_SETTING WHERE remote_settingid = '%@' ORDER BY remote_projectname", segmenId]; NSLog(@"query : %@", query); } sqlite3_stmt *statement; if(sqlite3_prepare_v2(db, [query UTF8String], -1, &statement, nil)==SQLITE_OK){ NSLog(@"sqlite row : %d", SQLITE_ROW); NSLog(@"sqlite 3 : %d", sqlite3_step(statement)); while (sqlite3_step(statement) == SQLITE_ROW) { char *nameProject = (char *)sqlite3_column_text(statement, 0); if (nameProject == nil) { NSLog(@"UNNAMED"); }else { NSLog(@"else"); nameProjectStr = [NSString stringWithUTF8String:nameProject]; } projectName.text = nameProjectStr; nameProject = nil; } NSLog(@"project name : %@", projectName.text); sqlite3_close(db); } 

}

when i nslog the value, sqlite3_step (statement) always shows 101, and sqlite_row always shows 100. Why can this happen ?? can someone help me please?

Thank you

Hello

-risma -

+4
source share
4 answers

As you said, sqlite3_step (statement) always shows 101, which means that sqlite3_step has completed execution, therefore, your SQL query does not return any rows from the database. I would recommend you check the database first if any record exists in the REMOTE_SETTING table for the remote_settingid you are referring to.

For your reference, I took the following permanent snippet from sqlite.org

 #define SQLITE_DONE 101 /* sqlite_step() has finished executing */ 
+5
source

Delete the NSLog entry in which you have sqlite3_step (). Since you have an NSLog statement executing sqlite3_step (), the record has already passed here. Therefore, the while loop will not execute, since there are no more steps to jump. I think this will help you. :)

+3
source

From your description of the output of the log, I would suggest that your query returns an empty set of rows. However, I see two other problems:

  • You drop the result of your first call to sqlite3_step because you register it, and then call sqlite3_step again. This means that you skip the first line.
  • You should probably not close the database after a single request. You should complete the expression after the request, but only close the database when your application shuts down.

Try:

 if(sqlite3_prepare_v2(db, [query UTF8String], -1, &statement, nil)==SQLITE_OK){ NSLog(@"sqlite row : %d", SQLITE_ROW); int stepResult = sqlite3_step(statement); NSLog(@"sqlite 3 : %d", stepResult); while (stepResult == SQLITE_ROW) { char *nameProject = (char *)sqlite3_column_text(statement, 0); if (nameProject == nil) { NSLog(@"UNNAMED"); }else { NSLog(@"else"); nameProjectStr = [NSString stringWithUTF8String:nameProject]; } projectName.text = nameProjectStr; nameProject = nil; stepResult = sqlite3_step(statement); } NSLog(@"project name : %@", projectName.text); sqlite3_finalize(&statement); } 
0
source

Try something like this.

 sqlite3 *database1; NSString *sqlStatement1 = @""; if(sqlite3_open([databasePath UTF8String], &database1) == SQLITE_OK) { sqlStatement1 = [NSString stringWithString:@"SELECT uniqueid from shortlisted"]; sqlite3_stmt *compiledStatement1; if(sqlite3_prepare_v2(database1, [sqlStatement1 cStringUsingEncoding:NSUTF8StringEncoding], -1, &compiledStatement1, NULL) == SQLITE_OK) { while(sqlite3_step(compiledStatement1) == SQLITE_ROW) { NSString * str = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 1)]; } } sqlite3_finalize(compiledStatement1); sqlite3_close(database1); } 

Also close the database instance whenever you open them. If you do not close the instance, this may cause problems.

Sorry, I am inserting this from my project .. but in fact I'm in a bit of a hurry ... I will change this answer when I get home ...

0
source

Source: https://habr.com/ru/post/1338069/


All Articles