I am new to android programming and have just managed to build a working sqlite database.
However when data is entered incorrectly, such as into the "Expiry date" section which requires the user to input in the format of YYYY/MM/DD, the program will crash if inputted wrong. Is there a way to reject the information being added to the database avoiding the crash?
Below is the code for setting up the database and inputting into the respective columns
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table " + TABLE_NAME + " (" + COL_1 + " INTEGER PRIMARY KEY," + COL_2 + " TEXT," + COL_3 + " DATE," + COL_4 + " DATE" + ")");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(db);
}
public boolean insertData(String Name, String Datereceived, String Expirydate) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(COL_2, Name);
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
contentValues.put(COL_3, dateFormat.format(new Date(Datereceived)));
contentValues.put(COL_3, Datereceived);
contentValues.put(COL_4, dateFormat.format(new Date(Expirydate)));
long result = db.insert(TABLE_NAME, null, contentValues);
if (result == -1)
return false;
else
return true;
}
And here is the code for the updating the database
public void UpdateData() {
btnviewUpdate.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
boolean isUpdate = myDb.updateData(editTextId.getText().toString(), editname.getText().toString(),
editdate.getText().toString(), editexpiry.getText().toString());
if (isUpdate == true)
Toast.makeText(DatabaseUpdater.this, "Data updated", Toast.LENGTH_LONG).show();
else
Toast.makeText(DatabaseUpdater.this, "Data not updated", Toast.LENGTH_LONG).show();
}
}
);
}