anjum
anjum

Reputation: 556

Unknown error while executing select statement

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?

Upvotes: 1

Views: 2434

Answers (4)

anjum
anjum

Reputation: 556

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!!!

Upvotes: 1

Deepesh
Deepesh

Reputation: 8053

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)];

Upvotes: 1

Charan
Charan

Reputation: 4946

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);

Upvotes: 1

mhunturk
mhunturk

Reputation: 296

    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);

Upvotes: 2

Related Questions