我想使用以下代码将一些文本保存到我的数据库中。但我无法让它工作......通常这是将 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)