I had one database, then I had to create another one for other purposes. After creation, I changed DATEBASE_VERSION , but everything is exactly the error:

 E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.fedia.regestration, PID: 2593 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.app.MainActivity}: android.database.sqlite.SQLiteException: table Contact already exists (code 1): , while compiling: CREATE TABLE Contact ( Name TEXT, Color TEXT, Price TEXT) Caused by: android.database.sqlite.SQLiteException: table ContactFruit already exists (code 1): , while compiling: CREATE TABLE ContactFruit ( Name TEXT, Color TEXT, Price TEXT) 

DB 1:

 public static final String TAG = DBHelper.class.getSimpleName(); public static final int DATEBASE_VERSION = 3; public static final String DATEBASE_NAME = "ContactDB"; public static final String TABLE_CONTACT = "Contact"; public static final String KEY_ID = "ID"; public static final String KEY_NAME = "Name"; public static final String KEY_LOGIN = "Login"; public static final String KEY_PASS = "Pass"; public int countUser; public DBHelper(Context context) { super(context, DATEBASE_NAME, null, DATEBASE_VERSION); } public static final String CREATE_TABLE_USERS = "CREATE TABLE " + TABLE_CONTACT + " ( " + KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + KEY_NAME + " TEXT, " + KEY_LOGIN + " TEXT, " + KEY_PASS + " TEXT)"; @Override public void onCreate(SQLiteDatabase db) { db.execSQL(CREATE_TABLE_USERS); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { db.execSQL("DROP TABLE IF EXISTS " + TABLE_CONTACT); onCreate(db); } 

DB 2:

 public static final int DATEBASE_VERSION = 3; public static final String DATEBASE_NAME_FRUIT = "FruitBD"; public static final String TABLE_CONTACT_FRUIT = "ContactFruit"; public static final String KEY_NAME_FRUIT = "Name"; public static final String KEY_COLOR = "Color"; public static final String KEY_PRICE = "Price"; public DBHelperFruit(Context context) { super(context, DATEBASE_NAME_FRUIT, null, DATEBASE_VERSION); } public static final String CREATE_TABLE_FRUIT = "CREATE TABLE " + TABLE_CONTACT_FRUIT + " ( " + KEY_NAME_FRUIT + " TEXT, " + KEY_COLOR + " TEXT, " + KEY_PRICE + " TEXT)"; @Override public void onCreate(SQLiteDatabase db) { db.execSQL(CREATE_TABLE_FRUIT); onCreate(db); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { db.execSQL("DROP TABLE IF EXISTS " + TABLE_CONTACT_FRUIT); onCreate(db); } 

The error also indicates that the problem is in this method:

 @Override 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.setName(cursor.getString(0)); fruit.setColor(cursor.getString(1)); fruit.setPrice(Integer.parseInt(cursor.getString(2))); fruitList.add(fruit); } while (cursor.moveToNext()); } return fruitList; } 

    1 answer 1

    The onCreate(...) method is called when creating a database and serves to configure it (create tables, etc.).

    You have this method as follows:

     @Override public void onCreate(SQLiteDatabase db) { db.execSQL(CREATE_TABLE_FRUIT); onCreate(db); } 

    In this method, you create a table and for some reason you explicitly call the onCreate(...) method onCreate(...) , creating a recursion. At the same time, the request to create a table is re-executed, which causes an exception:

     SQLiteException: table ContactFruit already exists 

    because this table already exists (it was created when the onCreate(...) method was first called).

    This problem can be solved by removing the onCreate(db); call onCreate(db); from the onCreate(...) method.