Consult a record on Sqlite

Asked

Viewed 1,569 times

0

I’m having a problem to perform a query in a Sqlite database of an android application I’m studying. I can recover all the records that the database contains, but I can’t search a single table record. Follows the code of the bank and the class handling it.

public class BancoDeDados extends SQLiteOpenHelper {
private static final String NOME_BD = "JurisIndicium";
private static final int VERSAO_BD = 1;

//variáveis para criação da tabela usuário
public static final String TABELA_USUARIO = "usuario";
public static final String ID = "_id";
public static final String NOME = "nome";
public static final String OAB = "oab";
public static final String SENHA = "senha";

//variáveis para criação da tabela Processo
public static final String TABELA_PROCESSO = "processo";
public static final String ID_PROCESSO = "_idProcesso";
public static final String N_DO_PROCESSO = "nDoProcesso";
public static final String TIPO_ACAO = "tipoAcao";
public static final String DATA_JULGADO = "dataJulgado";
public static final String N_SPROC = "nSproc";
public static final String PROTOCOLO = "protocolo";
public static final String ORGAO_E_RELATOR = "orgaoRelator";
public static final String REVISOR  = "revisor";
public static final String PROTELANTE  = "protelante";
public static final String REPRESENTANTE_PROTELANTE = "representanteProtelante";
public static final String PROTELADO  = "protelado";
public static final String REPRESENTANTE_PROTELADO = "representanteProtelado";
public static final String TERCEIRO = "terceiro";
public static final String ASSISTENTE = "assistente";
public static final String REPRESENTANTE_JURIDICO = "representanteJuridico";
public static final String MOVIMENTACAO = "movimentacao";
public static final String OBS = "obs";
public static final String INICIO = "inicio";
public static final String FIM = "fim";

public BancoDeDados(Context contexto) {//construtor da classe
    super(contexto, NOME_BD, null, VERSAO_BD);
}

@Override//cria o banco em sua inexistencia e estabelece a conexão
public void onCreate(SQLiteDatabase bd) {

    //bloco de criação da tabela usuário
    bd.execSQL("create table "+TABELA_USUARIO
            +"("+ID+" integer primary key autoincrement, "
            +NOME+" text not null, "
            +OAB+" text not null, "
            +SENHA+" text not null);");

    //bloco de criação da tabela processo
    bd.execSQL("create table processo"
            +"("+ID_PROCESSO+" integer primary key autoincrement, "
            +N_DO_PROCESSO+" text not null, "
            +TIPO_ACAO+" text not null, "
            +DATA_JULGADO+" text not null, "
            +N_SPROC+" text not null, "
            +PROTOCOLO+" text not null, "
            +ORGAO_E_RELATOR+" text not null, "
            +REVISOR+" text not null, "
            +PROTELANTE+" text not null, "
            +REPRESENTANTE_PROTELANTE+" text not null, "
            +PROTELADO+" text not null,"
            +REPRESENTANTE_PROTELADO+" text not null, "
            +TERCEIRO+" text not null, "
            +ASSISTENTE+" text not null, "
            +REPRESENTANTE_JURIDICO+" text not null, "
            +MOVIMENTACAO+" text not null, "
            +OBS+" text not null, "
            +INICIO+" text not null, "
            +FIM+" text not null);");
}

@Override//atualiza o banco ja criado
public void onUpgrade(SQLiteDatabase bd, int oldVersion, int newVersion) {//
    bd.execSQL("drop table usuario");
    onCreate(bd);
}

}

public Processo buscaProcesso(String numeroProcesso){
    Processo processo;
    bd = bdHelper.getReadableDatabase();
    Cursor cursor = bd.query(BancoDeDados.TABELA_PROCESSO, new String[]{BancoDeDados.TIPO_ACAO,
                    BancoDeDados.PROTELANTE, BancoDeDados.MOVIMENTACAO},
                    BancoDeDados.N_DO_PROCESSO + " = ?", new String[]{numeroProcesso+""},
                    null, null, null);
    if(cursor != null){
        cursor.moveToNext();
        processo = new Processo();
        processo.setTipoAcao(cursor.getString(0));
        processo.setProtelante(cursor.getString(1));
        processo.setUltimaMovimentacao(cursor.getString(2));
    }else{
        return null;
    }
    cursor.close();
    return processo;

}

Thanks for all your help. PS: if I made a mistake regarding the posting rules, please correct me.

  • 1

    Rodolfo, what would be the problem you’re having? At first the only problems I see are: cursor will never be null, what can occur is the moveToNext return false if there are no results, recommend using a moveToFirst, checking the return right at the beginning. Another thing is the indexes, the ideal is to use the getColumnIndexOrThrow(nomeDaColuna) and not directly access, the Android persistence api does not work the same as JDBC :/

  • Wakin, thank you for your reply. I am passing a String as parameter to the process query method, and I was trying to use it to get a record whose process number was equal to this String. I only got back when I passed the registration id. Thank you.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.