0

我有一个长期运行的任务,我在我的活动上运行 onResume。该任务涉及查询数据库,然后解密一些数据,然后手动对其进行排序,然后使用事务更新数据库中的排序顺序。

当我从活动主 UI 线程运行它时,这工作正常,但是当我从 AsyncTask 中执行相同的任务时,我总是会收到以下错误:

  • I/SqliteDatabaseCpp(5166): sqlite 返回: 错误代码 = 1, msg = no such table: Household, db=/mnt/sdcard/myDatabase.db
  • 没有这样的表:编译时:没有这样的表:家庭:,编译时:SELECT DISTINCT street FROM Household WHERE street IS NOT NULL AND LENGTH(LTRIM(RTRIM(street)))>0

我知道数据库存在并且 SQL 语句很好,因为它在 AsyncTask 之外运行良好。从 AsyncTask 中访问我的数据库是否会导致问题?

我在下面的“SELECT DISTINCT”原始查询中遇到错误。

private boolean update_street_sort_order() {
    boolean returnValue = false;
    DBUtilities objDbUtil = null;
    Cursor cCases = null;
    final String SORT_ATTRIBUTE = "street_sort_order";
    final int STREET_INDEX = 0;
    final int ENCRYPTED_STREET = 0;
    final int DECRYPTED_STREET = 1;
    try {
        objDbUtil = DBUtilities.getInstance(this);
        if (objDbUtil != null) { // Get list of cases
            ArrayList<String[]> alStreet = new ArrayList<String[]>();
            SQLiteDatabase sqlitedatabase = objDbUtil.getDatabase();
            if (sqlitedatabase != null && sqlitedatabase.isOpen()) {
                cCases = sqlitedatabase.rawQuery("SELECT DISTINCT street "
                        + "FROM Household " + "WHERE street IS NOT NULL "
                        + "AND LENGTH(LTRIM(RTRIM(street)))>0", null);
                String _password = this.context.getPassword();
                if (cCases != null && cCases.moveToFirst()) {
                    do { // Create list of en/decrypted streets
                        alStreet.add(new String[] {
                                cCases.getString(STREET_INDEX),
                                Crypto.decrypt(_password,
                                        cCases.getString(STREET_INDEX)) });
                    } while (cCases.moveToNext());
                }
                if (cCases != null) {
                    cCases.close();
                    cCases = null;
                }
                int alStreet_length = alStreet.size();
                if (alStreet_length > 0) {
                    Collections.sort(alStreet, new Comparator<String[]>() {
                        @Override
                        public int compare(String[] lhs, String[] rhs) {
                            return lhs[DECRYPTED_STREET]
                                    .compareToIgnoreCase(rhs[DECRYPTED_STREET]);
                        }
                    }); // sort decrypted street using custom comparator
                    StringBuilder sql_transaction = new StringBuilder(
                            "BEGIN TRANSACTION;" + "UPDATE Household SET "
                                    + SORT_ATTRIBUTE + "=NULL;");
                    for (int i = 0; i < alStreet_length; i++) {
                        sql_transaction.append(String.format(
                                "UPDATE Household " + "SET "
                                        + SORT_ATTRIBUTE + "=%1$d "
                                        + "WHERE street=\"%2$s\";", i,
                                alStreet.get(i)[ENCRYPTED_STREET]));
                    }
                    sql_transaction.append("COMMIT;");
                    // execute transaction
                    sqlitedatabase.execSQL(sql_transaction.toString());
                }
                returnValue = true;
            }
        }
    } catch (Exception e) {
        Log.e(Utilities.getFullMethodName(e), e.getMessage());
    } finally {
        if (objDbUtil != null) { // release resources
            objDbUtil.close();
            objDbUtil = null;
        }
    }
    return returnValue;
4

0 回答 0