0

my android app fetch data from server then store it locally in SQL, when i install app first time on mobile it creates SQL database then it checks the current data and the data fetched from server then it compares if the data is matched or not, if not then first it call delete method

public void deleteUfoneData() {

    SQLiteDatabase db = this.getWritableDatabase();
    db.execSQL("DELETE FROM " + ufoneEntry.TABLE_NAME);

}

then it calls insert method, since the database is empty delete method executes without problem but when i make changes on server data then launch app again then it crashes on delete method, where i went wrong ?

public boolean insertWaridData(String name, String price, String vol, String val, String sub, String unsub, String bal) {

    SQLiteDatabase db = this.getWritableDatabase();

    ContentValues contentValues = new ContentValues();
    contentValues.put(waridEntry.COLUMN_PACKAGE_NAME, name);
    contentValues.put(waridEntry.COLUMN_PACKAGE_PRICE, price);
    contentValues.put(waridEntry.COLUMN_PACKAGE_VOL, vol);
    contentValues.put(waridEntry.COLUMN_PACKAGE_VAL, val);
    contentValues.put(waridEntry.COLUMN_PACKAGE_SUB, sub);
    contentValues.put(waridEntry.COLUMN_PACKAGE_UNSUB, unsub);
    contentValues.put(waridEntry.COLUMN_PACKAGE_BAL, bal);

    long result = db.insert(waridEntry.TABLE_NAME, null, contentValues);

    if (result == -1) {
        return false;
    } else {
        return true;
    }

}
Shah Zeb
  • 31
  • 2
  • 9
  • 3
    Can you clarify which programming language you are using here? Also, shouldn't this be tagged "sqlite" and not "mysql" or "sql-server"? – Dave Cross Aug 31 '16 at 13:54
  • There can be many reasons. You should look at the error message. it's not clear from your question. Some reasons: a user has not enough privileges to delete from a table; `ufoneEntry.TABLE_NAME` not defined or empty; the table doesn't exist; .... – valex Aug 31 '16 at 14:00
  • 1
    http://stackoverflow.com/questions/23353173/unfortunately-myapp-has-stopped-how-can-i-solve-this – laalto Aug 31 '16 at 14:23
  • it's android and the user have permission and it's defined as well – Shah Zeb Aug 31 '16 at 14:44
  • You could do a `insertOrThrow` instead and catch the `SQLException` to see what that gives. Or it might be that some of the columns you insert into are nullable and that 2nd param to [insert](https://developer.android.com/reference/android/database/sqlite/SQLiteDatabase.html#insert) shouldn't be null? – LukStorms Aug 31 '16 at 16:53

0 Answers0