0

SqliteOpenHelper by default creates database in mode_private. How can we create world readable/writable db using SqliteOpenHelper ?

Or Else Do I need to use Context.openOrCreateDatabase()

ankitjaininfo
  • 11,961
  • 7
  • 52
  • 75
  • Mhm.. is there really a way to make it world readable? In the dev guide is written: `SQLite Databases: Store structured data in a *private* database.` – Chris Mar 10 '11 at 08:17
  • Yes, there is if you refer Context.openOrCreateDatabase() it accepts world read/write. However I could not find a way using SQLiteOpenHelper, which by default passes 0 (i.e. mode_private) – ankitjaininfo Mar 10 '11 at 09:47

2 Answers2

0

How can we create world readable/writable db using SqliteOpenHelper ?

We can't do that. ContextImpl.openOrCreateDatabase() actually opens/creates database using SQLiteDatabase.openOrCreateDatabase() method and then sets the permission for the database file using class android.os.FileUtils which is not part of the public API. So unless you want to use reflection the only possible way to make database world-readable/-writable is to use Context.openOrCreateDatabase().

Volo
  • 28,673
  • 12
  • 97
  • 125
  • And if the goal is to use the DB from another app, then you should probably be using a Content Provider. – rogerkk Mar 20 '11 at 07:52
0

Opening a Database as world Readable/writable is definitely possible. But then what is the necessity of a Database? You can use files instead..!!

Opening a Database as world Readable/writable is not recommended.

Always remember this:

  • Open a database only when necessary, because it is costly.

  • Open only in the mode necessary either read or write or both.

  • Close it as soon as the manipulations are over.

If you want to share a Database or a resource among your applications you can use SharedUserID. Inorder to use SharedUserID, the applications must be signed by the same Key.

For More info see my post here at sree.cc

http://sree.cc/google/android/sharing-resources-in-different-aplications-using-shareduserid

Here is the code Snippet for the same:

private void getDB() {
        //accessing file using SHAREDUSERID
        try 
        { 
            //creating context from mainAPP for accessing database
            ctx = createPackageContext(
            "com.schogini.sharedDB.pack",
            Context.CONTEXT_IGNORE_SECURITY);
            if(ctx==null){
                return;
            }
        }
        catch (PackageManager.NameNotFoundException e) { 
            //package not found
            Log.e("Error",e.getMessage());
        }
        try
        { 
            File myDbFile = ctx.getDatabasePath("sharedDB.db");
            if (myDbFile.exists()) 
            {
                dbb = openOrCreateDatabase(myDbFile.getPath(), SQLiteDatabase.OPEN_READWRITE, null);
                dbb.setVersion(1);
                dbb.setLocale(Locale.getDefault());
                dbb.setLockingEnabled(true);
                try{
                    cur=dbb.rawQuery("select * from TABLENAME;",null);
                    try{
                        cur.moveToFirst();
                        int k=cur.getColumnCount();
                        lv_arr=new String[k];
                        for(int i=0;i<k;i++)
                        {
                            lv_arr[i]=""+cur.getString(i);
                            Toast.makeText(LaunchActivity.this, "Data "+i, Toast.LENGTH_SHORT).show();
                        }
                    }
                    catch(Exception e)
                    {
                        //may be an empty database
                        Log.e("Error",e.getMessage());
                        dbb.close();
                    }
                }
                catch(Exception e)
                {
                    Log.e("Error",e.getMessage());
                    dbb.close();
                }
            }
            else
            {
                //database not found
                Toast.makeText(LaunchActivity.this, "DataBase Doesnot Exist", Toast.LENGTH_SHORT).show();
            }
        }
       catch(Exception e)
       {
            Log.i("\n\nTAG",e.toString());
       }
}

Not recommended:

If you want the word readable, then create it world readable. Use openFileOutput() or openOrCreateDatabase(). Declare the context that creates the DB as World readable. Note this method is not safe by any means.

Do a reference here.

http://developer.android.com/reference/android/content/Context.html#MODE_WORLD_READABLE