Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does SQLCipher allow to access Database by other app.(if DB in SD-CARD)

I am trying one thing in SQLCiper.

This thing is success in SQLite Database in SD-CARD

1- created one app firstApp. with Database in SD-CARD

2- created second app secondApp.

i am trying to read data from SD-CARD in my second App.

Edit:- My database in SDCARD.

public class SdcardCipherDataBase extends SQLiteOpenHelper
{

    public static final String  DATABASE_FILE_PATH = "/sdcard";
    public static final String  DATABASE_NAME = "sdCipherDatabase";
    public final static String NAME ="name";
    public final static String ADDRESS ="address";
    public final static String CITY ="city";

    public SdcardCipherDataBase(final Context context) {
        super(context,Environment.getExternalStorageDirectory()+File.separator+DATABASE_FILE_PATH + File.separator + DATABASE_NAME, null, 1);
    }


    @Override
    public void onCreate(SQLiteDatabase db) {
        try
        {  
            db.execSQL( "CREATE TABLE information(_id INTEGER PRIMARY KEY AUTOINCREMENT,name TEXT, address TEXT,city TEXT);");

        }

        catch (SQLiteException ex)
        {
            Log.e("Hello", "error -- " + ex.getMessage(), ex);
        }
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // TODO Auto-generated method stub
        db.execSQL("DROP Table mylistdata");
        onCreate(db);
    }
}

I am using this Database in same app:-

public void databasCollection()
    {
        SQLiteDatabase.loadLibs(this);
        File file = getDatabasePath("/mnt/sdcard/sdcard/sdCipherDatabase");
        file.mkdir();
        file.delete();
        db = SQLiteDatabase.openOrCreateDatabase(file, "myapp", null);
        sdb = new SdcardCipherDataBase(this);

        sdb.getWritableDatabase("myapp");
        ContentValues c = new ContentValues();
        c.put(SdcardCipherDataBase.NAME,"monty");
        c.put(SdcardCipherDataBase.ADDRESS,"BTM");
        c.put(SdcardCipherDataBase.CITY,"Bangalore");
        db.insert("information", SdcardCipherDataBase.NAME, c);
        db.close();
    }

but i want to use same database in other app aslo.

i am trying to do some thing like this

public void databaseFromOtherApp()
{
    SQLiteDatabase.loadLibs(this);
    File file = getDatabasePath("/mnt/sdcard/sdcard/sdCipherDatabase");
    db = SQLiteDatabase.openOrCreateDatabase(file, "myapp", null);
    dh = new DatabaseHelper(this);

    Cursor cursor = db.rawQuery("select * from information", null);
    cursor.moveToFirst();
    String s = cursor.getString(cursor.getColumnIndex(NAME));
    Toast.makeText(this,"Name is "
            +s, Toast.LENGTH_LONG).show();

    cursor.close();
    db.close();
}

but showing error .

 Caused by: net.sqlcipher.database.SQLiteException: no such table: information: , while compiling: select * from information
like image 235
Monty Avatar asked Jan 30 '13 12:01

Monty


1 Answers

This line in your second app is wrong:

dh = new DatabaseHelper(this);

You need an instance of SdcardCipherDataBase also in your second app, because that class knows about tables and table names. Currently you are using a plain DatabaseHelper which does not know what to look for.

UPDATE:
Even if you could do what you need, it's not a wise thing to do. As you can see, you'll need to copy all your database-related logic to any other app using it. What if you need a 3rd app to do the same? Copy again all the classes? What if you need to modify a table to add a new column? You'll have to modify classes in all the three apps. Normally you'll want to keep your database access layer in the first app, and provide access to other apps via a content provider.

like image 133
Mister Smith Avatar answered Sep 17 '22 09:09

Mister Smith