SqliteOpenHelper
默认情况下在mode_private
. 我们如何使用 SqliteOpenHelper 创建世界可读/可写数据库?
否则我是否需要使用Context.openOrCreateDatabase()
SqliteOpenHelper
默认情况下在mode_private
. 我们如何使用 SqliteOpenHelper 创建世界可读/可写数据库?
否则我是否需要使用Context.openOrCreateDatabase()
我们如何使用 SqliteOpenHelper 创建世界可读/可写数据库?
我们不能那样做。ContextImpl.openOrCreateDatabase()
实际上使用方法打开/创建数据库SQLiteDatabase.openOrCreateDatabase()
,然后使用android.os.FileUtils
不属于公共 API 的类设置数据库文件的权限。因此,除非您想使用反射,否则使数据库世界可读/可写的唯一可能方法是使用Context.openOrCreateDatabase()
.
绝对可以将数据库打开为世界可读/可写。但是,数据库的必要性是什么?您可以改用文件..!!
不建议将数据库打开为世界可读/可写。
永远记住这一点:
仅在必要时才打开数据库,因为它的成本很高。
仅在必要的模式下打开,读取或写入或两者兼而有之。
如果您想在您的应用程序之间共享数据库或资源,您可以使用 SharedUserID。为了使用 SharedUserID,应用程序必须由相同的 Key 签名。
有关更多信息,请参阅我在 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