Comparison of an edttext with a SQLite Bank value

1

I am developing an android application but I do not know how to prevent the user from registering two equal times.

Bank:

 String sql4 ="CREATE TABLE "+TABELAAGE+"("
                +IDAGE+ " integer primary key autoincrement,"
                +NOMECLIAGE+ " text not null,"
                +DATA+ " text not null,"
                +HORA+ " text not null,"
                +SERVICO+ " text not null,"
                        +VALOR+ " text not null"
                +")";

I can not let the user book two equal times on the same date

    
asked by anonymous 05.12.2016 / 21:09

1 answer

1

For this, it is necessary to make an appointment!

This will return the ID (if any) object with the same DATA and HORA !

If there is no record, this method will return -1

    public Integer validarData(final String DATA, final String HORA){

        final SQLiteDatabase db = getWritableDatabase();
        final Cursor cursor = db.query(TABELAAGE, new String[]{"id"}, "DATA = ? AND HORA =?", new String[]{DATA, HORA}, null, null, null, null);
       //se nulo ou vazio não encontrou nenhum com a mesma data e hora, retorna -1
        if (null == cursor || !cursor.moveToFirst()) return -1;

        final Integer id = cursor.getInt(0);
        cursor.close();
        return id;
    }

If you are changing the Object, make sure the ID returned is equal to the object ID.

Follows:

  public void validar(){
        Integer idComDataEHora = validarData("01/12/2016", "17:18:19");
        if(idComDataEHora.equals(-1)){
            // não exite nenhum registro
        }else{
            /// Se vc está editando, verifique se o id é igual ao que está editando!

        }

    }
    
05.12.2016 / 21:28