0

My problem is that i cant get the values from the select query as it executes 101 error.

I think that my problem is that it has to be with the quotes in select statement

sqlite3 * database ;

NSString * path = [[[ NSBundle mainBundle ]resourcePath ] stringByAppendingPathComponent : @"cars.sqlite"];
int returnCode = sqlite3_open ([ path cStringUsingEncoding : NSUTF8StringEncoding], &database) ;

sqlite3_stmt * statement ;
char * st ;
NSString *querySQL = [NSString stringWithFormat: @"select brand,model from cars where brand=%@;", tmpbrand];

const char *query_stmt = [querySQL UTF8String];
st = sqlite3_mprintf (query_stmt);

if(sqlite3_prepare_v2 ( database , query_stmt , -1 ,& statement ,NULL )==SQLITE_OK){
returnCode = sqlite3_step ( statement ) ; // this statement returns a record

while ( returnCode == SQLITE_ROW ) // while there are rows
{
    models*tmp=[[models alloc]init];

    char* n=(char*)sqlite3_column_text(statement,0);
    tmp=[NSString stringWithCString:n encoding:NSUTF8StringEncoding];
    char* model=(char*)sqlite3_column_text(statement, 1);
    tmp.model=[NSString stringWithCString:model encoding:NSUTF8StringEncoding];


    [data addObject:tmp];

    returnCode = sqlite3_step ( statement ) ; // this statement returns a record

}
    sqlite3_finalize(statement);
}

}

1 Answer 1

1

A quick look at the documentation shows that error 101 means "done".

The SQLITE_DONE result code indicates that an operation has completed.

If you're not getting the rows you expect, you're right, it's probably your query. To make it work you probably do need to put single quotes around the '%@'.

However, this is not the right way of doing it. You should be using prepared statements. That is, your query looks like select brand,model from cars where brand=? and you fill in the values using the bind function.

Sign up to request clarification or add additional context in comments.

2 Comments

I tried to do this with single quotation and I couldnt manage to make it work.. If its possible to show me how to do it by using prepared statements.. Im quite a begginer at this..
Here's the first link in a Google search for "objective c sqlite prepared statement": stackoverflow.com/q/1187154/2998

Your Answer

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