Format date in the EditText to be written in Spanish format (MM-dd-yyyy) and rewrite it in English format (yyyy-MM-dd) in the sqlite database.
Here is my code of adapter:
public class AuxGlucDbAdapter {
public static final String KEY_IDPACIENTE = "IdPaciente";
public static final String KEY_FECHA = "Fecha";
public static final String KEY_EVE1 = "Eve1";
public static final String KEY_EVE2 = "Eve2";
public static final String KEY_EVE3 = "Eve3";
public static final String KEY_EVE4 = "Eve4";
public static final String KEY_EVE5 = "Eve5";
public static final String KEY_EVE6 = "Eve6";
public static final String KEY_EVE7 = "Eve7";
public static final String KEY_ID = "_id";
SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
Date date = sdf.parse(KEY_FECHA);
private static final String TAG = "AuxGlucDbAdapter";
private DatabaseHelper mDbHelper;
private SQLiteDatabase mDb;
private static final String DATABASE_CREATE =
"create table AuxGluc (_id integer primary key autoincrement, "
+ "IdPaciente numeric not null, Fecha datetime not null, Eve1 numeric not null, Eve2 numeric not null, Eve3 numeric not null, Eve4 numeric not null, Eve5 numeric not null, Eve6 numeric not null, Eve7 numeric not null);";
private static final String DATABASE_NAME = "data";
private static final String DATABASE_TABLE = "AuxGluc";
private static final int DATABASE_VERSION = 2;
private final Context mCtx;
private static class DatabaseHelper extends SQLiteOpenHelper {
DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(DATABASE_CREATE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
+ newVersion + ", which will destroy all old data");
db.execSQL("DROP TABLE IF EXISTS AuxGluc");
onCreate(db);
}
}
public AuxGlucDbAdapter(Context ctx) {
this.mCtx = ctx;
}
public AuxGlucDbAdapter open() throws SQLException {
mDbHelper = new DatabaseHelper(mCtx);
mDb = mDbHelper.getWritableDatabase();
return this;
}
public void close() {
mDbHelper.close();
}
public long createNote(String IdPaciente, String Fecha, String Eve1, String Eve2, String Eve3, String Eve4, String Eve5, String Eve6, String Eve7) {
ContentValues initialValues = new ContentValues();
initialValues.put(KEY_IDPACIENTE, IdPaciente);
initialValues.put(KEY_FECHA, Fecha);
initialValues.put(KEY_EVE1, Eve1);
initialValues.put(KEY_EVE2, Eve2);
initialValues.put(KEY_EVE3, Eve3);
initialValues.put(KEY_EVE4, Eve4);
initialValues.put(KEY_EVE5, Eve5);
initialValues.put(KEY_EVE6, Eve6);
initialValues.put(KEY_EVE7, Eve7);
return mDb.insert(DATABASE_TABLE, null, initialValues);
}
public boolean deleteNote(long Id) {
return mDb.delete(DATABASE_TABLE, KEY_ID + "=" + Id, null) > 0;
}
public Cursor fetchAllNotes() {
return mDb.query(DATABASE_TABLE, new String[] {KEY_ID, KEY_IDPACIENTE, KEY_FECHA,
KEY_EVE1, KEY_EVE2, KEY_EVE3, KEY_EVE4, KEY_EVE5, KEY_EVE6, KEY_EVE7}, null, null, null, null, null, null);
}
public Cursor fetchNote(long Id) throws SQLException {
Cursor mCursor =
mDb.query(true, DATABASE_TABLE, new String[] {KEY_ID, KEY_IDPACIENTE,
KEY_FECHA, KEY_EVE1, KEY_EVE2, KEY_EVE3, KEY_EVE4, KEY_EVE5, KEY_EVE6, KEY_EVE7}, KEY_ID + "=" + Id, null,
null, null, null, null);
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}
public boolean updateNote(long Id, String IdPaciente, String Fecha, String Eve1, String Eve2, String Eve3, String Eve4, String Eve5, String Eve6, String Eve7) {
ContentValues args = new ContentValues();
args.put(KEY_IDPACIENTE, IdPaciente);
args.put(KEY_FECHA, Fecha);
args.put(KEY_EVE1, Eve1);
args.put(KEY_EVE2, Eve2);
args.put(KEY_EVE3, Eve3);
args.put(KEY_EVE4, Eve4);
args.put(KEY_EVE5, Eve5);
args.put(KEY_EVE6, Eve6);
args.put(KEY_EVE7, Eve7);
return mDb.update(DATABASE_TABLE, args, KEY_ID + "=" + Id, null) > 0;
}
}
And here other code:
public class NoteEdit extends Activity {
private EditText mIdPacienteText;
private EditText mFechaText;
private EditText mEve1Text;
private EditText mEve2Text;
private EditText mEve3Text;
private EditText mEve4Text;
private EditText mEve5Text;
private EditText mEve6Text;
private EditText mEve7Text;
private Long mId;
private AuxGlucDbAdapter mDbHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mDbHelper = new AuxGlucDbAdapter(this);
mDbHelper.open();
setContentView(R.layout.note_edit);
setTitle(R.string.edit_note);
mIdPacienteText = (EditText) findViewById(R.id.IdPaciente);
mFechaText = (EditText) findViewById(R.id.Fecha);
mEve1Text = (EditText) findViewById(R.id.Eve1);
mEve2Text = (EditText) findViewById(R.id.Eve2);
mEve3Text = (EditText) findViewById(R.id.Eve3);
mEve4Text = (EditText) findViewById(R.id.Eve4);
mEve5Text = (EditText) findViewById(R.id.Eve5);
mEve6Text = (EditText) findViewById(R.id.Eve6);
mEve7Text = (EditText) findViewById(R.id.Eve7);
Button confirmButton = (Button) findViewById(R.id.confirm);
mId = (savedInstanceState == null) ? null :
(Long) savedInstanceState.getSerializable(AuxGlucDbAdapter.KEY_ID);
if (mId == null) {
Bundle extras = getIntent().getExtras();
mId = extras != null ? extras.getLong(AuxGlucDbAdapter.KEY_ID)
: null;
}
populateFields();
confirmButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
setResult(RESULT_OK);
finish();
}
});
}
private void populateFields() {
if (mId != null) {
Cursor note = mDbHelper.fetchNote(mId);
startManagingCursor(note);
mIdPacienteText.setText(note.getString(
note.getColumnIndexOrThrow(AuxGlucDbAdapter.KEY_IDPACIENTE)));
mFechaText.setText(note.getString(
note.getColumnIndexOrThrow(AuxGlucDbAdapter.KEY_FECHA)));
mEve1Text.setText(note.getString(
note.getColumnIndexOrThrow(AuxGlucDbAdapter.KEY_EVE1)));
mEve2Text.setText(note.getString(
note.getColumnIndexOrThrow(AuxGlucDbAdapter.KEY_EVE2)));
mEve3Text.setText(note.getString(
note.getColumnIndexOrThrow(AuxGlucDbAdapter.KEY_EVE3)));
mEve4Text.setText(note.getString(
note.getColumnIndexOrThrow(AuxGlucDbAdapter.KEY_EVE4)));
mEve5Text.setText(note.getString(
note.getColumnIndexOrThrow(AuxGlucDbAdapter.KEY_EVE5)));
mEve6Text.setText(note.getString(
note.getColumnIndexOrThrow(AuxGlucDbAdapter.KEY_EVE6)));
mEve7Text.setText(note.getString(
note.getColumnIndexOrThrow(AuxGlucDbAdapter.KEY_EVE7)));
}
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
saveState();
outState.putSerializable(AuxGlucDbAdapter.KEY_ID, mId);
}
@Override
protected void onPause() {
super.onPause();
saveState();
}
@Override
protected void onResume() {
super.onResume();
populateFields();
}
private void saveState() {
String IdPaciente = mIdPacienteText.getText().toString();
String Fecha = mFechaText.getText().toString();
String Eve1 = mEve1Text.getText().toString();
String Eve2 = mEve2Text.getText().toString();
String Eve3 = mEve3Text.getText().toString();
String Eve4 = mEve4Text.getText().toString();
String Eve5 = mEve5Text.getText().toString();
String Eve6 = mEve6Text.getText().toString();
String Eve7 = mEve7Text.getText().toString();
if (mId == null) {
long Id = mDbHelper.createNote(IdPaciente, Fecha, Eve1, Eve2, Eve3, Eve4, Eve5, Eve6, Eve7);
if (Id > 0) {
mId = Id;
}
} else {
mDbHelper.updateNote(mId, IdPaciente, Fecha, Eve1, Eve2, Eve3, Eve4, Eve5, Eve6, Eve7);
}
}
}
Sqlite does not have a “datetime” type. You would be better off storing the Unix Timestamp (date.getTime() I think) – which is in milliseconds, and converting that in sqlite to whatever you like using the date(), time(), and strftime() functions in sqlite (see links below).
Please see this link to learn about sqlite data types.
See this link to see how to format dates in sqlite.
Also see this, and this question for more help.
EDIT: In response to your question (from the link above):
So when you store yyyy-MM-dd it is actually storing it as a TEXT field (or string). When you pull it out it comes out just as you stored it, but it is a string and cannot be manipulated as a date without parsing in Java first. Even with my method you still have to parse the date out, but you can format dates in your queries however you like (yyyy-MM-dd or MM-dd-yyyy).
The advantage of storing the date in milliseconds as an INTEGER in sqlite is that you can now sort your dates in your query.
EDIT:
Example:
Your database creation string would look like the following:
Now when you save a date, you could use Androids Calendar class, and call myCalendar.getTimeInMillis() to get the integer value. So your createNode signature would look like this: