次のコードを使用して、テキストをデータベースに保存したいと考えています。しかし、私はそれを機能させることができません...通常、それはsmtをデータベースに保存する方法です...
-(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);
}
}
しかし、何らかの理由でシミュレータでしか機能しません! しかし、私の電話では機能しません...常にELSEに入ります:
if (sqlite3_step(sqlStatement) == SQLITE_DONE)