ORMLite - SQLCipher Data is Lost from Retrieve Data After Force Close / Kill Apps From Recent Apps












0















i want to implement SQLChiper with ORMLite this library, i want to set build gradle follow us :



build.gradle



implementation 'com.j256.ormlite.cipher:ormlite-sqlcipher:1.2@aar'
implementation 'com.j256.ormlite:ormlite-core:5.1'
implementation 'com.j256.ormlite:ormlite-android:5.1'
implementation 'net.zetetic:android-database-sqlcipher:3.5.9@aar'


DatabaseHelper.java



public class DatabaseHelper extends OrmLiteSqliteOpenHelper {

private static final String DATABASE_NAME = "ut_v2_secure.db";
private static final String DATABASE_PATH = String.format("/data/data/%s/databases/", BuildConfig.APPLICATION_ID);
private static final int DATABASE_VERSION = 1;
private static final String PASSWORD = "Testing Apps";

private static DatabaseHelper sInstance;

private Dao<Profile, Integer> profileDao;

public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);

// Decrypt Database
getWritableDatabase(getPassword());
}

@Override
public void onCreate(SQLiteDatabase database, ConnectionSource connectionSource) {
try {

setNullDaoObject();

TableUtils.createTableIfNotExists(connectionSource, Profile.class);



} catch (SQLException | java.sql.SQLException e) {
LogUtil.debug("Could not create new table: " + e);
}
}

private void setNullDaoObject() {
profileDao = null;
}

@Override
public void onUpgrade(net.sqlcipher.database.SQLiteDatabase db, ConnectionSource connectionSource, int oldVersion, int newVersion) {
Log.d("DatabaseHelper", "onUpgrade: " + oldVersion + " to " + newVersion);

try {
onCreate(db, connectionSource);
} catch (SQLException | java.sql.SQLException e) {
LogUtil.debug("Could not create new table: " + e);
}
}

@Override
protected String getPassword() {
return PASSWORD;
}

public static synchronized DatabaseHelper getInstance(Context context) {
if (sInstance == null) {
sInstance = new DatabaseHelper(context.getApplicationContext());
}

return sInstance;
}
public Dao<Profile, Integer> getProfileDao() {
if (profileDao == null) {
try {
profileDao = getDao(Profile.class);
} catch (java.sql.SQLException e) {
LogUtil.debug("Failed get profileDao: " + e.getMessage());
}
}
return profileDao;
}

@Override
public void close() {
setNullDaoObject();

super.close();
}

}


Initialize Library on App.java :



App.java



package com.devleoper.id;

import android.app.Activity;
import android.app.Application;
import android.os.Bundle;

import net.sqlcipher.database.SQLiteDatabase;

/**
* Created by developer on 11/14/18.
*/
public class App extends Application{

@Override
public void onCreate() {
super.onCreate();

// Iniitalize SQL Chiper
SQLiteDatabase.loadLibs(this);

}

}


i want call helper database on BaseActivity to Another Activity



BaseActivity.java



public abstract class BaseActivity extends AppCompatActivity {

public DatabaseHelper databaseHelper = null;

@Override
public void onCreate(Bundle savedInstanceState, PersistableBundle persistentState) {
super.onCreate(savedInstanceState, persistentState);
}

/**
* You'll need this in your class to get the helper from the manager once per class.
*/
public DatabaseHelper getDatabaseHelper() {
if (databaseHelper == null) {
databaseHelper = DatabaseHelper.getInstance(this);
}
return databaseHelper;
}


/**
* You'll need this in your class to release the helper when done.
*/
public void releaseDatabaseHelper() {
if (databaseHelper != null) {
OpenHelperManager.releaseHelper();
databaseHelper = null;
}
}


@Override
protected void onDestroy() {
super.onDestroy();
// release DB
releaseDatabaseHelper();
}
}


MainActivity.class



public class MainActivity extends BaseActivity {


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash_screen);

try {
// Get Data List Profile
List<Profile> profiles = getBaseActivity().getDatabaseHelper().getProfileDao().queryForAll();
if (profiles != null && profiles.size() > 0) {
profile = profiles.get(0);
}
} catch (SQLException e) {
e.printStackTrace();
}
}

@Override
public void onBackPressed() {
}



@Override
protected void onDestroy() {
super.onDestroy();
}

}


Problem
I found the problem as follows :




  1. when I retrieve data for the first time the data application can be
    on the MainActivity.java list, List size 100 from
    Library Ormlite-SQLChiper.

  2. then I press the home button on the application, then I press the
    recent apps button to apps running, to do the force / Kill apps.

  3. Then I reopen the application.

  4. When I retrive the Data List in MainActivity.java, the
    data size is 0


  5. the data is lost.


i want to use for function retrieve data is :



getBaseActivity().getDatabaseHelper().getProfileDao().queryForAll();


is there a solution to this problem ?, is my coding wrong?
or incorrect to retrieve when the application is force close from recent apps and reopen.



Thank you.










share|improve this question



























    0















    i want to implement SQLChiper with ORMLite this library, i want to set build gradle follow us :



    build.gradle



    implementation 'com.j256.ormlite.cipher:ormlite-sqlcipher:1.2@aar'
    implementation 'com.j256.ormlite:ormlite-core:5.1'
    implementation 'com.j256.ormlite:ormlite-android:5.1'
    implementation 'net.zetetic:android-database-sqlcipher:3.5.9@aar'


    DatabaseHelper.java



    public class DatabaseHelper extends OrmLiteSqliteOpenHelper {

    private static final String DATABASE_NAME = "ut_v2_secure.db";
    private static final String DATABASE_PATH = String.format("/data/data/%s/databases/", BuildConfig.APPLICATION_ID);
    private static final int DATABASE_VERSION = 1;
    private static final String PASSWORD = "Testing Apps";

    private static DatabaseHelper sInstance;

    private Dao<Profile, Integer> profileDao;

    public DatabaseHelper(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);

    // Decrypt Database
    getWritableDatabase(getPassword());
    }

    @Override
    public void onCreate(SQLiteDatabase database, ConnectionSource connectionSource) {
    try {

    setNullDaoObject();

    TableUtils.createTableIfNotExists(connectionSource, Profile.class);



    } catch (SQLException | java.sql.SQLException e) {
    LogUtil.debug("Could not create new table: " + e);
    }
    }

    private void setNullDaoObject() {
    profileDao = null;
    }

    @Override
    public void onUpgrade(net.sqlcipher.database.SQLiteDatabase db, ConnectionSource connectionSource, int oldVersion, int newVersion) {
    Log.d("DatabaseHelper", "onUpgrade: " + oldVersion + " to " + newVersion);

    try {
    onCreate(db, connectionSource);
    } catch (SQLException | java.sql.SQLException e) {
    LogUtil.debug("Could not create new table: " + e);
    }
    }

    @Override
    protected String getPassword() {
    return PASSWORD;
    }

    public static synchronized DatabaseHelper getInstance(Context context) {
    if (sInstance == null) {
    sInstance = new DatabaseHelper(context.getApplicationContext());
    }

    return sInstance;
    }
    public Dao<Profile, Integer> getProfileDao() {
    if (profileDao == null) {
    try {
    profileDao = getDao(Profile.class);
    } catch (java.sql.SQLException e) {
    LogUtil.debug("Failed get profileDao: " + e.getMessage());
    }
    }
    return profileDao;
    }

    @Override
    public void close() {
    setNullDaoObject();

    super.close();
    }

    }


    Initialize Library on App.java :



    App.java



    package com.devleoper.id;

    import android.app.Activity;
    import android.app.Application;
    import android.os.Bundle;

    import net.sqlcipher.database.SQLiteDatabase;

    /**
    * Created by developer on 11/14/18.
    */
    public class App extends Application{

    @Override
    public void onCreate() {
    super.onCreate();

    // Iniitalize SQL Chiper
    SQLiteDatabase.loadLibs(this);

    }

    }


    i want call helper database on BaseActivity to Another Activity



    BaseActivity.java



    public abstract class BaseActivity extends AppCompatActivity {

    public DatabaseHelper databaseHelper = null;

    @Override
    public void onCreate(Bundle savedInstanceState, PersistableBundle persistentState) {
    super.onCreate(savedInstanceState, persistentState);
    }

    /**
    * You'll need this in your class to get the helper from the manager once per class.
    */
    public DatabaseHelper getDatabaseHelper() {
    if (databaseHelper == null) {
    databaseHelper = DatabaseHelper.getInstance(this);
    }
    return databaseHelper;
    }


    /**
    * You'll need this in your class to release the helper when done.
    */
    public void releaseDatabaseHelper() {
    if (databaseHelper != null) {
    OpenHelperManager.releaseHelper();
    databaseHelper = null;
    }
    }


    @Override
    protected void onDestroy() {
    super.onDestroy();
    // release DB
    releaseDatabaseHelper();
    }
    }


    MainActivity.class



    public class MainActivity extends BaseActivity {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_splash_screen);

    try {
    // Get Data List Profile
    List<Profile> profiles = getBaseActivity().getDatabaseHelper().getProfileDao().queryForAll();
    if (profiles != null && profiles.size() > 0) {
    profile = profiles.get(0);
    }
    } catch (SQLException e) {
    e.printStackTrace();
    }
    }

    @Override
    public void onBackPressed() {
    }



    @Override
    protected void onDestroy() {
    super.onDestroy();
    }

    }


    Problem
    I found the problem as follows :




    1. when I retrieve data for the first time the data application can be
      on the MainActivity.java list, List size 100 from
      Library Ormlite-SQLChiper.

    2. then I press the home button on the application, then I press the
      recent apps button to apps running, to do the force / Kill apps.

    3. Then I reopen the application.

    4. When I retrive the Data List in MainActivity.java, the
      data size is 0


    5. the data is lost.


    i want to use for function retrieve data is :



    getBaseActivity().getDatabaseHelper().getProfileDao().queryForAll();


    is there a solution to this problem ?, is my coding wrong?
    or incorrect to retrieve when the application is force close from recent apps and reopen.



    Thank you.










    share|improve this question

























      0












      0








      0








      i want to implement SQLChiper with ORMLite this library, i want to set build gradle follow us :



      build.gradle



      implementation 'com.j256.ormlite.cipher:ormlite-sqlcipher:1.2@aar'
      implementation 'com.j256.ormlite:ormlite-core:5.1'
      implementation 'com.j256.ormlite:ormlite-android:5.1'
      implementation 'net.zetetic:android-database-sqlcipher:3.5.9@aar'


      DatabaseHelper.java



      public class DatabaseHelper extends OrmLiteSqliteOpenHelper {

      private static final String DATABASE_NAME = "ut_v2_secure.db";
      private static final String DATABASE_PATH = String.format("/data/data/%s/databases/", BuildConfig.APPLICATION_ID);
      private static final int DATABASE_VERSION = 1;
      private static final String PASSWORD = "Testing Apps";

      private static DatabaseHelper sInstance;

      private Dao<Profile, Integer> profileDao;

      public DatabaseHelper(Context context) {
      super(context, DATABASE_NAME, null, DATABASE_VERSION);

      // Decrypt Database
      getWritableDatabase(getPassword());
      }

      @Override
      public void onCreate(SQLiteDatabase database, ConnectionSource connectionSource) {
      try {

      setNullDaoObject();

      TableUtils.createTableIfNotExists(connectionSource, Profile.class);



      } catch (SQLException | java.sql.SQLException e) {
      LogUtil.debug("Could not create new table: " + e);
      }
      }

      private void setNullDaoObject() {
      profileDao = null;
      }

      @Override
      public void onUpgrade(net.sqlcipher.database.SQLiteDatabase db, ConnectionSource connectionSource, int oldVersion, int newVersion) {
      Log.d("DatabaseHelper", "onUpgrade: " + oldVersion + " to " + newVersion);

      try {
      onCreate(db, connectionSource);
      } catch (SQLException | java.sql.SQLException e) {
      LogUtil.debug("Could not create new table: " + e);
      }
      }

      @Override
      protected String getPassword() {
      return PASSWORD;
      }

      public static synchronized DatabaseHelper getInstance(Context context) {
      if (sInstance == null) {
      sInstance = new DatabaseHelper(context.getApplicationContext());
      }

      return sInstance;
      }
      public Dao<Profile, Integer> getProfileDao() {
      if (profileDao == null) {
      try {
      profileDao = getDao(Profile.class);
      } catch (java.sql.SQLException e) {
      LogUtil.debug("Failed get profileDao: " + e.getMessage());
      }
      }
      return profileDao;
      }

      @Override
      public void close() {
      setNullDaoObject();

      super.close();
      }

      }


      Initialize Library on App.java :



      App.java



      package com.devleoper.id;

      import android.app.Activity;
      import android.app.Application;
      import android.os.Bundle;

      import net.sqlcipher.database.SQLiteDatabase;

      /**
      * Created by developer on 11/14/18.
      */
      public class App extends Application{

      @Override
      public void onCreate() {
      super.onCreate();

      // Iniitalize SQL Chiper
      SQLiteDatabase.loadLibs(this);

      }

      }


      i want call helper database on BaseActivity to Another Activity



      BaseActivity.java



      public abstract class BaseActivity extends AppCompatActivity {

      public DatabaseHelper databaseHelper = null;

      @Override
      public void onCreate(Bundle savedInstanceState, PersistableBundle persistentState) {
      super.onCreate(savedInstanceState, persistentState);
      }

      /**
      * You'll need this in your class to get the helper from the manager once per class.
      */
      public DatabaseHelper getDatabaseHelper() {
      if (databaseHelper == null) {
      databaseHelper = DatabaseHelper.getInstance(this);
      }
      return databaseHelper;
      }


      /**
      * You'll need this in your class to release the helper when done.
      */
      public void releaseDatabaseHelper() {
      if (databaseHelper != null) {
      OpenHelperManager.releaseHelper();
      databaseHelper = null;
      }
      }


      @Override
      protected void onDestroy() {
      super.onDestroy();
      // release DB
      releaseDatabaseHelper();
      }
      }


      MainActivity.class



      public class MainActivity extends BaseActivity {


      @Override
      protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_splash_screen);

      try {
      // Get Data List Profile
      List<Profile> profiles = getBaseActivity().getDatabaseHelper().getProfileDao().queryForAll();
      if (profiles != null && profiles.size() > 0) {
      profile = profiles.get(0);
      }
      } catch (SQLException e) {
      e.printStackTrace();
      }
      }

      @Override
      public void onBackPressed() {
      }



      @Override
      protected void onDestroy() {
      super.onDestroy();
      }

      }


      Problem
      I found the problem as follows :




      1. when I retrieve data for the first time the data application can be
        on the MainActivity.java list, List size 100 from
        Library Ormlite-SQLChiper.

      2. then I press the home button on the application, then I press the
        recent apps button to apps running, to do the force / Kill apps.

      3. Then I reopen the application.

      4. When I retrive the Data List in MainActivity.java, the
        data size is 0


      5. the data is lost.


      i want to use for function retrieve data is :



      getBaseActivity().getDatabaseHelper().getProfileDao().queryForAll();


      is there a solution to this problem ?, is my coding wrong?
      or incorrect to retrieve when the application is force close from recent apps and reopen.



      Thank you.










      share|improve this question














      i want to implement SQLChiper with ORMLite this library, i want to set build gradle follow us :



      build.gradle



      implementation 'com.j256.ormlite.cipher:ormlite-sqlcipher:1.2@aar'
      implementation 'com.j256.ormlite:ormlite-core:5.1'
      implementation 'com.j256.ormlite:ormlite-android:5.1'
      implementation 'net.zetetic:android-database-sqlcipher:3.5.9@aar'


      DatabaseHelper.java



      public class DatabaseHelper extends OrmLiteSqliteOpenHelper {

      private static final String DATABASE_NAME = "ut_v2_secure.db";
      private static final String DATABASE_PATH = String.format("/data/data/%s/databases/", BuildConfig.APPLICATION_ID);
      private static final int DATABASE_VERSION = 1;
      private static final String PASSWORD = "Testing Apps";

      private static DatabaseHelper sInstance;

      private Dao<Profile, Integer> profileDao;

      public DatabaseHelper(Context context) {
      super(context, DATABASE_NAME, null, DATABASE_VERSION);

      // Decrypt Database
      getWritableDatabase(getPassword());
      }

      @Override
      public void onCreate(SQLiteDatabase database, ConnectionSource connectionSource) {
      try {

      setNullDaoObject();

      TableUtils.createTableIfNotExists(connectionSource, Profile.class);



      } catch (SQLException | java.sql.SQLException e) {
      LogUtil.debug("Could not create new table: " + e);
      }
      }

      private void setNullDaoObject() {
      profileDao = null;
      }

      @Override
      public void onUpgrade(net.sqlcipher.database.SQLiteDatabase db, ConnectionSource connectionSource, int oldVersion, int newVersion) {
      Log.d("DatabaseHelper", "onUpgrade: " + oldVersion + " to " + newVersion);

      try {
      onCreate(db, connectionSource);
      } catch (SQLException | java.sql.SQLException e) {
      LogUtil.debug("Could not create new table: " + e);
      }
      }

      @Override
      protected String getPassword() {
      return PASSWORD;
      }

      public static synchronized DatabaseHelper getInstance(Context context) {
      if (sInstance == null) {
      sInstance = new DatabaseHelper(context.getApplicationContext());
      }

      return sInstance;
      }
      public Dao<Profile, Integer> getProfileDao() {
      if (profileDao == null) {
      try {
      profileDao = getDao(Profile.class);
      } catch (java.sql.SQLException e) {
      LogUtil.debug("Failed get profileDao: " + e.getMessage());
      }
      }
      return profileDao;
      }

      @Override
      public void close() {
      setNullDaoObject();

      super.close();
      }

      }


      Initialize Library on App.java :



      App.java



      package com.devleoper.id;

      import android.app.Activity;
      import android.app.Application;
      import android.os.Bundle;

      import net.sqlcipher.database.SQLiteDatabase;

      /**
      * Created by developer on 11/14/18.
      */
      public class App extends Application{

      @Override
      public void onCreate() {
      super.onCreate();

      // Iniitalize SQL Chiper
      SQLiteDatabase.loadLibs(this);

      }

      }


      i want call helper database on BaseActivity to Another Activity



      BaseActivity.java



      public abstract class BaseActivity extends AppCompatActivity {

      public DatabaseHelper databaseHelper = null;

      @Override
      public void onCreate(Bundle savedInstanceState, PersistableBundle persistentState) {
      super.onCreate(savedInstanceState, persistentState);
      }

      /**
      * You'll need this in your class to get the helper from the manager once per class.
      */
      public DatabaseHelper getDatabaseHelper() {
      if (databaseHelper == null) {
      databaseHelper = DatabaseHelper.getInstance(this);
      }
      return databaseHelper;
      }


      /**
      * You'll need this in your class to release the helper when done.
      */
      public void releaseDatabaseHelper() {
      if (databaseHelper != null) {
      OpenHelperManager.releaseHelper();
      databaseHelper = null;
      }
      }


      @Override
      protected void onDestroy() {
      super.onDestroy();
      // release DB
      releaseDatabaseHelper();
      }
      }


      MainActivity.class



      public class MainActivity extends BaseActivity {


      @Override
      protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_splash_screen);

      try {
      // Get Data List Profile
      List<Profile> profiles = getBaseActivity().getDatabaseHelper().getProfileDao().queryForAll();
      if (profiles != null && profiles.size() > 0) {
      profile = profiles.get(0);
      }
      } catch (SQLException e) {
      e.printStackTrace();
      }
      }

      @Override
      public void onBackPressed() {
      }



      @Override
      protected void onDestroy() {
      super.onDestroy();
      }

      }


      Problem
      I found the problem as follows :




      1. when I retrieve data for the first time the data application can be
        on the MainActivity.java list, List size 100 from
        Library Ormlite-SQLChiper.

      2. then I press the home button on the application, then I press the
        recent apps button to apps running, to do the force / Kill apps.

      3. Then I reopen the application.

      4. When I retrive the Data List in MainActivity.java, the
        data size is 0


      5. the data is lost.


      i want to use for function retrieve data is :



      getBaseActivity().getDatabaseHelper().getProfileDao().queryForAll();


      is there a solution to this problem ?, is my coding wrong?
      or incorrect to retrieve when the application is force close from recent apps and reopen.



      Thank you.







      java android database ormlite sqlcipher-android






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 14 '18 at 12:47









      khailani ridwankhailani ridwan

      12




      12
























          0






          active

          oldest

          votes











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53300605%2formlite-sqlcipher-data-is-lost-from-retrieve-data-after-force-close-kill-app%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53300605%2formlite-sqlcipher-data-is-lost-from-retrieve-data-after-force-close-kill-app%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Xamarin.iOS Cant Deploy on Iphone

          Glorious Revolution

          Dulmage-Mendelsohn matrix decomposition in Python