Enter and show dates SQLITE Android studio

1

I have 3 fields that are (code, name, career) those fields are saved by a button and saved in a database SQLite , so all right, but I need that too have the date on which those data were entered, how could I do that? The idea was to see if they could help me how to make the date automatically entered without having to be all the time entering the date and time, and then using another button in mainActivity access the records, but mainly I need to know How to do to enter the dates and how to make the query to get the date? Thank you very much already.

Database:

package com.example.juan.myapplication2;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class BD extends SQLiteOpenHelper {

private static final String NOMBRE_BD = "prueba";
private static final int VERSION_BD = Integer.parseInt("1");
private static final String TABLA_CURSOS="CREATE TABLE CURSOS(CODIGO TEXT 
PRIMARY KEY, CURSO TEXT, CARRERA TEXT)";

public BD(Context context) {
    super(context, NOMBRE_BD, null, VERSION_BD);
}


@Override
public void onCreate(SQLiteDatabase db) {
        db.execSQL(TABLA_CURSOS);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS" +TABLA_CURSOS);
        db.execSQL(TABLA_CURSOS);
}

 public void aregarcursos(String codigo, String curso, String carrera) {
    SQLiteDatabase bd=getWritableDatabase();
    if (bd!=null){
        bd.execSQL("INSERT INTO CURSOS 
 VALUES('"+codigo+"','"+curso+"','"+carrera+"')");
        bd.close();
    }

}
}

Second Activity:

public class segundo extends AppCompatActivity {
EditText codigotxt, nombretxt, carreratxt;
Button guardar;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_segundo);

    codigotxt=(EditText) findViewById(R.id.codigotxt);
    nombretxt=(EditText) findViewById(R.id.nombretxt);
    carreratxt=(EditText) findViewById(R.id.carreratxt);
    guardar=(Button) findViewById(R.id.guardar);

final BD bd= new BD(getApplicationContext());
guardar.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        bd.aregarcursos(codigotxt.getText().toString(),nombretxt.getText().toString(),carreratxt.getText().toString());
        Toast.makeText(getApplicationContext(),"Se agrego", Toast.LENGTH_SHORT).show();

        }
    });

    }

}
    
asked by Juan Iriarte 26.09.2018 в 00:32
source

2 answers

0

SQLite, How to make the date automatically registered without having to insert the date and time value?.

For this you can define the date field in this way:

DATE DEFAULT CURRENT_TIMESTAMP

Therefore the script to create the table would change, you must add the field you want and define the type DATE :

private static final String TABLA_CURSOS="CREATE TABLE CURSOS(CODIGO TEXT 
PRIMARY KEY, CURSO TEXT, CARRERA TEXT, DATE DEFAULT CURRENT_TIMESTAMP)";

and modify the method that performs the insertion of data, specifying only the fields to insert since the field DATE will automatically insert the value of the date:

 public void aregarcursos(String codigo, String curso, String carrera) {
    SQLiteDatabase bd=getWritableDatabase();

   if (bd!=null){
        bd.execSQL("INSERT INTO CURSOS(CODIGO, CURSO, CARRERA) VALUES('"+codigo+"','"+curso+"','"+carrera+"')");
        bd.close();
    }

  }
  • Important: delete the data of your application or your application since you have a structure created which will not change if the version of your database is the same ( VERSION_BD ).
answered by 26.09.2018 в 01:08
0

Another way: first you declare the columns in the DB Helper:

 public static final String CURSOS_COLUMNA_CURSO = "curso";
 public static final String CURSOS_COLUMNA_FECHA = "fecha";
 ...

You add another column:

FECHA TEXT

You aggravate this method to get the current date:

private String getDate() {             // se vería así: miercoles 26/09/2018 05:30 p.m.
    SimpleDateFormat dateFormat = new SimpleDateFormat("EEEE dd/MM/yyyy hh:mm  a", Locale.getDefault());
    Date date = new Date();
    return dateFormat.format(date);
}

Modify your aggregation method:

public void aregarcursos(String codigo, String curso, String carrera) {
SQLiteDatabase bd=getWritableDatabase();
ContentValues cv = new ContentValues();
    cv.put("curso", curso);
    cv.put("carrera", carrera);
    cv.put("fecha", getDate());  // aquí se inserta la fecha actual en forma automática, sin necesidad de ingresarla manualmente
    bd.insert("cursos", null, cv);
}

The date is saved in a string that you can display in a textview :

 String fecha = cursor.getString(cursor.getColumnIndex(BD.CURSOS_COLUMNA_FECHA));
 textView.setText(fecha);

Finally, delete the emulator / device app and reinstall it with the changes already made.

    
answered by 27.09.2018 в 01:27