简体   繁体   English

如何删除sqlite数据库表中的行?

[英]How to delete a row in a sqlite database table?

I am using fmdb for managing my database. 我正在使用fmdb来管理我的数据库。 I could not find any example for deleting a row from a table in fmdb. 我找不到任何从fmdb中的表中删除行的示例。 I tried 我试过了

  NSString *sqlStat=@"DELETE from tableName WHERE id=3";    
  FMResultSet *rs = [database executeQuery:sqlStat];

but its not working because when I checked the total number of entries in table, I am getting the same number as before executing the above statement. 但它不起作用,因为当我检查表中的条目总数时,我得到的数字与执行上述语句之前的数字相同。 So, what is a proper way to delete a row from a table using fmdb? 那么,使用fmdb从表中删除行的正确方法是什么?

FMDB can be a little finicky if you dont pass in the object as an NSNumber. 如果您不将对象作为NSNumber传递,FMDB可能有点挑剔。 This is the supported, and safe way of formatting queries. 这是格式化查询的受支持且安全的方式。

[db executeUpdate:@"DELETE FROM theTable WHERE id = ?", [NSNumber numberWithInt:myObject.id]];

You should replace: 你应该替换:

... [database executeQuery:sqlStat] ... ... [数据库executeQuery:sqlStat] ...

with: 有:

... [database executeUpdate:sqlStat]; ... [database executeUpdate:sqlStat];

Also, try adding: 另外,尝试添加:

[database beginTransaction];

before your CRUD block, and: 在您的CRUD阻止之前,并且:

[database commit];

after executing an update/delete/insert operation. 执行更新/删除/插入操作后。

;) ;)

i also ran into the same symptom. 我也遇到了同样的症状。 and my problem was i didnt call (and caused "out of memory" error) 我的问题是我没有调用(并导致“内存不足”错误)

[db open];

be sure to do this to debug your fmdb issues db.traceExecution=YES; 一定要这样做来调试你的fmdb问题db.traceExecution = YES; db.logsErrors=YES; db.logsErrors = YES;

you need to ensure all the processes 你需要确保所有的过程

NSString *query = @"delete from places where published = 1";
const char *sqlStatement = [query UTF8String];
sqlite3_stmt *compiledStatement;
if(sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK) {
    // Loop through the results and add them to the feeds array
    while(sqlite3_step(compiledStatement) == SQLITE_ROW) {
        // Read the data from the result row
        NSLog(@"result is here");
    }

    // Release the compiled statement from memory
    sqlite3_finalize(compiledStatement);

}
int numberOfEffectedRow = sqlite3_changes(database);
return numberOfEffectedRow; // get number of effected rows

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM