iphone:从sqlite数据库中删除行

时间:2011-05-12 18:20:05

标签: iphone objective-c sqlite

我已经尝试了很多方法来实现这个但是无法从sqlite中删除行,请帮我纠正下面的代码

dbPath = [self applicationDocumentsDirectory];
NSLog(@"%@", dbPath);
dbPath=[dbPath stringByAppendingPathComponent:@"database"];
dbPath=[dbPath stringByAppendingPathComponent:@"OFFENDERSDB.sqlite"];

NSLog(@"database path --> %@", dbPath);

if (sqlite3_open([dbPath UTF8String], &database) == SQLITE_OK) {
    const char *sqlStatement = ("DELETE * from OFFENDERS_LIST where ID=%d ",2);

    sqlite3_stmt *compiledStatement;

    if (sqlite3_prepare(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK) {

        NSLog(@"offender deleted");
    }
    sqlite3_finalize(compiledStatement);
}

提前致谢

1 个答案:

答案 0 :(得分:5)

你尝试过使用:

sqlite3_exec(database, sqlStatement...

使用DELETE语句?

也......它是DELETE FROM ....不是DELETE * FROM ....

从互联网上的另一个地方窃取以下代码以获得更完整的示例......

sqlite3 *db;
int rc;

rc = sqlite3_open( "C:\\MyDatabase", &db );

if ( rc )
{
        sqlite3_close(db);
}
else //Database connection opened successfuly
{
        char *zErrMsg = 0;

        rc = sqlite3_exec( db, "DELETE FROM yourTable", NULL, NULL, &zErrMsg );

        if( rc != SQLITE_OK )
        {
                sqlite3_free( zErrMsg );
        }

        sqlite3_close(db);
}