开发者

SqliteOpenHelper for Mode_World_Readable, how?

开发者 https://www.devze.com 2023-02-16 02:58 出处:网络
SqliteOpenHelper by defaul开发者_JAVA百科t creates database in mode_private. How can we create world readable/writable db using SqliteOpenHelper ?

SqliteOpenHelper by defaul开发者_JAVA百科t creates database in mode_private. How can we create world readable/writable db using SqliteOpenHelper ?

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


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().


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

0

精彩评论

暂无评论...
验证码 换一张
取 消