Error inserting data in SQLite: java.lang.IllegalStateException: getDatabase called recursively

3
  

1-07 22: 50: 59.756 11315-11315 / com.bob.airport_rentals   E / AndroidRuntime: FATAL EXCEPTION: main                                                                                 Process: com.bob.automotive rental, PID: 11315
  java.lang.IllegalStateException: getDatabase called recursively
  at   android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked (SQLiteOpenHelper.java:203)   at   android.database.sqlite.SQLiteOpenHelper.getWritableDatabase (SQLiteOpenHelper.java:163)   at   com.bob.alquiler_automovel.Model.DBAutomoveis._insert (DBAutomoveis.java:57)   at   com.bob.alquiler_automovel.Model.DBAutomoveis.onCreate (DBAutomoveis.java:47)

Follow my class:

 public class DBAutomoveis extends SQLiteOpenHelper {

        private static final String NOME_BD = "dbauto";
        private static final int VERSAO_BD = 1;
        private Context contexto;
        public static final String AUTO_TABLE_NAME = "tableauto";
        public static final String AUTO_COLUMN_ID = "id";
        public static final String AUTO_COLUMN_NAME = "name";
        public static final String AUTO_COLUMN_PLACA = "placa";
        public static final String AUTO_COLUMN_TYPE = "type";
        public static final String AUTO_COLUMN_DISPONIVEL = "disponivel";
        private static final ArrayList<String> nomeAuto = new ArrayList<String>();
        private static final ArrayList<String> placaAuto = new ArrayList<>();
        private static final ArrayList<String> typeAuto = new ArrayList<>();
        private static final int disponivelAuto = 0;
        private int i=0;
        public DBAutomoveis(Context context) {
            super(context, NOME_BD, null, VERSAO_BD);
            this.contexto = context;
        }

        @Override
        public void onCreate(SQLiteDatabase sqLiteDatabase) {
            String sql = "CREATE TABLE IF NOT EXISTS " + AUTO_TABLE_NAME + " ("
                    + AUTO_COLUMN_ID + " integer primary key,"
                    + AUTO_COLUMN_NAME + " text,"
                    + AUTO_COLUMN_PLACA + " text,"
                    + AUTO_COLUMN_TYPE + "text,"
                    + AUTO_COLUMN_DISPONIVEL + "integer"
                    + ")";
            sqLiteDatabase.execSQL(sql);
            _insert(sqLiteDatabase);
        }

        @Override
        public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
            sqLiteDatabase.execSQL("DROP TABLE IF EXISTS "+AUTO_TABLE_NAME);
            onCreate(sqLiteDatabase);
        }

        private void _insert(SQLiteDatabase dbsql) {
            dbsql = this.getWritableDatabase();
            nomeAuto.add(0,"Uno 2017");
            nomeAuto.add(1,"I30");
            nomeAuto.add(2,"Jeep");
            placaAuto.add(0,"HHB8767");
            placaAuto.add(1,"HGB8767");
            placaAuto.add(2,"AHC8756");
            typeAuto.add(0,"Basico");
            typeAuto.add(1,"Intermediário");
            typeAuto.add(2,"Executivo");
            try {
                ContentValues values = new ContentValues();
                for (i=0;nomeAuto.size() < 4;i++){
                    values.put(AUTO_COLUMN_ID, i);
                    values.put(AUTO_COLUMN_NAME, nomeAuto.get(i));
                    values.put(AUTO_COLUMN_PLACA, placaAuto.get(i));
                    values.put(AUTO_COLUMN_TYPE, typeAuto.get(i));
                    values.put(AUTO_COLUMN_DISPONIVEL, 0);
                    dbsql.insert(AUTO_TABLE_NAME,"",values);
                }

            } finally {
                dbsql.close();
            }
        }

        public ArrayList<Automoveis> getAutoByType(String type){
            ArrayList<Automoveis> autos = new ArrayList<>();
            String query = "SELECT * FROM "+AUTO_TABLE_NAME+" where type = '"+type+"'";
            SQLiteDatabase db = this.getWritableDatabase();
            Cursor cursor = db.rawQuery(query,null);
            try {
                Automoveis automoveis = null;
                if (cursor.moveToFirst()) {
                    do {
                        automoveis = new Automoveis();
                        automoveis.id = cursor.getString(cursor.getColumnIndex(AUTO_COLUMN_ID));
                        automoveis.name = cursor.getString(cursor.getColumnIndex(AUTO_COLUMN_NAME));
                        automoveis.placa = cursor.getString(cursor.getColumnIndex(AUTO_COLUMN_PLACA));
                        automoveis.type = cursor.getString(cursor.getColumnIndex(AUTO_COLUMN_TYPE));
                        automoveis.disponivel = cursor.getInt(cursor.getColumnIndex(AUTO_COLUMN_DISPONIVEL));
                        autos.add(automoveis);
                    } while (cursor.moveToNext());
                }
                return autos;
            }finally {
                db.close();
            }
        }
    }

How to resolve this issue? I just need to open the app with data already on the screen.

    
asked by anonymous 07.11.2017 / 23:54

1 answer

0

You are getting a connection to the bank without closing the previous link.

In method _insert(SQLiteDatabase dbsql) delete line

dbsql = this.getWritableDatabase();

dbsql already has a reference to the database, it was passed to the method in the SQLiteDatabase dbsql parameter.

Also remove the line dbsql.close(); , the SQLiteDatabase reference is passed to you so you should leave it open: whoever created it is responsible for closing it.

Since there is nothing to do in case of any exceptions, delete the try/finally .

    
08.11.2017 / 00:04