SqliteOpenHelper for Mode_World_Readable,怎么样?

发布于 2024-10-21 04:53:24 字数 363 浏览 4 评论 0原文

SqliteOpenHelper 默认情况下以 mode_private 创建数据库。我们如何使用 SqliteOpenHelper 创建世界可读/可写数据库?

或者我需要使用Context.openOrCreateDatabase()

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

趴在窗边数星星i 2024-10-28 04:53:24

我们如何使用 SqliteOpenHelper 创建世界可读/可写数据库?

我们不能那样做。 ContextImpl.openOrCreateDatabase() 实际上使用 SQLiteDatabase.openOrCreateDatabase() 方法打开/创建数据库,然后使用类 android.os.FileUtils 设置数据库文件的权限 这不是公共 API 的一部分。因此,除非您想使用反射,否则使数据库世界可读/可写的唯一可能方法是使用 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().

街道布景 2024-10-28 04:53:24

将数据库打开为世界可读/可写绝对是可能的。那么数据库的必要性是什么呢?您可以使用文件代替..!!

不建议将数据库打开为全局可读/可写。

永远记住这一点:

  • 仅在必要时打开数据库,
    因为它的成本很高。

  • 仅在必要的模式下打开
    读取或写入或两者。

  • 进行操作后立即关闭
    结束了。

如果您想在应用程序之间共享数据库或资源,可以使用 SharedUserID。为了使用 SharedUserID,应用程序必须使用相同的密钥进行签名。

有关详细信息,请参阅我在 sree.cc 上发布的帖子

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

这是相同的代码片段:

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());
       }
}

不推荐:

如果您想要单词可读,然后将其创建为世界可读。使用 openFileOutput()openOrCreateDatabase()。将创建数据库的上下文声明为全局可读。请注意,此方法无论如何都不安全。

在此做个参考。

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文