0
votes

I Spent days trying to fix this code and I give up !! What I'm doing is taking input from user and insert it at database if he press "save" button. when I run this code and After I pressed "save" button. 1- I can't quit from the View ( to another tab ). 2- the inserting didn't applied!!

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDir = [paths objectAtIndex:0];
    NSString *dbPath = [documentsDir stringByAppendingPathComponent:@"CAMAClientDB2.sqlite"];
    sqlite3 *database;


    //Copy the database from the package to the usrrs filesystem
    if (sqlite3_open([dbPath UTF8String], &database)== SQLITE_OK) {//start if 
        NSLog(@"dataBaseOpen");
        const char *sqlStatement = "insert into Location (Latitude,Longitude,LocationName,Tag) VALUES (?,?,?,?)";
        // 
        sqlite3_stmt *compileStatement;

        if (sqlite3_prepare_v2(database, sqlStatement, -1, &compileStatement, NULL) == SQLITE_OK) { // start if 2
            if(SQLITE_DONE!=sqlite3_step(compileStatement))
            {
            sqlite3_bind_double(compileStatement, 1, [latitudeTextField.text doubleValue]);

            sqlite3_bind_double(compileStatement, 2, [longitudeTextField.text doubleValue]);

            sqlite3_bind_text( compileStatement, 3, [lName.text UTF8String], -1, SQLITE_TRANSIENT);

            sqlite3_bind_text(compileStatement, 4, [myText.text UTF8String] ,-1, SQLITE_TRANSIENT);
             NSLog( @" successfully done" );
            if(sqlite3_step(compileStatement) != SQLITE_DONE ) {
                NSLog( @" not done" );
            }  
            }

        }
    }

and I put in appDelegate code for coping in the file manager:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDir = [paths objectAtIndex:0];

NSString *dbPath = [documentsDir stringByAppendingPathComponent:@"CAMAClientDB2.sqlite"];


NSFileManager *fileManager = [NSFileManager defaultManager];

BOOL success = [fileManager fileExistsAtPath:dbPath];

if (success) {

    NSLog(@"we have the database");

} else {

    NSLog(@"we have no database");

    NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"CAMAClientDB2.sqlite"];


    BOOL moved = [fileManager copyItemAtPath:defaultDBPath toPath:dbPath error:nil];

    if (moved) {
        NSLog(@"database copied");
    }



}

Did I miss something !! what is the problem !!

1

1 Answers

0
votes

For your #2 problem: 1. You have an extra sqlite_step call. Your sequence should be:

    sqlite3_prepare_v2()
            sqlite_bind_....
            sqlite_bind_....
            for as many bind's as you need, and then
            sqlite_step()
  1. If the sqlite_step still fails, check the return code and look for guidance in the docs

  2. If your MacOS Application is Sandbox'd, this won't work. There is currently no way to update an sqlite database on it's own. You have to either get user write permission for the directory the sqlite file is in, or you have to put it in a package (which is effectively the same thing)