Android 列表大小从sqlite查询返回0
我试图通过连接三个表来查询前提列表。但是,当我添加WHERE时,我的列表返回空。我试着把一切都记录下来。。似乎没有它,我可以得到一个前提列表。。。但这是错误的。我尝试在sqlite的DB Browser上运行查询,它设法运行并返回正确的列表。请帮帮我 这是我的查询码Android 列表大小从sqlite查询返回0,android,mysql,sqlite,android-cursor,Android,Mysql,Sqlite,Android Cursor,我试图通过连接三个表来查询前提列表。但是,当我添加WHERE时,我的列表返回空。我试着把一切都记录下来。。似乎没有它,我可以得到一个前提列表。。。但这是错误的。我尝试在sqlite的DB Browser上运行查询,它设法运行并返回正确的列表。请帮帮我 这是我的查询码 public List<TXN_Premise> getTxnTableData(String tableName, String columnName) { ArrayList<TXN_Prem
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;
}
public List getTxnTableData(String tableName,String columnName){
ArrayList itemList=新建ArrayList();
String selectQuery=“选择”
+常数.列_前提_参考+“,”
+Constants.COLUMN_PREMISE_NAME+“,”
+Constants.COLUMN_premission_ADDRESS+“,”
+Constants.REF_PREMISE_CATEGORY_TABLE+“+Constants.COLUMN_PREMISE_CATEGORY_ID+”作为“+Constants.COLUMN_FK_PREMISE_CATEGORY_ID+”,”
+Constants.REF_PREMISE_CATEGORY_MASTER_TABLE+“+Constants.COLUMN_PREMISE_CATEGORY_MASTER_ID+”作为“+Constants.COLUMN_FK_PREMISE_CATEGORY_MASTER_ID+”
+常量.列_前提_注册表_编号+“,”
+Constants.COLUMN_PREMISE_REG_DATE+“,”
+Constants.REF_PREMISE_CATEGORY_TABLE+“+Constants.COLUMN_DESCRIPTION+”作为PremiseContegoryDescription,“
+Constants.REF_PREMISE_CATEGORY_MASTER_TABLE+“+Constants.COLUMN_DESCRIPTION+”作为PremiseContegoryMasterDescription”
+“FROM”+表名
+“左联接”+常量.REF\u前提\u类别\u主表+“开”+
Constants.TXN_PREMISE_TABLE+“+Constants.COLUMN_PREMISE_CATEGORY_MASTER_ID+”=“+Constants.REF_PREMISE_CATEGORY_MASTER_TABLE+”+“+Constants.COLUMN_代码
+“左联接”+常量.REF\u前提\u类别\u表+“开”+
Constants.TXN_PREMISE_TABLE+“+Constants.COLUMN_FK_PREMISE_CATEGORY_ID+”=“+Constants.REF_PREMISE_CATEGORY_TABLE+”+“+Constants.COLUMN_代码
//“+”其中“+Constants.REF_PREMISE_CATEGORY_TABLE+”“+Constants.COLUMN_LANG+”='MYS'”
//+Constants.REF_PREMISE_CATEGORY_MASTER_TABLE+“+Constants.COLUMN_LANG+”='MYS'和“
//+Constants.COLUMN_PREMISE_NAME+“'-”和“+Constants.COLUMN_PREMISE_NAME+”-TIADA-”
+“订单依据”+列名+“ASC限制3”;
Log.d(“test”,“getTxnTableData:”+selectQuery);
试一试{
open();
//确保数据库不是空的
if(sqLiteDatabase!=null){
//获取数据库中所有状态的游标
Cursor Cursor=sqLiteDatabase.rawQuery(selectQuery,null);
d(“test”,“getTxnTableData:cursor”+cursor.toString());
Log.d(“测试”,“getTxnTableData:外部”);
if(cursor.moveToFirst()){
而(!cursor.isAfterLast()){
TXN_前提前提条件column=新的TXN_前提条件();
//setPremiseId(cursor.getString(0));
setPremiseRef(cursor.getString(cursor.getColumnIndex(Constants.COLUMN_PREMISE_REF));
setPremiseName(cursor.getString(cursor.getColumnIndex(Constants.COLUMN\u PREMISE\u NAME));
setPremiseAddress(cursor.getString(cursor.getColumnIndex(Constants.COLUMN_PREMISE_ADDRESS));
premiseColumn.setFk_premiseCategoryId(cursor.getString(cursor.getColumnIndex(Constants.COLUMN_FK_premiseCategoryId));
premiseColumn.setFk_premiseContegoryMasterId(cursor.getString(cursor.getColumnIndex(Constants.COLUMN_FK_premiseColumn_CATEGORY_MASTER_ID));
setPremiseGeno(cursor.getString(cursor.getColumnIndex(Constants.COLUMN_PREMISE_REG_NO));
setPremiseGrandate(cursor.getString(cursor.getColumnIndex(Constants.COLUMN_PREMISE_REG_DATE));
//todo:临时前提类别说明
premiseColumn.setPremiseCategoryName(cursor.getString(cursor.getColumnIndex(“premiseCategoryDescription”));
setPremiseColumn.SetPremiseColumnySterName(cursor.getString(cursor.getColumnIndex(“PremiseColumnySterDescription”));
Log.d(“test”,“getTxnTableData:premiseName”+cursor.getString(cursor.getColumnIndex(Constants.COLUMN\u PREMISE\u NAME));
//在光标中添加一列
itemList.add(premise列);
cursor.moveToNext();
}
}
cursor.close();
}
close();
}捕获(SQLE异常){
e、 printStackTrace();
}
Log.d(“test”、“getTxnTableData:itemlist”+itemlist.size());
返回项目列表;
}
由于WHERE子句依赖于基础数据,因此无法重新创建问题,从而确定问题
因此,您需要遵循简单的问题确定
正如您所指出的,它在没有WHERE子句的情况下工作
- 1) 删除WHERE子句并运行
- 1a)如果您现在检索行,然后逐条件逐步构建WHERE子句,这将突出显示问题
- 1b)如果在删除WHERE子句后,没有行的问题仍然存在,则从
String selectQuery=“SELECT*FROM”+tableName开始逐步构建整个SQL代码>,检查结果是否符合预期
Log.d("test", "getTxnTableData: cursor " + cursor.toString());
到
这将显示光标中的行数
此外,您还可以利用
编辑 仔细观察,WHER似乎存在一些问题
Log.d("test", "getTxnTableData: cursor " + cursor.getCount());
" 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-'"
" 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-'" +
")"