SqliteOpenHelper
默认在mode_private
中创建数据库。我们如何使用SqliteOpenHelper创建世界可读/可写的数据库?SqliteOpenHelper for Mode_World_Readable,怎么样?
要不然我需要使用Context.openOrCreateDatabase()
SqliteOpenHelper
默认在mode_private
中创建数据库。我们如何使用SqliteOpenHelper创建世界可读/可写的数据库?SqliteOpenHelper for Mode_World_Readable,怎么样?
要不然我需要使用Context.openOrCreateDatabase()
我们如何能够创造世界可读/写DB使用SqliteOpenHelper?
我们不能那样做。 ContextImpl.openOrCreateDatabase()
实际上使用SQLiteDatabase.openOrCreateDatabase()
方法打开/创建数据库,然后使用不属于公共API的类android.os.FileUtils
设置数据库文件的权限。因此,除非您想使用反射,否则使数据库具有世界可读/可写的唯一可能方式是使用Context.openOrCreateDatabase()
。
如果目标是使用其他应用程序的数据库,那么您应该使用Content Provider。 – rogerkk 2011-03-20 07:52:44
将数据库打开为世界可读可写是绝对有可能的。但是,数据库的必要性是什么?你可以使用文件来代替..!
将数据库打开为世界不推荐使用可读/写。只在必要时
打开一个数据库, 因为它是昂贵的:
一定要记住这一点。
只在需要的模式下打开,或者是 读取或写入,或者两者兼有。
如果要共享一个数据库或应用程序,你可以使用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
姆姆..是否真的有一种方法使它具有世界可读性?在开发指南中写道:'SQLite数据库:将结构化数据存储在* private *数据库中。' – Chris 2011-03-10 08:17:36
是的,如果您引用Context.openOrCreateDatabase()它接受世界读/写。不过,我找不到使用SQLiteOpenHelper的方法,默认情况下它传递0(即mode_private) – ankitjaininfo 2011-03-10 09:47:59