0

I have this simple function in my application :

-(NSMutableArray *)SelectProductID:(NSMutableArray *)arr
{
    NSLog(@"----------------");

    sqlite3_stmt *statement;

    NSMutableArray *arrPordID = [[NSMutableArray alloc]init];

    @try
    {

        //Get productID
        for(NSString *strSubProductID in arr)
        {


            NSString *s = [NSString stringWithFormat:@"SELECT ProductID FROM SubProducttable where SubProductID=%@",strSubProductID];

            const char *sql = [s cStringUsingEncoding:NSASCIIStringEncoding];

            if (sqlite3_prepare_v2(database, [s cStringUsingEncoding:NSUTF8StringEncoding], -1, &statement, NULL) == SQLITE_OK) {

                while (sqlite3_step(statement) == SQLITE_ROW){

                    char *dbString;

                    dbString = (char *)sqlite3_column_text(statement, 0);
                    NSString *pID = (dbString) ? [NSString stringWithUTF8String:dbString] : @"";
                    [arrPordID addObject:pID];

                }

            }             

        }
    }
    @catch (NSException *exception) {

        @throw exception;
    }
    @finally {

        sqlite3_finalize(statement);
    }
    return arrPordID;

}

I am encountering a strange problem here. When application reaches while (sqlite3_step(statement) == SQLITE_ROW){, loop is never entered. I don't know why. I executed the same query in SQLite manager (when application is not running). And I get result as a single one. The result I get is 2. But here I am getting nothing.

And yes, I always close the database in SQLite manager whenever I run my application. I have also cleaned the application, restarted XCode, and removed the application from simulator. But no success.

Also I saw a strange thing during debugging. While debugging, sqlite3_stmt *statement is always skipped. Is this the reason I am not getting any result?

2
  • What type is your SubProductID in SubProducttable? Commented Sep 13, 2011 at 17:54
  • sqlite3_errcode() and sqlite3_errmsg() are your friends. Ask them for help. Commented Sep 13, 2011 at 18:07

2 Answers 2

1

Have you tried subproductId in single quotes?

NSString *s = [NSString stringWithFormat:@"SELECT ProductID FROM SubProducttable where SubProductID='%@'",strSubProductID];
Sign up to request clarification or add additional context in comments.

1 Comment

Very strange. I never have this problem. I never use single quotes and still everything is fine. Don't know why this time it didn't work..!!
0
if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK) {
    // Setup the SQL Statement and compile it for faster access
    const char *sqlQuery = @"SELECT ProductID FROM SubProducttable where SubProductID=%@",strSubProductID;
    sqlite3_stmt *statement;
    if(sqlite3_prepare_v2(database, statement, -1, &sqlQuery, NULL) == SQLITE_OK) {
        while(sqlite3_step(sqlQuery ) == SQLITE_ROW) {
            // Read the data and add to your array object
        }
    }
    // Release the compiled statement from memory
    sqlite3_finalize(statement);

}
sqlite3_close(database)

1 Comment

I have used separate function where I have opened and closed database. It is not the issue. Because before the above function I have a similar SQLite query in a function. It runs successfully.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.