1

In AppDelegate I have retrieve the path of my database:

NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentDirectory=[paths objectAtIndex:0];
self.sqlFile=[[NSString alloc]init];
self.sqlFile=[documentDirectory stringByAppendingPathComponent:@"AnimalData.sqlite"];

In one of my viewcontroller where I have to select values from the database, the code:

NSString *trimString=[appDelegate.mainBreedSelected stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];

NSString *selectSQL=[NSString stringWithFormat:@"select breed from AnimalsTable where mainBreed=\"%@\"",trimString];
sqlite3_stmt *selectStatement;
const char *select_stmt=[selectSQL UTF8String];
if (sqlite3_open([appDelegate.sqlFile UTF8String],&database)==SQLITE_OK)
{
    sqlite3_prepare_v2(database,select_stmt,-1,&selectStatement,NULL);
    if (sqlite3_step(selectStatement)==SQLITE_DONE)
    {
        while(sqlite3_step(selectStatement) == SQLITE_ROW)
        {
            NSString *animalBreed = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectStatement, 2)];
            NSLog(@"Breed:%@",animalBreed);
        }
    }
    else
        NSAssert1(0, @"Error while deleting. '%s'", sqlite3_errmsg(database));
    sqlite3_finalize(selectStatement);
    sqlite3_close(database);
}
else
    NSLog(@"Database cannot be opened");

I am getting error as:

* Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Error while selecting. 'unknown error''

How to solve this issue?

1
  • I have made another change just to see if it would work But I am getting new error Changes is NSString selectSQL=[NSString stringWithFormat:@"select breed from AnimalsTable where mainBreed= %@",trimString]; And New error for this is:** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Error while deleting. 'no such column: Sheep'' Commented Apr 17, 2012 at 8:38

4 Answers 4

2
    select breed from AnimalsTable where mainBreed=\"%@\"",trimString

you are selecting one column in the table but

    NSString *animalBreed = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectStatement, 2);

At there you want to get the 3rd column so change 2 to 0 like below:

    NSString *animalBreed = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectStatement, 0);
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your suggestion, I have made changes in my code. But still the problem is not solved. The problem is in prepare statement.
1

it should not be

sqlite3_prepare_v2(database,select_stmt,-1,&selectStatement,NULL);

it should be

sqlite3_prepare_v2(database,[selectSQL UTF8String],-1,&selectStatement,NULL);

1 Comment

thanks, But problem is not solved with the changes you have suggested.
1

try this and check your database path :-

NSString *trimString=[appDelegate.mainBreedSelected stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];

sqlite3 *database;

NSString *selectSQL=[NSString stringWithFormat:@"select breed from AnimalsTable where mainBreed ='%@'",trimString];
sqlite3_stmt *selectStatement;
if (sqlite3_open([appDelegate.sqlFile UTF8String],&database)==SQLITE_OK) 
   {
        sqlite3_prepare_v2(database,[selectSQL cStringUsingEncoding:NSUTF8StringEncoding],-1,&selectStatement,NULL);
        if (sqlite3_step(selectStatement)==SQLITE_DONE) 
            {
         while(sqlite3_step(selectStatement) == SQLITE_ROW) 
             {
            NSString *animalBreed = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectStatement, 0)];
            NSLog(@"Breed:%@",animalBreed);
             }
        }
        else
         NSAssert1(0, @"Error while deleting. '%s'", sqlite3_errmsg(database));
         sqlite3_finalize(selectStatement);
        sqlite3_close(database);
    }
    else
     NSLog(@"Database cannot be opened");

and check your column no at fetch database sqlite3_column_text(selectStatement, 0)];

4 Comments

you have probelem in this line please use this :- NSString *animalBreed = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectStatement, 0)];
I have done that changes too. I have retrieve my database path in appDelegate. And used in another view for inserting values it works fine, I dont understand whats the problem with select statement.
try this:- NSString *selectSQL=[NSString stringWithFormat:@"select breed from AnimalsTable where mainBreed ='%@'",trimString];
please nslog this line [appDelegate.sqlFile UTF8String] and check
1

I have changed the code:

if (sqlite3_open([appDelegate.sqlFile UTF8String], &database) == SQLITE_OK) {
    NSLog(@"%@",appDelegate.sqlFile);
    NSString *trimString=[appDelegate.mainBreedSelected stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    NSString *select_sql=[NSString stringWithFormat:@"select breed from AnimalsTable where mainBreed='%@'",trimString];
    const char *sql = [select_sql UTF8String];
    sqlite3_stmt *selectstmt;
    if(sqlite3_prepare_v2(database, sql, -1, &selectstmt, NULL) == SQLITE_OK) {

        while(sqlite3_step(selectstmt) == SQLITE_ROW) {

            NSString *animalBreed = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 0)];
            NSLog(@"Breed:%@",animalBreed);
        }
    }
}
else
    sqlite3_close(database); 

And it's working! Thank god!!!

Comments

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.