SpokaneDude
SpokaneDude

Reputation: 4984

error in sqlite3_prepare_v2 statement

I have this code, where the sqlite3_prepare_v2 statement is not returning OK... any ideas why?

    // Setup the SQL Statement and compile it for faster access
    const char *sqlStatement = "SELECT * FROM CardData";
    sqlite3_stmt *compiledStatement;

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *saveDirectory = [paths objectAtIndex:0];
    NSString *databasePath = [saveDirectory stringByAppendingPathComponent:@"ppcipher.s3db"];

//  [dm openDatabaseWithPassword:password];  //  open the d/b   

    sqlite3 *database;
    if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK) {
        sqlite3_key(database,password,9);
        if(sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK) {

Upvotes: 0

Views: 1150

Answers (2)

MikeIsrael
MikeIsrael

Reputation: 2889

I believe the third argument in sqlite3_prepare_v2 is

"Maximum length of zSql in bytes"

and you have -1 set there. Maybe try setting it to

if(sqlite3_prepare_v2(database, sqlStatement, strlen(sqlStatement), &compiledStatement, NULL) == SQLITE_OK) {

Upvotes: 0

Doug Currie
Doug Currie

Reputation: 41220

Here are some possibilities:

  1. The databasePath is incorrect, and you are creating a new database rather than opening the one you think; see sqlite3_open_v2 and SQLITE_OPEN_READONLY to detect this

  2. The schema does not contain a CardData table. Try printing out the schema with select * from sqlite_master;

  3. The password is incorrect, and the database cannot be decrypted

Upvotes: 1

Related Questions