列表大小 return 0 来自 sqlite 查询

List size return 0 from sqlite query

我试图通过连接三个表来查询前提列表。但是,当我添加 WHERE 时,我的列表 return 为空。我试图记录所有内容……似乎没有它,我就能获得前提列表……但这是错误的。我在 DB Browser for sqlite 上尝试了 运行ning 查询,它设法 运行 和 return 正确的列表。请帮助我。

这是我的查询代码..

public List<TXN_Premise> getTxnTableData(String tableName, String columnName) {

        ArrayList<TXN_Premise> itemList = new ArrayList<>();
        String selectQuery = "SELECT "
                + Constants.COLUMN_PREMISE_REF + ", "
                + Constants.COLUMN_PREMISE_NAME + ", "
                + Constants.COLUMN_PREMISE_ADDRESS + ", "
                + Constants.REF_PREMISE_CATEGORY_TABLE + "." + Constants.COLUMN_PREMISE_CATEGORY_ID + " AS " + Constants.COLUMN_FK_PREMISE_CATEGORY_ID +", "
                + Constants.REF_PREMISE_CATEGORY_MASTER_TABLE + "." + Constants.COLUMN_PREMISE_CATEGORY_MASTER_ID + " AS " + Constants.COLUMN_FK_PREMISE_CATEGORY_MASTER_ID + ", "
                + Constants.COLUMN_PREMISE_REG_NO + ", "
                + Constants.COLUMN_PREMISE_REG_DATE + ", "
                + Constants.REF_PREMISE_CATEGORY_TABLE + "." + Constants.COLUMN_DESCRIPTION + " AS premiseCategoryDescription, "
                + Constants.REF_PREMISE_CATEGORY_MASTER_TABLE + "." + Constants.COLUMN_DESCRIPTION + " AS premiseCategoryMasterDescription "

                + " FROM " + tableName

                + " LEFT JOIN " + Constants.REF_PREMISE_CATEGORY_MASTER_TABLE + " ON " +
                Constants.TXN_PREMISE_TABLE + "." + Constants.COLUMN_FK_PREMISE_CATEGORY_MASTER_ID + " = " + Constants.REF_PREMISE_CATEGORY_MASTER_TABLE + "." + Constants.COLUMN_CODE

                + " LEFT JOIN " + Constants.REF_PREMISE_CATEGORY_TABLE + " ON " +
                Constants.TXN_PREMISE_TABLE + "." + Constants.COLUMN_FK_PREMISE_CATEGORY_ID + " = " + Constants.REF_PREMISE_CATEGORY_TABLE + "." + Constants.COLUMN_CODE

//              + " WHERE " + Constants.REF_PREMISE_CATEGORY_TABLE + "." + Constants.COLUMN_LANG + " = 'MYS'"
//              + Constants.REF_PREMISE_CATEGORY_MASTER_TABLE + "." + Constants.COLUMN_LANG + " = 'MYS' AND "
//              + Constants.COLUMN_PREMISE_NAME + " <> '-' AND " + Constants.COLUMN_PREMISE_NAME + " <> '-TIADA-'"

                + " ORDER BY " + columnName + " ASC LIMIT 3";

        Log.d("test", "getTxnTableData: " + selectQuery);

        try {
            open();
            //make sure the database is not empty
            if (sqLiteDatabase != null) {
                //get a cursor for all state in the database
                Cursor cursor = sqLiteDatabase.rawQuery(selectQuery, null);

                Log.d("test", "getTxnTableData: cursor " + cursor.toString());

                Log.d("test", "getTxnTableData: outside");

                if (cursor.moveToFirst()) {
                    while (!cursor.isAfterLast()) {
                        TXN_Premise premiseColumn = new TXN_Premise();
//                      premiseColumn.setPremiseId(cursor.getString(0));
                        premiseColumn.setPremiseRef(cursor.getString(cursor.getColumnIndex(Constants.COLUMN_PREMISE_REF)));
                        premiseColumn.setPremiseName(cursor.getString(cursor.getColumnIndex(Constants.COLUMN_PREMISE_NAME)));
                        premiseColumn.setPremiseAddress(cursor.getString(cursor.getColumnIndex(Constants.COLUMN_PREMISE_ADDRESS)));
                        premiseColumn.setFk_premiseCategoryId(cursor.getString(cursor.getColumnIndex(Constants.COLUMN_FK_PREMISE_CATEGORY_ID)));
                        premiseColumn.setFk_premiseCategoryMasterId(cursor.getString(cursor.getColumnIndex(Constants.COLUMN_FK_PREMISE_CATEGORY_MASTER_ID)));
                        premiseColumn.setPremiseRegNo(cursor.getString(cursor.getColumnIndex(Constants.COLUMN_PREMISE_REG_NO)));
                        premiseColumn.setPremiseRegDate(cursor.getString(cursor.getColumnIndex(Constants.COLUMN_PREMISE_REG_DATE)));


                        //  todo: temp premise category description
                        premiseColumn.setPremiseCategoryName(cursor.getString(cursor.getColumnIndex("premiseCategoryDescription")));
                        premiseColumn.setPremiseCategoryMasterName(cursor.getString(cursor.getColumnIndex("premiseCategoryMasterDescription")));

                        Log.d("test", "getTxnTableData: premiseName " + cursor.getString(cursor.getColumnIndex(Constants.COLUMN_PREMISE_NAME)));

                        //add premiseColumn in the cursor
                        itemList.add(premiseColumn);
                        cursor.moveToNext();
                    }
                }
                cursor.close();
            }
            close();

        } catch (SQLException e) {
            e.printStackTrace();
        }

        Log.d("test", "getTxnTableData: itemlist " + itemList.size() );

        return itemList;
    }

不可能重现问题并因此确定问题,因为 WHERE 子句依赖于基础数据。

因此您需要遵循简单的问题确定。

正如您指出的那样,它可以在没有 WHERE 子句的情况下工作

  • 1) 删除WHERE子句和运行。

    • 1a) 如果您现在检索行然后逐个条件逐步构建 WHERE 子句,那将突出问题。

    • 1b) 如果删除 WHERE 子句后没有行的问题仍然存在,则从 String selectQuery = "SELECT * FROM " + tableName; 开始逐步构建整个 SQL,检查结果是否符合预期。

我建议更改

    Log.d("test", "getTxnTableData: cursor " + cursor.toString());

    Log.d("test", "getTxnTableData: cursor " + cursor.getCount());

这将显示游标中的行数。

此外,您还可以利用


编辑

仔细观察,WHERE 子句似乎存在一些问题:-

为简化起见,将在任何地方做出以下决议:-

  • Constants.REF_PREMISE_CATEGORY_TABLE 似乎将被替换为 rpc
  • Constants.REF_PREMISE_CATEGORY_MASTER_TABLE 似乎将被替换为 mrpc

  • Constants.COLUMN_LANG出现,将被替换为lang

  • Constants.COLUMN_PREMISE_NAME出现,将被替换为name

所以 :-

" WHERE " + Constants.REF_PREMISE_CATEGORY_TABLE + "." + Constants.COLUMN_LANG + " = 'MYS'"
              + Constants.REF_PREMISE_CATEGORY_MASTER_TABLE + "." + Constants.COLUMN_LANG + " = 'MYS' AND "
              + Constants.COLUMN_PREMISE_NAME + " <> '-' AND " + Constants.COLUMN_PREMISE_NAME + " <> '-TIADA-'"

变为(????(n) 用于指示问题,其中 n 标识特定问题,并指出它不是 SQL 的一部分) :-

" WHERE rpc.lang = 'MYS' ????(1) mrpc.lang = 'MYS' AND name <> '-' AND name <> '-TIADA-' ????(2)

  • 问题 1 ????(1) 两次检查之间没有条件,例如也许应该是 WHERE rpc.lang = 'MYS' AND mrpc.lang = 'MYS' .......

    • 这可能会产生类似
    • 的错误
    • ..... [ near "?????????": syntax error ] Exception Name: NS_ERROR_FAILURE Exception Message: Component returned failure code: 0x80004005 (NS_ERROR_FAILURE)
  • 问题 2 ????(2) 这对条件将 永远 满足 name 只能是 --TIADA- 永远不能两者都是 。也许您的意思是 OR 在这种情况下,我会将它们括在括号中。

所以也许代码可以是:-

" WHERE " + 
    Constants.REF_PREMISE_CATEGORY_TABLE + "." + 
        Constants.COLUMN_LANG + 
        " = 'MYS'" + 
" AND " + 
    Constants.REF_PREMISE_CATEGORY_MASTER_TABLE + "." +
        Constants.COLUMN_LANG + " = 'MYS' " + 
" AND (" +
            Constants.COLUMN_PREMISE_NAME + " <> '-' " + 
            "  OR " + 
            Constants.COLUMN_PREMISE_NAME + " <> '-TIADA-'" + 
")"