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();
}
});
}
}