如何从sqlite iOS中的一个表中删除多行? [英] How to delete Multiple row from one table in sqlite iOS?

查看:53
本文介绍了如何从sqlite iOS中的一个表中删除多行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表,sqlite 中的 MessageTable.我想从中删除选定的行.我怎样才能做到这一点.我已经努力搜索,但没有得到任何合适的答案.任何帮助,将不胜感激.TIA.

I have a table, MessageTable in sqlite.I want to delete selected rows from it. How can I achieve that. I have searched hard but didn't get any suitable answer. Any help would be appreciated. TIA.

推荐答案

如果您想从表中删除多个选定的行,IN 子句将是最佳选择之一.删除行时,您需要注意条件中要提及的列类型.

If you want to delete the multiple selected rows from a table, IN clause will be the one of the best choice. While deleting the rows you need to take care what kind of column you are going to mention in the condition.

以下是提及如何从表中删除行集的各种数据类型.

Here are the various data types mentioned how to delete the set of rows from a table.

对于数字:

NSString *query = [NSString stringWithFormat:@"DELETE FROM Messages WHERE ID IN (101,102,103,104)];;

这里对于数字或整数,我们可以将数字数组直接设置为查询,也可以设置为逗号分隔值.

Here for Numbers or Integers, we can set the Array of Numbers directly to the query or we can set as comma separated values.

在查询中容纳数组.

NSArray *idArraysToDelete = @[101, 102, 103, 104];
NSString *query = [NSString stringWithFormat:@"DELETE FROM Messages WHERE (ID IN %@)", idArraysToDelete];

对于字符串:

假设您要删除少数选定人员的消息,

Lets say you want to delete the messages of few selected peoples,

NSMutableArray *parameters = [NSMutableArray new];
for (id avatar in avatarNames) {
    if (avatar != [NSNull null]) {
        if ([avatar isKindOfClass:[NSString class]]) {
            NSString *strValue = [NSString stringWithFormat:@"'%@'", avatar];
            [parameters addObject:strValue];
        } else {
            [parameters addObject:bindVal];
        }
    }
}
NSString *deleteQuery = [NSString stringWithFormat:@"DELETE FROM Messages WHERE AVATAR IN (%@)", tableName, columnName, [parameters componentsJoinedByString:@","]];

这里对于字符串,SQLite 总是假设 VARCHAR 或 String 可能包含 UNICode 字符,因此在设置参数时,我们为每个值设置单引号('').SQLite 将假定单引号中提到的值是行值.一般来说,对于字符串的任何条件检查,单引号都是强制性的

Here for Strings, SQLite always assumes the VARCHAR or String may contain UNICode characters, So while setting the parameters we are setting single quote('') for each values. SQLite will assume the value mentioned in the single quotes are the row values. In general for any condition check on strings, single quotes are mandatory

这篇关于如何从sqlite iOS中的一个表中删除多行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆