我尝试通过连接三个表来查询前提列表。但是,当我添加 WHERE 时,我的列表返回空。我试图记录所有内容......似乎没有它,我能够获得一个前提列表......但它错了。我尝试在 DB Browser for sqlite 上运行查询,它设法运行并返回正确的列表。请帮帮我。
这是我的查询代码..
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;
}
答案 0 :(得分:1)
由于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());
然后将显示Cursor中的行数。
此外,您可以利用the utillities here
更仔细地看一下,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)
这对条件从不符合名称只能是 - 或 -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-'" +
")"