0

i want to save some text into my database with foloowing code. But i can´t get it work...Useally that is the way to store smt into a Database...

-(void)saveToDB
{
NSFileManager *fileMgr = [NSFileManager defaultManager];
NSString *dbPath = [[[NSBundle mainBundle] resourcePath ]stringByAppendingPathComponent:@"datenbankSpeed"];
BOOL success = [fileMgr fileExistsAtPath:dbPath];
if(!success)
{
    NSLog(@"Cannot locate database file '%@'.", dbPath);
}
if(!(sqlite3_open([dbPath UTF8String], &db) == SQLITE_OK))
{
    NSLog(@"An error has occured: %s", sqlite3_errmsg(db));

}


if (sqlite3_open([dbPath UTF8String], &db) == SQLITE_OK)
{

    NSString *insertSQL = [NSString stringWithFormat:@"INSERT INTO DATEN(typ, date, download, upload, ping, comment) VALUES (\"%@\", \"%@\", \"%@\",\"%@\", \"%@\",\"%@\")", @"1", @"24.09.2012", topDownloadLabel.text, topUploadLabel.text, pingLabel.text, @"Comment"];

    const char *insert_stmt = [insertSQL UTF8String];
    printf("%s\n", insert_stmt);
    NSLog(@"%@", [NSString stringWithUTF8String:insert_stmt]);

    if (sqlite3_prepare_v2(db, insert_stmt, -1, &sqlStatement, NULL) == SQLITE_OK)
    {
        if (sqlite3_step(sqlStatement) == SQLITE_DONE)
        {
            UIAlertView *didFailWithErrorMessage = [[UIAlertView alloc] initWithTitle: @"NSURLConnection " message: @"All good"  delegate: self cancelButtonTitle: @"Ok" otherButtonTitles: nil];
            [didFailWithErrorMessage show];
            [didFailWithErrorMessage release];
        }
        else
        {
            UIAlertView *didFailWithErrorMessage = [[UIAlertView alloc] initWithTitle: @"NSURLConnection " message: @"nada"  delegate: self cancelButtonTitle: @"Ok" otherButtonTitles: nil];
            [didFailWithErrorMessage show];
            [didFailWithErrorMessage release];
        }

    }
            sqlite3_finalize(sqlStatement);
    sqlite3_close(db);
}
}

but for some reason it only works in the simulator! But on my phone it won´t work... It always goes into the ELSE here:

if (sqlite3_step(sqlStatement) == SQLITE_DONE)

4 Answers 4

1

I am unable to find the issue, however i have some suggesions,

Not sure if this is your main problem, but it looks like you're modifying your database right in the bundle, which is too bad. The bundle is going to get erased/replaced when you release an update, for example.

When you app starts, you should look for the database in the documents directory, and if it's not there, (which it won't be the first time you start) you copy your default database from the bundle to the docs directory.

Code:

- (void)applicationDidFinishLaunching:(UIApplication *)application {

    [self copyDatabaseIfNeeded];

    // Configure and show the window
    [window addSubview:[navigationController view]];
    [window makeKeyAndVisible];
}

- (void) copyDatabaseIfNeeded {

    //Using NSFileManager we can perform many file system operations.
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSError *error;
    NSString *dbPath = [self getDBPath];
    BOOL success = [fileManager fileExistsAtPath:dbPath]; 

    if(!success) {

        NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@\"datenbankSpeed.sqlite\"];
        success = [fileManager copyItemAtPath:defaultDBPath toPath:dbPath error:&error];
        NSLog(@\"Database file copied from bundle to %@\", dbPath);

        if (!success) 
            NSAssert1(0, @\"Failed to create writable database file with message '%@'.\", [error localizedDescription]);
    } else {

        NSLog(@\"Database file found at path %@\", dbPath);

    }
}

Device is Case Sensitive. iPhone device care about case of the string,

example "database.sqlite" and "DataBase.sqlite" is different files for device, but the same files for simulator.So please check you query or code string cases.

Also you can log the insertSQL in console, copy it from console and open the Bundle sqlite file and execute the query using execute section. See whats happening :)

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

1 Comment

Yee my database is right in the bundle! So i have to move it to documents?! And then with a reference to my project??
0

The main bundle is read-only on a device. Try moving your database file into the documents directory or somewhere else you can write to.

1 Comment

What exactly you mean by move it to documents?1
0

try like this

  sqlite3_stmt *stmt;

            int x;
            char *update = "insert into meetingPhoneAlarm values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?);";
            x = sqlite3_prepare_v2(database, update, -1, &stmt, nil);
            NSLog(@"x=%d",x);


            if (x == SQLITE_OK) 
            { 
                sqlite3_bind_text(stmt, 1, NULL,-1, NULL);  
                sqlite3_bind_text(stmt, 2, [txt_subject.text UTF8String],-1, NULL);
                sqlite3_bind_text(stmt, 3, [txt_location.text UTF8String],-1, NULL);
                sqlite3_bind_text(stmt, 4, [txt_meetwith.text UTF8String],-1, NULL);
                sqlite3_bind_text(stmt, 5, [btn_partyDate.titleLabel.text UTF8String],-1, NULL);
                sqlite3_bind_text(stmt, 6, [btn_partyTime.titleLabel.text UTF8String],-1, NULL);
                sqlite3_bind_text(stmt, 7, [txt_notes.text UTF8String],-1, NULL);
                sqlite3_bind_text(stmt, 8, [btn_snooze.titleLabel.text UTF8String],-1, NULL);
                sqlite3_bind_text(stmt, 9, [string_ascending UTF8String],-1, NULL);
                sqlite3_bind_text(stmt, 10, [string_volume UTF8String],-1, NULL);
                sqlite3_bind_text(stmt, 11, [string_alarmsound UTF8String],-1, NULL);
                sqlite3_bind_text(stmt, 12,[btn_alarmDate.titleLabel.text UTF8String],-1, NULL);
                sqlite3_bind_text(stmt, 13,[btn_AMPM.titleLabel.text UTF8String],-1, NULL);
                sqlite3_bind_text(stmt, 14,[string_vibration UTF8String],-1, NULL);
                sqlite3_bind_text(stmt, 15,[string_repeatOption UTF8String],-1, NULL);

            }       
            if (sqlite3_step(stmt) != SQLITE_DONE){}
            //  NSLog(@"Error: %@",errorMsg); 
            sqlite3_finalize(stmt);

Comments

0

Ok got it by moving the Database to the Documents Directory! Thanks guys...

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.