aliyun / aliyun-log-android-sdk

MIT License
180 stars 40 forks source link

SQLITE_MAX_VARIABLE_NUMBER is 999,有可能触发。 #21

Closed captain-miao closed 5 years ago

captain-miao commented 5 years ago

查询 2000 条数据,然后删除,可能触发这个异常:SQLiteException: too many SQL variables,SQLITE_MAX_VARIABLE_NUMBER is 999.

    public void deleteTwoThousandRecords() {
        Query tableSelectQuery = daoSession.getLogEntityDao().queryBuilder().where(LogEntityDao.Properties.Timestamp.le(new Long(new Date().getTime()))).orderAsc(LogEntityDao.Properties.Timestamp).limit(2000).build();
        List<LogEntity> records = tableSelectQuery.list();
        List ids = new ArrayList();
        for(LogEntity log : records){
            ids.add(log.getId());
        }

        DeleteQuery tableDeleteQuery = daoSession.getLogEntityDao().queryBuilder().where(LogEntityDao.Properties.Id.in(ids)).buildDelete();
        tableDeleteQuery.executeDeleteWithoutDetachingEntities();
        daoSession.clear();
        DbUtils.vacuum(daoSession.getDatabase());
    }

Maximum Number Of Host Parameters In A Single SQL Statement

A host parameter is a place-holder in an SQL statement that is filled in using one of the sqlite3_bind_XXXX() interfaces. Many SQL programmers are familiar with using a question mark ("?") as a host parameter. SQLite also supports named host parameters prefaced by ":", "$", or "@" and numbered host parameters of the form "?123".

Each host parameter in an SQLite statement is assigned a number. The numbers normally begin with 1 and increase by one with each new parameter. However, when the "?123" form is used, the host parameter number is the number that follows the question mark.

SQLite allocates space to hold all host parameters between 1 and the largest host parameter number used. Hence, an SQL statement that contains a host parameter like ?1000000000 would require gigabytes of storage. This could easily overwhelm the resources of the host machine. To prevent excessive memory allocations, the maximum value of a host parameter number is SQLITE_MAX_VARIABLE_NUMBER, which defaults to 999.

The maximum host parameter number can be lowered at run-time using the sqlite3_limit(db,SQLITE_LIMIT_VARIABLE_NUMBER,size) interface. sqlite.org

navono commented 3 years ago

这个后来怎么解决的?