Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8875279
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T18:56:16+00:00 2026-06-14T18:56:16+00:00

I am trying to create a sqlCipher encrypted database in sdCard and then read

  • 0

I am trying to create a sqlCipher encrypted database in sdCard and then read from there and store values.

This is my code.

public class DataBaseHelper extends SQLiteOpenHelper 
{
    private static String TAG = "DataBaseHelper"; // Tag just for the LogCat window
    private static String DB_PATH ;//path of our database
    private static String DB_NAME = "application-database";// Database name
    private static int DATABASE_VERSION = 1;

    private SQLiteDatabase mDataBase; 
    private final Context mContext;

    private static final String DATABASE_CREATE_TABLE1 =
            "create table notes (_id integer primary key autoincrement, myval);";


    public DataBaseHelper(Context context) 
    {
        super(context, DB_NAME, null, DATABASE_VERSION );    
        this.mContext = context;
        DB_PATH = Environment.getExternalStorageDirectory() + "/Personal Folder/";
    }

    public void createDataBase() throws IOException
    {
        //If database not exists create it from the assets
        boolean mDataBaseExist = checkDataBase();
        if(!mDataBaseExist)
        {
          try 
          {
                File dbFile = new File(DB_PATH + DB_NAME);
                SQLiteDatabase.loadLibs(mContext);
                dbFile.mkdirs();
                dbFile.delete();
                SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(dbFile, "setPassword", null);
                db.execSQL(DATABASE_CREATE_TABLE1);
                Log.e(TAG, "createDatabase database created");
          } 
          catch (SQLException mIOException) 
          {
                throw new Error("Error Creating Database");
          }
        }
    }

    private boolean checkDataBase()
    {
       File dbFile = new File(DB_PATH + DB_NAME);
       return dbFile.exists();
    }

    //Open the database, so we can query it
    public boolean openDataBase() throws SQLException
    {
        String mPath = DB_PATH + DB_NAME;
        SQLiteDatabase.loadLibs(mContext);
        mDataBase = SQLiteDatabase.openDatabase(mPath, "setPassword", null, SQLiteDatabase.CREATE_IF_NECESSARY);
        return mDataBase != null;
    }

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

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) 
    {
       Log.w(TAG, "Upgrading database from version " + oldVersion + " to " + newVersion + ", which will destroy all old data");

       /*
        if (oldVersion == 2)
        {
            db.execSQL("ALTER TABLE notes ADD " + KEY_DATA + " blog");
            db.execSQL("ALTER TABLE notes ADD " + KEY_TYPE + " text");

        }

        if (newVersion == 3)
        {
            db.execSQL("ALTER TABLE notes ADD " + KEY_TYPE + " text");
        }
        */
    }

    @Override
    public void onCreate(SQLiteDatabase arg0) {
        // TODO Auto-generated method stub

    }

}


public class PADatabaseAdapter 
{
    private static final String TAG = "DbAdapter";
    private final Context mContext;
    private DataBaseHelper mDbHelper;
    private SQLiteDatabase mDb;

    /**
     * Database creation sql statement
     */
    public PADatabaseAdapter(Context ctx)
    {
        this.mContext = ctx;
        mDbHelper = new DataBaseHelper(mContext);
    }

    public PADatabaseAdapter createDatabase() throws SQLException 
    {
        try 
        {
            mDbHelper.createDataBase();
        } 
        catch (IOException mIOException) 
        {
            Log.e(TAG, mIOException.toString() + "  UnableToCreateDatabase");
            throw new Error("UnableToCreateDatabase");
        }
        return this;
    }

    public PADatabaseAdapter open(String password) throws SQLException 
    {
        try 
        {
            mDbHelper.openDataBase(password);
            //mDbHelper.close();
            mDb = mDbHelper.getmDataBase();
                    // mDbHelper.getWritableDatabase(password);

        } 
        catch (SQLException mSQLException) 
        {
            Log.e(TAG, "open >>"+ mSQLException.toString());
            throw mSQLException;
        }
        System.gc();
        return this;
    }

    public boolean isOpen ()
    {
        if (mDb !=null)
            return mDb.isOpen();
        else
            return false;
    }

    public void rekey (String password)
    {
        mDb.execSQL("PRAGMA rekey = '" + password + "'");
        System.gc();
    }

    public void close() 
    {
        mDb.close();
        mDbHelper.close();
    }

This is the code I am using in my activity

   mContext = this;
   mDbHelper = new PADatabaseAdapter(this);        
   mDbHelper.createDatabase();

   mDbHelper.open("setPassword");
   long as = mDbHelper.createNote("abc");
   mDbHelper.close();

   mDbHelper.open("setPassword");
   Cursor mCursor =   mDbHelper.fetchAllNotes();
   mDbHelper.close();

The problem is, in db.exec(CREATE_tABLE) it either does not create table OR something else is wrong because long as = mDbHelper.createNote("abc"); gives error no such table notes

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T18:56:18+00:00Added an answer on June 14, 2026 at 6:56 pm

    If you look at your code, you will see:

    @Override
    public void onCreate(SQLiteDatabase arg0) {
        // TODO Auto-generated method stub
    
    }
    

    The idea is that you are supposed to replace the // TODO with actual code that populates the database. You can tell this reading the documentation for SQLiteOpenHelper:

    You create a subclass implementing onCreate(SQLiteDatabase), onUpgrade(SQLiteDatabase, int, int) and optionally onOpen(SQLiteDatabase), and this class takes care of opening the database if it exists, creating it if it does not, and upgrading it as necessary. Transactions are used to make sure the database is always in a sensible state.

    You then use getReadableDatabase() and getWritableDatabase() methods on SQLiteOpenHelper to access your database. In the case of SQLCipher for Android’s version of those methods, you pass the passphrase as a parameter.

    Hence, I recommend that you:

    1. Move SQLiteDatabase.loadLibs(mContext); to your constructor.

    2. Delete the rest of openDataBase(), and all of close() (since that code already exists)

    3. Rewrite checkDataBase() to get rid of the invalid DB_PATH and use getDatabasePath() instead

    4. Move db.execSQL(DATABASE_CREATE_TABLE1); to onCreate()

    5. Delete the rest of createDataBase()

    6. Use the SQLiteOpenHelper properly in your activity

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to follow this tutorial: http://sqlcipher.net/ios-tutorial/ I create a database called sqlcipher.db then
I'm trying to compile SQLCipher. I've downloaded source code from http://sqlcipher.net/ . Then I'm
I'm trying create simple application in C++. This application has to read from file
Trying to create Database as follows: USE Master GO IF NOT EXISTS(SELECT [Name] FROM
I trying create empty white image with php, this s my code $bg =
I am trying create system in which I can login from php and then
Trying to create a background-image slideshow and am getting this error... This is the
I'm trying create a log specifically for logging user account activity. I created this
I'm trying create an immutable object and initialise it from xml config file in
I trying create a class derivated from System.Web.UI.Page and in override Render i set

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.