How to use Dates and Times in Sqlite

Asked

Viewed 168 times

0

I’m having trouble using dates and times in Sqlite, somehow not saving the data.

below my databaseAdapter

public class DataBaseAdapter extends SQLiteOpenHelper{

private static final int DATABASE_VERSION = 1;
protected static final String DATABASE_NAME = "Assistente.BD";


public DataBaseAdapter(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

@Override
public void onCreate(SQLiteDatabase db) {

    String sql = "CREATE TABLE compromissos " +
            "( id INTEGER PRIMARY KEY AUTOINCREMENT," +
            " compromisso TEXT" +
            " data TEXT" +
            " horario TEXT)";

    db.execSQL(sql);





}

@Override
public void onUpgrade(SQLiteDatabase db, int i, int i1) {

    String sql = "DROP TABLE IF EXITS compromissos";
    db.execSQL(sql);
    onCreate(db);

} 

the create method

//CRIAR COMPROMISSOS
public boolean create (Compromissos compromissos) {

    ContentValues contentValues = new ContentValues();
    contentValues.put("compromisso", compromissos.getCompromisso());
    contentValues.put("data", compromissos.getData());
    contentValues.put("horario", compromissos.getHorario());



    SQLiteDatabase db = this.getWritableDatabase();

    boolean isCreate = db.insert("compromissos", null, contentValues) > 0;
    db.close();

    return isCreate;
}

my booking form

@Override
public void onClick(View view) {


    if (view == btData) {
        final Calendar c = Calendar.getInstance();
        dia = c.get(Calendar.DAY_OF_MONTH);
        mes = c.get(Calendar.MONTH);
        ano = c.get(Calendar.YEAR);

        DatePickerDialog datePickerDialog = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                dataId.setText(dayOfMonth + "/" + (monthOfYear + 1) + "/" + year);


            }
        }
                , dia, mes, ano);
        datePickerDialog.show();

    }
    if (view == btHorario) {
        final Calendar c = Calendar.getInstance();
        hora = c.get(Calendar.HOUR_OF_DAY);
        minutos = c.get(Calendar.MINUTE);

        TimePickerDialog timePickerDialog = new TimePickerDialog(this, new TimePickerDialog.OnTimeSetListener() {
            @Override
            public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                horarioId.setText(hourOfDay + ":" + minute);

            }
        }, hora, minutos, false);
        timePickerDialog.show();

    }

    if (view == btAgendar){
        final Context context = view.getContext();
        String compromisso = compromissoId.getText().toString();
        String datatexto = dataId.getText().toString();
        int data = Integer.parseInt(datatexto);
        String horariotexto = horarioId.getText().toString();
        int horario = Integer.parseInt(horariotexto);


        Compromissos compromissos = new Compromissos();
        compromissos.setCompromisso(compromisso);
        compromissos.setData(data);
        compromissos.setHorario(horario);

        boolean criarCompromissocomsucesso = new CompromissoController(context).create(compromissos);

        if (criarCompromissocomsucesso) {
            Toast.makeText(context, "Compromisso agendado com sucesso!", Toast.LENGTH_LONG).show();

            ((ListaCompromissos) context).contadorDeRegistros();

            abrirListadeCompromissos();




        } else {
            Toast.makeText(context, "Não foi possivel agendar o compromisso!", Toast.LENGTH_LONG).show();
        }

    }


}

private void abrirListadeCompromissos(){
    Intent i = new Intent(FormularioCompromisso.this, ListaCompromissos.class);
    startActivity(i);
}
  • Is returning some error?

  • no, only when I test on the phone and the moment I click schedule, appears the message "the application stopped"

No answers

Browser other questions tagged

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