使用 OrmLite 在 SqlLite 中组合删除语句几乎没有帮助

发布于 2024-11-19 04:59:34 字数 365 浏览 6 评论 0原文

我无法在 Android 应用程序中组合删除语句(我正在使用 OrmLite)。

我有一张桌子,里面装满了记录。其中两个字段是“dateCreated”(类型 Date)和“imageSize”(类型 int)。在我的代码中,我有一个方法free(int size)。此方法告诉我,我必须从表中删除总和为“imageSize”<= 大小的最旧记录。

例如..我得到参数1000。每条记录的值可以说是100。这意味着我必须删除10条最旧的记录。

有人可以为我提供最佳的原始 ​​SQL 语句,甚至更好的 OrmLite 代码吗?

我将不胜感激。

I have trouble putting together a delete statement in my Android application (I am using OrmLite).

I have a table filled with records. Two of the fields are "dateCreated" (type Date) and "imageSize" (type int). In my code I have a method free(int size). This method tells me that I have to delete oldest records from my table that sum "imageSize" <= size.

For instance .. i get parameter 1000. Each record has value lets say 100. That means i have to delete 10 of the oldest records.

Can some one please provide me with optimal raw SQL statement or even better an OrmLite code for this?

I would be most gratefull.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

山色无中 2024-11-26 04:59:34

不幸的是,您无法使用单个 SQL 语句来完成此操作。没有办法说

选择记录,直到它们的总和小于 X

您可以执行多个查询,直到找到总和小于 X 的最旧记录,但这需要多次单独的 SQL 调用。

我建议选择最后 X 个图像及其大小,然后使用 DELETE ... IN ... 删除正确数量的图像。这是伪代码:

while (true) {
   SELECT id, imageSize FROM yourtable ORDER BY dateCreated DESC LIMIT 10;
   find the images from the bottom whose sum(imageSize) <= parameter
   delete the found images
   break if you exceed the parameter otherwise loop and get the next 10
}

Unfortunately, you can't do this with a single SQL statement. There is no way to say

select records until their sum is less than X

You could doing multiple queries until you found the oldest records whose sum is less than X but it would take a number of separate SQL calls.

I'd recommend selecting the last X images with their sizes and then doing a delete of the right number of images using a DELETE ... IN .... Here's the pseudo code:

while (true) {
   SELECT id, imageSize FROM yourtable ORDER BY dateCreated DESC LIMIT 10;
   find the images from the bottom whose sum(imageSize) <= parameter
   delete the found images
   break if you exceed the parameter otherwise loop and get the next 10
}
鹿童谣 2024-11-26 04:59:34

试试这个,

DELETE FROM yourtable WHERE imageSize <= (SELECT SUM(ImageSize) FROM yourtable)

为你的函数使用相同的参数

try this,

DELETE FROM yourtable WHERE imageSize <= (SELECT SUM(ImageSize) FROM yourtable)

Use the same parameter for your function

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文