首页 > 解决方案 > 如何强制 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 而不是这个?

标签: androidsqlormlite

解决方案


pendingLocations尝试使用此代码从DatabaseTable 类为 LocationDto的表中删除所有数据。

try {
       Dao<LocationDto, Integer> pendingLocationsDao = dbHelper.getPendingLocationsDao();
        DeleteBuilder<LocationDto, Integer> deleteBuilder = pendingLocationsDao .deleteBuilder();
        deleteBuilder.delete();
    } catch (SQLException e) {
        e.printStackTrace();
    }

推荐阅读