2011-04-07 115 views
0

当我在iPhone模拟器中运行此代码时,我无法从数据库中删除记录。如果我在SQLite数据库浏览器中运行这个查询,它效果很好。这里是我的代码无法从SQLite中删除记录iPhone

SQLiteDB *sqliteConnect=[[SQLiteDB alloc] init]; 
sqlite3 *database; 

if(sqlite3_open([sqliteConnect.databasePath UTF8String], &database)==SQLITE_OK) 
{ 
    sqlite3_stmt *compiledstatement; 

    const char *sqlstmt="delete from searchHistory where id = (select min(id) from searchHistory) "; 

    if(sqlite3_prepare_v2(database, sqlstmt, -1, &compiledstatement, NULL)==SQLITE_OK) 
    { 
     sqlite3_step(compiledstatement); 

     if(SQLITE_DONE != sqlite3_step(compiledstatement)) 

      NSAssert1(0,@"Error while creating delete statement => %s",sqlite3_errmsg(database)); 
    } 
    NSLog(@"delete DONE"); 
    sqlite3_finalize(compiledstatement); 
} 
sqlite3_close(database); 

当我运行使用调试器,我得到以下错误=>

2011-04-08 08:42:34.049 MyApp[1838:20b] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Error while creating delete statement => not an error' 

SQLiteDB是我的类,它包含init的& checkAndCopyDB方法的逻辑。我的数据库被复制到文档目录中。其他数据库操作正常。请帮帮我。谢谢

回答

6

你打给sqlite3_step两次?

试试这个:

{ 
    int result = sqlite3_step(compiledstatement); 

    if(SQLITE_DONE != result) 

     NSAssert1(0,@"Error while creating delete statement => %s",sqlite3_errmsg(database)); 
} 
+0

=>谢谢...它完美.. – iOSAppDev 2011-04-07 16:52:54

1

也许尝试:

if(SQLITE_DONE != sqlite3_step(compiledstatement)) 
    NSAssert1(0,@"Error while creating delete statement => %s",sqlite3_errmsg(database)); 
+0

感谢您的答复... deanWombourne解决方案为我工作。 – iOSAppDev 2011-04-07 16:52:03