我的Cursor.moveToFirst总是返回false

时间:2018-06-29 12:48:11

标签: java android sql sqlite

我正在对数据库进行选择,而cursor.moveToFirst始终返回false, 我已经在寻找答案了,什么也没有。 那是我的数据库功能

public List<Obra> buscarListaMapa(String tipoGeoReferenciamentoFiltro, String idJurisdicionadoFiltro) {
        SQLiteDatabase db = helper.getReadableDatabase();

        Cursor cursor = db.rawQuery("SELECT * FROM " + BancoDadosHelper.TABELA_OBRA + " WHERE "+ BancoDadosHelper.COLUNA_ID_TIPO_GEOREFENCIAMENTO + " = '"+tipoGeoReferenciamentoFiltro+"' and "+
                BancoDadosHelper.COLUNA_ID_JURISDICIONADO+" = '"+idJurisdicionadoFiltro+"'", null);


        List<Obra> listaMapa = new ArrayList<>();

        if (cursor.moveToFirst()) {
            do {
                String numObra = cursor.getString(cursor.getColumnIndex(BancoDadosHelper.COLUNA_NUM_OBRA));
                String descObra = cursor.getString(cursor.getColumnIndex(BancoDadosHelper.COLUNA_DESCRICAO_OBRA));
                String geoReferenciamento = cursor.getString(cursor.getColumnIndex(BancoDadosHelper.COLUNA_GEOREFERENCIAMENTO));
                String tipoObra = cursor.getString(cursor.getColumnIndex(BancoDadosHelper.COLUNA_TIPO_OBRA));
                Obra novaObra = new Obra(numObra, descObra, tipoObra, geoReferenciamento);
                listaMapa.add(novaObra);

            } while (cursor.moveToNext());
        }

        cursor.close();
        db.close();
        return listaMapa;
    }

当我在数据库WORKBENCH上进行相同选择时,我的结果是

工作台执行相同选择的结果

enter image description here

1 个答案:

答案 0 :(得分:1)

为光标添加空条件检查,以确保您的光标具有一些数据。

 if(cursor!=null{
 cursor.moveToFirst();
            do {
                String numObra = 
 cursor.getString(cursor.getColumnIndex(BancoDadosHelper.COLUNA_NUM_OBRA));
                String descObra = 
 cursor.getString(cursor.getColumnIndex(BancoDadosHelper.COLUNA_DESCRICAO_OBRA));
                String geoReferenciamento = 
 cursor.getString(cursor.getColumnIndex(BancoDadosHelper.COLUNA_GEOREFERENCIAMENTO 
 ));
                String tipoObra = 
 cursor.getString(cursor.getColumnIndex(BancoDadosHelper.COLUNA_TIPO_OBRA));
                Obra novaObra = new Obra(numObra, descObra, tipoObra, 
 geoReferenciamento);
                listaMapa.add(novaObra);

            } while (cursor.moveToNext());


 }
相关问题