我想使用下面的代碼將一些文本保存到我的數據庫中。但我不能得到它的工作... Useally那就是SMT存儲到數據庫的方式......將文本保存到sqlite3
-(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);
}
}
但由於某些原因,只在模擬器中工作!但在我的手機也不需額外的工作......它總是進入人在這裏:
if (sqlite3_step(sqlStatement) == SQLITE_DONE)
Ye我的數據庫是正確的捆綁!所以我必須將它移動到文檔?!然後參考我的項目? – davidOhara