SQL删除无法按预期工作

时间:2014-07-13 15:58:47

标签: javascript sqlite titanium

如果我将每个(一次一个)放在相同的代码中,第二个将标题更新为var testTitle,第一个不会删除与WHERE匹配的行/项。

function deleteTableAItem(e) {
    var db3 = Ti.Database.open('thedatabase');

    var getTheID = db3.execute('SELECT ID FROM tableA WHERE myIndexColumn=?', e.itemIndex);
    var theID = getTheID.fieldByName('ID');

    Ti.API.info(tableLinkRef + " " + theID); //they match as intended

    var testTitle = "DID I CHANGE?";

    db3.execute('DELETE FROM tableB WHERE tableLinkRef=? AND tableARef=?',theID,theTableAItemClicked); // fixed by adding another comparison.

    //db3.execute('UPDATE tableB SET titleColumn=? WHERE tableLinkRef=?',testTitle,theID); // this DOES update the titleColumn in matching rows/items when the code is active and the previous is commented out

    db3.execute('DELETE FROM tableA WHERE myIndexColumn=?', e.itemIndex); //this deletes the row from the first table. I want to delete the rows I have associated in tableB as well

    db3.close();
}

3 个答案:

答案 0 :(得分:1)

代码中的DELETE sql看起来很好。您可以打印出生成的整个SQL以查看它是否真的正确,或者查看sqlite是否返回任何错误。

答案 1 :(得分:1)

我不明白为什么你需要两个条件(WHERE tableLinkRef =?AND tableARef =?',theID,theTableAItemClicked)用于DELETE而且只有一条UPDATE标准(WHERE tableLinkRef =?AND tableARef = ?”,theID,theTableAItemClicked)

db3.execute('DELETE FROM tableB WHERE tableLinkRef=? AND tableARef=?',theID,theTableAItemClicked); // fixed by adding another comparison.

//db3.execute('UPDATE tableB SET titleColumn=? WHERE tableLinkRef=?',testTitle,theID); // this DOES update the titleColumn in matching rows/items when the code is active and the previous is commented out

由于您说更新正在处理正确的行,因此您只需从该命令中获取WHERE子句。

db3.execute('DELETE FROM tableB WHERE tableLinkRef=?',theID);

这似乎令人困惑,因为tableLinkRef似乎是对tableA的id的引用,tableARef似乎也描述了这一点。

答案 2 :(得分:0)

如果在更新之前删除记录,则无需更新任何内容。我只想在第一行之前移动第二行,但你真的想删除你刚刚花时间更新的东西吗?