2012-09-27 58 views
1

我有一个SQLite数据库(.db的),复制它的资产的文件夹,并有将其复制到/数据/数据/包名/数据库。问题是我没有数据库文件夹,并没有成功创建它。要创建“数据库”文件夹中存储SQLite数据库

String dirPath = "/data/data/com.gatec.douaa/databases/"; 
Log.d("Directory",dirPath); 
File projDir = new File(dirPath); 
if (!projDir.exists()) 
     projDir.mkdirs(); 

你能帮我创建该文件夹来存储数据库吗? 非常感谢。

+3

请考虑使用'SQLiteAssetHelper',它处理这一切为您:https://github.com/jgilfelt/android-sqlite-asset-helper – CommonsWare

+0

@CommonsWare :所以不需要将数据库复制到/ databases文件夹?优秀会尝试:)。 – androniennn

+0

数据库仍然需要在那里复制,只是这样做的重要举措是为你完成的。 – Barak

回答

0

在我experieces的“数据库”目录中创建您使用的数据库的第一次。因此,如果要从“资产”目录复制数据库,则必须强制在“data/data/package_name/databases”路径中创建数据库。 这只发生在棒棒糖(android 5),在我的经验。 然后你可以使用下面的代码:

File file = new File(context.getDatabasePath("database_name.db").getPath()); 
    if (!file.exists()) 
    { 
     try 
     { 
      file.createNewFile(); 
     } 
     catch (IOException e) 
     { 

     } 
    }