android - 如何强制 ORM lite 生成正确的 DELETE 查询?
问题描述
在android项目中使用orm lite时出现以下问题:
我有应该从表中删除的行。总是表的所有行。所以查询总是可以像:“DELETE ALL FROM ...”
我正在通过此代码删除行:
Logger.getDataAccessLogger()
.info("clearLocationsPendingToSend...");
final Dao<LocationDto, Integer> pendingLocationsDao = DBConnectionFactory
.getInstance(mCtx).getPendingLocationsDao();
pendingLocationsDao.callBatchTasks(new Callable<Void>() {
@Override
public Void call() throws Exception {
pendingLocationsDao.delete(pendingLocationsDao
.queryForAll());
return null;
}
});
问题是这种方法正在生成这样的 sql 查询:
DELETE FROM `pendingLocations` WHERE `id` IN (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?.........
所以,当有很多行,比如 5000 行时,我会在 sql 中得到太多参数的异常:
android.database.sqlite.SQLiteException: too many SQL variables (code 1): , while compiling: DELETE FROM `pendingLocations` WHERE `id` IN (?,?,?,.....
我应该如何更改代码以强制 orm 不使用此类查询?要使用 DELETE all 而不是这个?
解决方案
pendingLocations
尝试使用此代码从DatabaseTable 类为 LocationDto的表中删除所有数据。
try {
Dao<LocationDto, Integer> pendingLocationsDao = dbHelper.getPendingLocationsDao();
DeleteBuilder<LocationDto, Integer> deleteBuilder = pendingLocationsDao .deleteBuilder();
deleteBuilder.delete();
} catch (SQLException e) {
e.printStackTrace();
}
推荐阅读
- lua - 在指定位置运行 .exe 或 lua 中的文本
- activiti - 如何提高Activity引擎历史表清理的性能
- html - 如何在不使用任何第三方许可软件的情况下以角度创建/制作基于可移动 SVG 的形状和连接器/线?
- oop - 实现登录注册的UML类图
- if-statement - 为什么与 Excel 中的单元格相比,If 语句的范围行为不同
- javascript - 使用 evaluateJavascript 方法在 Cordova 的 WKWebview 上设置本地图像
- node.js - 如何使用 Socket.io 实时更新 API
- django - 如何在 django 中显示不同项目的总和?
- python - 如何将熊猫数据框上的布尔列信息复制到另一个?
- c - Valgrind:将内存标记为只读