Comparison of an edttext with an Sqlite Bank value

Asked

Viewed 333 times

1

I’m developing an application on android but do not know how to prevent the user register two equal times.

Bench:

 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 cannot let the user schedule two equal times on the same date

  • Hello! You added the DATA field as the right text? how do you save this field? What is the format? You do not want to allow an identical date?

  • Yes, there may be two identical dates but not two identical times on the same date

1 answer

1

This requires a consultation!

This will return the ID (if there is) an 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, check if 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!

        }

    }
  • In the case in place of the questions put editTexts?? And in the insert button call the function?

  • function n , the method "validardata"

  • And I’m having an error in the line "final Sqlitedatabase db = getWritableDatabase();" says q n can find the method

  • In the action of the button you call the validate ! This in turn will call the validate Date(DATE, TIME). On the log of DATE and TIME, put the fields coming from Edittext.

  • The method getWritableDatabase() comes from Sqliteopenhelper. Here is an example: http://www.vogella.com/tutorials/AndroidSQLite/article.html

  • in case I create the bank in a separate class than I do Insert

  • In the same class!

  • public integer validarData(final String DATA, final String TIME){ Sqlitedatabase db = bank.getWritableDatabase(); final Cursor cursor = db.query(bank.TABELAAGE, new String[]{"id"}, "DATA = ? AND TIME =?", new String[]{Dia.gettext().toString(), Time.toString()}, null, null, null, null); //if null or empty did not find any with the same date and time, returns -1 if (null == cursor || !cursor.moveToFirst()) Return -1; final Integer id = cursor.getInt(0); cursor.close(); Return id; }

  • was like this, but I’m having another error java.lang.Nullpointerexception: Attempt to invoke virtual method 'android.database.sqlite.Sqlitedatabase com.example.userlocal.oficinadocabelo.banco.banco.getWritableDatabase()' on a null Object Reference

  • it is likely that the bank is null! It would be interesting to create a new question, then it is easier to help!

  • ready already created

  • http://answall.com/questions/169995/compara%C3%A7%C3%A3o-de-um-edittext-com-valor-do-banco2

Show 7 more comments

Browser other questions tagged

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