0

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();
                }
            }
    );
}
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459

2 Answers2

1

You can do:

public boolean insertData(String Name, String Datereceived, String Expirydate) {
   try {
       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;
   } catch(Exception x) {
      return false;
   }

Not very good but you can start with that.

Alexander Kulyakhtin
  • 47,782
  • 38
  • 107
  • 158
1

do one thing before putting the value in database check is it a valid date or not ,you can insert the data if its valid or insert some dummy value.use the below code to check its valid date or not

   public static boolean isValidFormat(String format, String value) {
    Date date = null;
    try {
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        date = sdf.parse(value);
        if (!value.equals(sdf.format(date))) {
            date = null;
        }
    } catch (ParseException ex) {
        ex.printStackTrace();
    }
    return date != null;
}

More date validity check try this link -- Java: Check the date format of current string is according to required format or not

Community
  • 1
  • 1
Jithu P.S
  • 1,843
  • 1
  • 19
  • 32