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)
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:
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 🙂