如何在ios中编写更新查询以将多行更新为单个sql查询

时间:2017-03-30 12:27:54

标签: ios objective-c sqlite

我想在Objective-C中编写用于将多行更新为单个SQL查询的更新查询。

有谁知道我怎么能做到这一点?请帮忙。

以下是我的更新查询: -

UPDATE chatDetailTable SET threadId = '584140258203930624', createdByFirstName = 'Poonam', createdByLastName = 'Deshpande', createdById = '201', createdByImgUrl = '', createdByRole = 'teacher', msgType = 'text/plain', msgText = '1111', canEdit = '1', deliverOn = '2017-03-30T12:23:20.73Z', thumbUrl = '', isOwned = '1', replies = '0', mediaPath = '', isFailed = '', read = '1', isSeen = '0', key = '', gmid = '', contact_id = '2', recipientFirstName = 'Indrajit', recipientLastName = 'Joshi', recipientId = '2', recipientImgUrl = '', recipientRole = 'user', isGroup = '0', threadStatus = 'open', mediaUrl = '(null)', status = 'pendingApproval', reviewedById = '0', broadcastId = '584140258136821760' WHERE msgId = '584140258203930625';UPDATE chatDetailTable SET threadId = '584140258266845184', createdByFirstName = 'Poonam', createdByLastName = 'Deshpande', createdById = '201', createdByImgUrl = '', createdByRole = 'teacher', msgType = 'text/plain', msgText = '1111', canEdit = '1', deliverOn = '2017-03-30T12:23:20.73Z', thumbUrl = '', isOwned = '1', replies = '0', mediaPath = '', isFailed = '', read = '1', isSeen = '0', key = '', gmid = '', contact_id = '4', recipientFirstName = 'Hrusikesh', recipientLastName = 'Desai', recipientId = '4', recipientImgUrl = '', recipientRole = 'user', isGroup = '0', threadStatus = 'open', mediaUrl = '(null)', status = 'pendingApproval', reviewedById = '0', broadcastId = '584140258136821760' WHERE msgId = '584140258266845185';UPDATE chatDetailTable SET threadId = '584140258266845186', createdByFirstName = 'Poonam', createdByLastName = 'Deshpande', createdById = '201', createdByImgUrl = '', createdByRole = 'teacher', msgType = 'text/plain', msgText = '1111', canEdit = '1', deliverOn = '2017-03-30T12:23:20.73Z', thumbUrl = '', isOwned = '1', replies = '0', mediaPath = '', isFailed = '', read = '1', isSeen = '0', key = '', gmid = '', contact_id = '1002', recipientFirstName = 'Pramod', recipientLastName = 'Agarwal', recipientId = '1002', recipientImgUrl = '', recipientRole = 'branchAdmin', isGroup = '0', threadStatus = 'open', mediaUrl = '(null)', status = 'pendingApproval', reviewedById = '0', broadcastId = '584140258136821760' WHERE msgId = '584140258333954048';

2 个答案:

答案 0 :(得分:0)

根据您的查询,您必须使用列名设置所有值,并在没有任何WHERE条件的情况下进行更新,这样它将使用提供的新值更新表中可用的所有行。

UPDATE chatDetailTable SET threadId = '584140258203930624', createdByFirstName = 'Poonam',

不包括您不想更新的列。

如果您希望每行都有不同的不同值,那么根据您的API响应,您需要循环数据并按primary_key条件更新。

NSMutableArray *serverResponse;

for (NSDictionary *dictObject in serverResponse) {

    NSString *strUpdateQry = [NSString stringWithFormat:@"UPDATE chatDetailTable SET threadId = %@, createdByFirstName = %@ Where id = %@",dictObject[@"threadId"],dictObject[@"FirstName"],<PRIMARY_KEY>];
    //Execute Update qry with database classes.
}

在上面的代码中,您需要动态传递数据并使用某个主键生成UPDATE查询,并使用您的数据库类执行它。

语法: The basic syntax of UPDATE query with WHERE clause is as follows: UPDATE table_name SET column1 = value1, column2 = value2...., columnN = valueN WHERE [condition]; You can combine N number of conditions using AND or OR operators.

希望这有助于更新您的表格。

答案 1 :(得分:0)

请试试这个

您只能更新要更新的行

"update TableName set fieldName1 = 'value1', fieldName2 = 'value2', fieldName3 ='value3' where id IN(1,2,3)"

IN:仅更新1,2和3个ID行

希望它对你有用