How to delete a row in a sqlite database table?

不想你离开。 提交于 2019-12-22 05:13:29

问题


I am using fmdb for managing my database. I could not find any example for deleting a row from a table in 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?


回答1:


FMDB can be a little finicky if you dont pass in the object as an NSNumber. This is the supported, and safe way of formatting queries.

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



回答2:


You should replace:

... [database executeQuery:sqlStat] ...

with:

... [database executeUpdate:sqlStat];

Also, try adding:

[database beginTransaction];

before your CRUD block, and:

[database commit];

after executing an update/delete/insert operation.

;)




回答3:


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; db.logsErrors=YES;




回答4:


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


来源:https://stackoverflow.com/questions/1696044/how-to-delete-a-row-in-a-sqlite-database-table

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!