如何有选择地替换sqlite行?

时间:2014-10-09 20:21:45

标签: ios xcode sqlite fmdb

在我的情况下2我试图替换qrcode的后续sqlite条目。我想用新的FBid更新行。如何选择然后更换?这与sqlite有关吗?还是我需要在我的应用程序中应用一些逻辑?

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docsPath = [paths objectAtIndex:0];
NSString *path = [docsPath stringByAppendingPathComponent:@"easyKTDB.sqlite"];

FMDatabase *database = [FMDatabase databaseWithPath:path];

[database open];

FMResultSet *results = [database executeQuery:@"select * from ktcode where code = ?;", qrCode];

while ([results next]) {
    ktcode *aktcode = [ktcode new];
    aktcode.ktCODE = [results stringForColumn:@"code"];
    aktcode.ktFBID = [results stringForColumn:@"fbid"];
    aktcode.uniqueID = [results intForColumn:@"id"];

    [ktcodeArray addObject:aktcode];

}

switch (ktcodeArray.count)
{
    case 0 :
        [database executeUpdate:@"insert into ktcode (code, fbid) values (?, ?)", qrCode, FBid];
        NSLog(@"%@ is now registered to %@ - %@", qrCode, userName, FBid);
        break;

    case 1:
        [database executeUpdate:@"insert into ktcode (code, fbid) values (?, ?)", qrCode, FBid];
        NSLog(@"%@ now belongs to %@ - %@", qrCode, userName, FBid);
        break;

    case 2:
        //[database executeUpdate:@""];
         NSLog(@"ktCodeArray: %@", ktcodeArray);
         NSLog(@"%@ has been transfered to %@ - %@", qrCode, userName, FBid);
        break;

    default :
        NSLog(@"Nothing else can be done");
        //delete multiple entries here

1 个答案:

答案 0 :(得分:0)

您似乎在SQLite中寻找REPLACE关键字:https://www.sqlite.org/lang_replace.html

如果您有唯一标识符,则可以执行以下操作:

[database executeUpdate:@"replace into ktcode (id, code, fbid) values (?, ?, ?)", uniqueId, qrCode, FBid];