0

For some reason, the following code is not storing any data in my sqlite database. The return code is SQLITE_DONE (101) so it is not giving me any error messages. The method is being called a number of times to populate a few rows in the database. Can anyone see where i'm going wrong?

- (void)storePersonInDatabase:(Person *)person {
    const char *sql = "INSERT INTO PERSON (ID, NAME, NOTES, ADDRESS, PROMOTED, VEGETARIAN) values (?, ?, ?, ?, ?, ?)";
    sqlite3_stmt *statement;

    // Prepare the data to bind.
    NSData *imageData = person.imageData;
    NSString *personId = [person.personId stringValue];
    NSString *personName = person.name;
    NSString *address = person.address;
    NSString *notes = person.notes;
    NSString *isVegetarian = (person.isVegetarian) ? @"1" : @"0";
    NSString *isPromoted = (person.isPromoted) ? @"1" : @"0";

    // Prepare the statement.
    if (sqlite3_prepare_v2(database, sql, -1, &statement, NULL) == SQLITE_OK) {
        // Bind the parameters (note that these use a 1-based index, not 0).
        sqlite3_bind_text(statement, 1, [personId UTF8String], -1, SQLITE_TRANSIENT);
        sqlite3_bind_text(statement, 2, [personName UTF8String], -1, SQLITE_TRANSIENT);
        sqlite3_bind_text(statement, 3, [notes UTF8String], -1, SQLITE_TRANSIENT);
        sqlite3_bind_text(statement, 4, [address UTF8String], -1, SQLITE_TRANSIENT);
        sqlite3_bind_text(statement, 5, [isPromoted UTF8String], -1, SQLITE_TRANSIENT);
        sqlite3_bind_text(statement, 6, [isVegetarian UTF8String], -1, SQLITE_TRANSIENT);

    }

    // Execute the statement.
    int returnCode = sqlite3_step(statement);
    if (returnCode != SQLITE_DONE) {
        // error handling...
        NSLog(@"An error occoured");
    }

//This is how I set up the db

NSString *sqliteDb = [[NSBundle mainBundle] pathForResource:@"Persons" ofType:@"sqlite"];
if(sqlite3_open([sqliteDb UTF8String], &database) != SQLITE_OK){
    NSLog(@"Unable to open database");
}
12
  • sqlite.org/c3ref/c_abort.html Commented May 17, 2013 at 8:50
  • Instead of binding Data to SQ query pass data directly to Inser query ... Commented May 17, 2013 at 8:51
  • Where is you SQLite data files stored? Commented May 17, 2013 at 8:51
  • I will also need to store an image in this row so I was trying to avoid putting the strings directly in the query. The SQLite data file is stored in the root directory of the project. I am able to read from it no problem. Commented May 17, 2013 at 8:54
  • are you getting inserted value in SELECT query ? Commented May 17, 2013 at 8:59

2 Answers 2

2

As a few people have suggested, you need to copy the database to a location outside of your project bundle. There you can read and write as you wish, otherwise you are essentially just creating new databases every time you try to run your 'storePersonInDatabase' method.

You should perform that same check that Daij-Djan provided before trying to interact with your database. You should also save the location or name of your database somewhere for easy access and file checking.

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

Comments

1

you show how you try to open a sqlite file in your app bundle

you cant write to your app bundle

the app bundle is read only

you need to copy it somewhere you can write to. It sounds like it should go to library/documents.

the pseudo code is:

if(!sqlite_already_in_library) {
    [file_manager copyFileFrom:sqlite-in-bundle to:sqlite-in-library_path];
}

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.