Removed an unnecessary column, changed after this DATEBASE_VERSSION and such an error:

FATAL EXCEPTION: main java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example/com.MainActivity}: android.database.sqlite.SQLiteException: no such table: ContactFruit (code 1): , while compiling: SELECT * FROM ContactFruit 

writes what is wrong with the method:

  public List<Fruit> getAllFruits() { List<Fruit> fruitList = new ArrayList<Fruit>(); String selectQuery = "SELECT * FROM " + TABLE_CONTACT_FRUIT; SQLiteDatabase db = this.getWritableDatabase(); Cursor cursor = db.rawQuery(selectQuery, null); if (cursor.moveToFirst()) { do { Fruit fruit = new Fruit(); fruit.setId(Integer.parseInt(cursor.getString(0))); fruit.setName(cursor.getString(1)); fruit.setPrice(Integer.parseInt(cursor.getString(2))); fruitList.add(fruit); } while (cursor.moveToNext()); } return fruitList; } 
  • Show the class code for working with the database. - post_zeew
  • Added to the question - Fedia

1 answer 1

After you upgraded the DATEBASE_VERSION version, the onUpgrade(...) method was called when connecting to the database, which deleted the existing table:

 db.execSQL("DROP TABLE IF EXISTS " + TABLE_CONTACT_FRUIT); 

That's why you get the corresponding exception. You are trying to add an entry to a table that does not exist.

After deleting a table, you must create it. The onUpgrade(...) method code would be:

 @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { db.execSQL("DROP TABLE IF EXISTS " + TABLE_CONTACT_FRUIT); db.execSQL(CREATE_TABLE_FRUIT); }