0

I got the same errors, even when I tried some solutions posted on stack.

Here's my logcat :

07-07 11:43:45.631: E/SQLiteDatabase(2841): close() was never explicitly called on database '/data/data/com.up.ridechaser/databases/DBs' 
07-07 11:43:45.631: E/SQLiteDatabase(2841): android.database.sqlite.DatabaseObjectNotClosedException: Application did not close the cursor or database object that was opened here
07-07 11:43:45.631: E/SQLiteDatabase(2841):     at android.database.sqlite.SQLiteDatabase.<init>(SQLiteDatabase.java:1943)
07-07 11:43:45.631: E/SQLiteDatabase(2841):     at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:1007)
07-07 11:43:45.631: E/SQLiteDatabase(2841):     at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:986)
07-07 11:43:45.631: E/SQLiteDatabase(2841):     at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:962)
07-07 11:43:45.631: E/SQLiteDatabase(2841):     at com.up.ridechaser.DataBaseHelper.openDataBase(DataBaseHelper.java:67)
07-07 11:43:45.631: E/SQLiteDatabase(2841):     at com.up.ridechaser.MainActivity.onCreate(MainActivity.java:39)
07-07 11:43:45.631: E/SQLiteDatabase(2841):     at android.app.Activity.performCreate(Activity.java:4466)
07-07 11:43:45.631: E/SQLiteDatabase(2841):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
07-07 11:43:45.631: E/SQLiteDatabase(2841):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
07-07 11:43:45.631: E/SQLiteDatabase(2841):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
07-07 11:43:45.631: E/SQLiteDatabase(2841):     at android.app.ActivityThread.access$600(ActivityThread.java:123)
07-07 11:43:45.631: E/SQLiteDatabase(2841):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
07-07 11:43:45.631: E/SQLiteDatabase(2841):     at android.os.Handler.dispatchMessage(Handler.java:99)
07-07 11:43:45.631: E/SQLiteDatabase(2841):     at android.os.Looper.loop(Looper.java:137)
07-07 11:43:45.631: E/SQLiteDatabase(2841):     at android.app.ActivityThread.main(ActivityThread.java:4424)
07-07 11:43:45.631: E/SQLiteDatabase(2841):     at java.lang.reflect.Method.invokeNative(Native Method)
07-07 11:43:45.631: E/SQLiteDatabase(2841):     at java.lang.reflect.Method.invoke(Method.java:511)
07-07 11:43:45.631: E/SQLiteDatabase(2841):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
07-07 11:43:45.631: E/SQLiteDatabase(2841):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
07-07 11:43:45.631: E/SQLiteDatabase(2841):     at dalvik.system.NativeStart.main(Native Method)

I tried to close my cursor and my database using this in DATABASEHELPER with this code:

EDIT :

public class DataBaseHelper extends SQLiteOpenHelper {

    String DB_PATH = null;
    private static String DB_NAME = "DBs";
    private SQLiteDatabase myDataBase;
    private final Context myContext;

    public DataBaseHelper(Context context) {
        super(context, DB_NAME, null, 10);
        this.myContext = context;
        this.DB_PATH = "/data/data/" + context.getPackageName() + "/" + "databases/";
        Log.e("Path 1", DB_PATH);
    }

    public void createDataBase() throws IOException {
        boolean dbExist = checkDataBase();
        if (dbExist) {
            Log.e("YDB", "existOne");
        } else {
            this.getWritableDatabase();
            try {
                copyDataBase();
            } catch (IOException e) {
                throw new Error("Error copying database");
            }
        }
    }

    private boolean checkDataBase() {
        File databasePath = myContext.getDatabasePath(DB_NAME);
        return databasePath.exists();
    }

    private void copyDataBase() throws IOException {
        InputStream myInput = myContext.getAssets().open(DB_NAME);
        String outFileName = DB_PATH + DB_NAME;
        OutputStream myOutput = new FileOutputStream(outFileName);
        byte[] buffer = new byte[10];
        int length;
        while ((length = myInput.read(buffer)) > 0) {
            myOutput.write(buffer, 0, length);
        }
        myOutput.flush();
        myOutput.close();
        myInput.close();

    }

    public void openDataBase() throws SQLException {
        String myPath = DB_PATH + DB_NAME;
        myDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE);
    }

    @Override
    public synchronized void close() {
        if (myDataBase != null)
            myDataBase.close();
        super.close();
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        if (newVersion > oldVersion)
            try {
                copyDataBase();
            } catch (IOException e) {
                e.printStackTrace();

            }
    }

    public ArrayList<String> getAll(String table, String column, int select, String[] selection, String[] value) {
        ArrayList<String> array_list = new ArrayList<String>();

        SQLiteDatabase db = this.getReadableDatabase();
        Cursor res = null;

        if (select == 1) {
            res = db.rawQuery("select * from " + table, null);

            res.moveToFirst();
            while (res.isAfterLast() == false) {
                array_list.add(res.getString(res.getColumnIndex(column)));
                res.moveToNext();
            }

        }

        else {
            res = db.rawQuery("SELECT * FROM " + table + " WHERE (" + selection[0] + "='" + value[0] + "')" + "AND ("
                    + selection[1] + "='" + value[1] + "')", null);

            res.moveToFirst();

            try {
                while (res.isAfterLast() == false) {
                    array_list.add(res.getString(res.getColumnIndex(column)));
                    res.moveToNext();

                }
            } catch (SQLiteException e) {

            } finally {
                res.close();

            }

        }

        return array_list;
    }

}

At same time, I added this code to all my activities :

@Override
    protected void onDestroy() {
        super.onDestroy();
        super.onDestroy();
        if (myDbHelper != null) {
            myDbHelper.close();
        }
    }

    public void onPause() {
        super.onPause();
        if (myDbHelper != null) {
            myDbHelper.close();
        }
    }

}

When I start my app , I get no errors , but when I pass to the second activity , errors get displayed !

S'ed Sckizo
  • 39
  • 1
  • 7
  • i found the answer for my problem , http://stackoverflow.com/questions/4557154/android-sqlite-db-when-to-close it solved everything – S'ed Sckizo Jul 07 '16 at 20:12

2 Answers2

1

Crash log complains you have some open cursor or db best is use finally clause to close cursor. only close db in some lifecycle method such as onStop or onDestroy not both not sure what sqlite does if you call close on already closed db.

0

android do not db close

android system default auto db close

 res.moveToFirst();
                while (res.isAfterLast() == false) {
                    array_list.add(res.getString(res.getColumnIndex(column)));
                    res.moveToNext();
                }
    -->

       while(res.moveToNext()){
     array_list.add(res.getString(res.getColumnIndex(column)));
    }


you can

try{
        while(res.moveToNext()){
     array_list.add(res.getString(res.getColumnIndex(column)));
    }


}
catch(SqlException e){

}
finlly{
res.close();


}
loadjang
  • 327
  • 1
  • 3
  • catch clause should only be added if you are interested to handle exception otherwise its bad programming practice to ignore exceptions....:) –  Jul 07 '16 at 13:49
  • Check your if clause cursor you don't close it. –  Jul 07 '16 at 14:34
  • I am talking about getall select ==1 –  Jul 07 '16 at 14:36