【问题标题】:SQL delete not working as expectedSQL 删除未按预期工作
【发布时间】:2014-07-13 15:58:47
【问题描述】:

如果我将每一个(一次一个)放在同一个代码中,第二个会将标题更新为 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();
}

【问题讨论】:

  • 您的代码真的是这样吗?如果是这样,它根本不会做任何事情。
  • 你需要展示实际代码。
  • 停止尝试描述你在做什么,你需要显示代码
  • 会很抱歉。正在努力。
  • TableAItemClicked 的值是多少?它没有传递给函数,所以它是未定义的吗?可能意味着删除将永远找不到要删除的行。

标签: javascript sqlite titanium


【解决方案1】:

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

【讨论】:

  • 他没有使用 MySQL,他使用的是 SQLite。
【解决方案2】:

我不明白为什么您需要两条标准(WHERE tableLinkRef=?AND tableARef=?'、theID、theTableAItemClicked)来进行 DELETE,而只需要一条标准来进行更新(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 似乎也描述了它。

【讨论】:

  • tableB 中的项目组链接到 tableA 中的项目,所有这些项目都需要与单击列表事件匹配。假设来自 tableA 的老师有来自 tableB 的孩子坐在桌子位置 1、2、3。其他教师也使用课桌位置 1、2、3,因为学校对所有班级使用相同的列表(教室)。所以我需要老师加上课桌位置。我的变量临时名称不是很好。
【解决方案3】:

如果您在更新之前删除了记录,则没有任何内容需要更新。我只想将第二行移到第一行之前,但您真的要删除刚刚花时间更新的内容吗?

【讨论】:

  • 对不起,我根本不想更新它,这严格来说是一个测试,看看 UPDATE 是否使用参数工作。 THx
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-05-15
  • 2020-03-12
  • 2011-08-06
  • 1970-01-01
  • 2014-07-26
  • 2019-06-22
相关资源
最近更新 更多