android – Error inserting data into SQLite : java.lang.IllegalStateException: getDatabase called recursively


1-07 22:50:59.756 11315-11315/ E/AndroidRuntime: FATAL EXCEPTION: main Process:, PID: 11315
java.lang.IllegalStateException: getDatabase called recursively
at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked( at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase( at .java:57) at

Here's 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;

        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"
                    + ")";

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

        private void _insert(SQLiteDatabase dbsql) {
            dbsql = this.getWritableDatabase();
            nomeAuto.add(0,"Uno 2017");
            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);

            } finally {

        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();
               = cursor.getString(cursor.getColumnIndex(AUTO_COLUMN_ID));
               = 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));
                    } while (cursor.moveToNext());
                return autos;
            }finally {

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


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

In the _insert(SQLiteDatabase dbsql) delete the 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 dbsql.close(); line dbsql.close(); , the SQLiteDatabase reference is passed to you, so you must leave it open: whoever created it is responsible for closing it.

Since there is nothing to do in case of an exception, drop the try/finally .

Scroll to Top