Adina Marin
Adina Marin

Reputation: 663

sqlite3_step(statement) == SQLITE_ROW never executed

I have a sqlite in my app , and everything seem to be ok , but i database method the while loop is not executed , and i can't get the result from query. Could somenone help me? Any help will be apreciate !

I also create a copy for databse, as i saw in other answer for other questions .

- (void)viewDidLoad {
    [super viewDidLoad];


    int sqlite3_open(const char *filename, sqlite3 **database);

    sqlite3 *contactDB; //Declare a pointer to sqlite database structure
    NSString *path = [[NSBundle mainBundle] pathForResource:@"verbeGeo" ofType:@"sqlite"];

    if (sqlite3_open([path UTF8String], &contactDB) == SQLITE_OK)
    {
        NSLog(@"DB is open");
    } else {
        NSLog(@"DB can be open");
    }


    NSString *querySQL = @"select id from conjugare where rowid=1";
    const char *query_stmt = [querySQL UTF8String];

    if (sqlite3_prepare_v2(contactDB, query_stmt, -1,
                           &statement, NULL) == SQLITE_OK)
    {
        NSLog(@"Statement prepared successfully");
    } else {
        NSLog(@"Statement preparation failed");

        NSLog(@"Error while creating update statement. '%s'", sqlite3_errmsg(contactDB));
        NSLog(@"%s Prepare failure '%s' (%1d)", __FUNCTION__, sqlite3_errmsg(contactDB), sqlite3_errcode(contactDB));
    }

    sqlite3_step(statement);
    sqlite3_finalize(statement);
    [self database];
       }

-(void)database{

    sqlite3 *contactDB;
    NSString *querySQL = @"select id from conjugare where rowid=1";

    const char *query_stmt = [querySQL UTF8String];

    sqlite3_prepare_v2(contactDB, query_stmt, -1, &statement, NULL);


    while (sqlite3_step(statement) == SQLITE_ROW)
    {
        NSString *idNumber =
        [[NSString alloc] initWithUTF8String:
         (const char *) sqlite3_column_text(statement, 0)];
            }
    sqlite3_finalize(statement);

}

Upvotes: 0

Views: 717

Answers (1)

Phillip Mills
Phillip Mills

Reputation: 31016

Your sqlite3_open statement is initializing the contactDB variable defined in viewDidLoad. The contactDB variable defined in database is local to that method and not initialized.

1) Either make contactDB an instance variable or pass it to database.

2) Check error returns for all calls (e.g. sqlite3_prepare_v2 in the database method).

Upvotes: 2

Related Questions