xcode sqlite删除一个表中的行,其中EXISTS在另一个表中

时间:2012-01-27 16:13:03

标签: objective-c ios ipad sqlite

当我运行以下代码从MAIN.NOTES表中删除一行时,如果tasks.todoDone没有匹配,则MAIN.NOTES中不会删除任何内容。但是如果一个tasks.todoDone大于'',那么即使task.todoNod不小于点头,MAIN.NOTES中的所有行都会被删除。

代码的第二部分,从MAIN.TASKS中删除正常工作,只删除与两个测试匹配的行。

我的EXISTS中有什么问题(选择*行?

if (sqlite3_open(dbpath, &tasksDB) == SQLITE_OK) {
    NSString *querySQLNotes = [NSString stringWithFormat:@"DELETE FROM MAIN.NOTES WHERE EXISTS (select * from MAIN.TASKS where tasks.todoNod <'%i' and tasks.todoDone > '')",nod];

    char *errMsg;
    const char *query_stmtN = [querySQLNotes UTF8String];// this should delete the notes for task that will be deleted next
    if (sqlite3_exec(tasksDB, query_stmtN, NULL, NULL, &errMsg) == SQLITE_OK) {
        int tc = sqlite3_changes(tasksDB);
        NSLog(@"total count of deleted notes %i",tc);
    }

    NSString *querySQL = [NSString stringWithFormat:@"DELETE FROM MAIN.TASKS WHERE todoNod <'%i' and todoDone > ''",nod];

    const char *query_stmt = [querySQL UTF8String];        
    if (sqlite3_prepare_v2(tasksDB, query_stmt, -1, &statement, NULL) == SQLITE_OK) {
        if (sqlite3_step(statement) == SQLITE_DONE) {
            NSLog(@"Match found and deleted");
        } else {
            NSLog(@"Match found and not deleted");
        }
        sqlite3_finalize(statement);  
        int tc = sqlite3_changes(tasksDB);
        NSLog(@"total count of deleted tasks %i",tc);
    }
    sqlite3_close(tasksDB);
} 

由于

0 个答案:

没有答案