Vlad Z.
Vlad Z.

Reputation: 3451

sqlite3_prepare error #26

I have following code (modified code from this tutorial):

    -(NSMutableArray *) getChampionDatabase
{
    NSMutableArray *championsArray = [[NSMutableArray alloc] init];

    @try {

    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSString *databasePath = [[NSBundle mainBundle]pathForResource: @"Champions Database" ofType:@"sqlite"];
    BOOL success = [fileManager fileExistsAtPath:databasePath];

    if (!success)
    {
        NSLog(@"cannot connect to Database! at filepath %@",databasePath);
    }

    if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK)
    {
    const char *sql = "SELECT CHAMPION_ID,CHAMPION_NAME,CHAMPION_IMG FROM CHAMPIONS";
    sqlite3_stmt *sqlStatement;

    if(sqlite3_prepare(database, sql, -1, &sqlStatement, NULL) == SQLITE_OK)
    {
        while (sqlite3_step(sqlStatement)==SQLITE_ROW) {
            championList *ChampionList = [[championList alloc]init];
            ChampionList.championId = [NSString stringWithUTF8String:(char *) sqlite3_column_text(sqlStatement,0)];
            ChampionList.championName = [NSString stringWithUTF8String:(char *) sqlite3_column_text(sqlStatement,1)];
            ChampionList.championImage = [NSString stringWithUTF8String:(char *) sqlite3_column_text(sqlStatement, 2)];
            [championsArray addObject:ChampionList];
    }
    }
        else
        {
            NSLog(@"problem with database prepare");
        }
    }
        else
        {
            NSLog(@"problem with database openning");
        }
    }

@catch (NSException *exception)
    {
    NSLog(@"An exception occured: %@", [exception reason]);
    }
@finally
    {
    return championsArray;
    }
}

After running this code i always have output : "problem with database prepare"

Then i tried to check error number of sqlite3_prepare by following code:

int ret = sqlite3_prepare(database, sql, -1, &sqlStatement, NULL);
            if (ret != SQLITE_OK) {
                NSLog(@"Error calling sqlite3_prepare: %d", ret);
            }

output: "Error calling sqlite3_prepare: 26"

error 26 - /*File opened that is not a database file */


File have sqlite 3 version

how is this possible? file extension is .sqlite and i can modify it in sqlite manager

Upvotes: 0

Views: 789

Answers (1)

tiguero
tiguero

Reputation: 11537

Make sure you added your DB to the ressource bundle of your project and don't put any space in your name of your DB. For example use a name like champion_db.sqlite.

Upvotes: 2

Related Questions