Error inserting data into Sqlite : java.lang.Illegalstateexception: getDatabase called recursively

Asked

Viewed 45 times

3

1-07 22:50:59.756 11315-11315/com.dev.bob.rent E/Androidruntime: FATAL EXCEPTION: main Process: com.dev.bob.aluguel_automovel, 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.dev.bob.aluguel_automovel.Model.Dbautomoveis. _Insert(Dbautomoveis.java:57) at com.dev.bob.aluguel_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.

1 answer

0


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

In the method _insert(SQLiteDatabase dbsql) eliminate the line

dbsql = this.getWritableDatabase();

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

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

Since there is nothing to be done in case of any exception, delete the try/finally.

  • same error, had already tried the recursive term.

  • Also eliminate the dbsql.close();

  • worked thanks!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.