Android:从资源文件夹复制数据库

时间:2013-11-26 02:47:00

标签: java android sqlite

我想将我的示例数据库从我的资产文件夹复制到/ data / data / packagename / databases /目录,以便稍后在应用程序中使用它。

我已经查看了许多教程和其他解决方案,例如此处(Copy Database from assets folder in unrooted device)和此处(copy database from assets to databases folder),但它对我不起作用。我没有错误,但已复制的数据库为空。我的编码有问题吗?

这是我复制数据库的编码。

       @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getWindow().requestFeature(Window.FEATURE_ACTION_BAR);
    setContentView(R.layout.activity_dictionary_type);

    File dbfile=new File("data/data/com.example.myidictionary/databases","DefinitionDB");

    if (!dbfile.exists()) {
        try {
            copyDatabase(dbfile);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        Toast.makeText(getApplicationContext(), "Sample data is being copied", Toast.LENGTH_LONG).show();
    }
    else
        Toast.makeText(getApplicationContext(), "Database Exist", Toast.LENGTH_LONG).show();

}

private void copyDatabase(File dbFile) throws IOException 
{
    InputStream is = this.getAssets().open("DefinitionDB");
    OutputStream os = new FileOutputStream(dbFile);

    byte[] buffer = new byte[1024];

    while (is.read(buffer) > 0) 
    {
        os.write(buffer);
    }

    os.flush();
    os.close();
    is.close();
}

1 个答案:

答案 0 :(得分:4)

尝试下面的代码并告诉我它是否正常工作?

 public class DataBaseWrapper extends SQLiteOpenHelper
 {
  private static String TAG = DataBaseWrapper.class.getName();
  private  String DB_PATH; //= "/data/data/com.example.yourproject/databases/";
  private static String DB_NAME = "Database.sqlite";
  private SQLiteDatabase myDataBase = null; 
  private final Context myContext;

  public DataBaseWrapper(Context context) 
  {
     super(context, DB_NAME, null, 1);

      this.myContext = context;
      DB_PATH="/data/data/" + context.getPackageName() + "/" + "databases/";
      Log.v("log_tag", "DBPath: " + DB_PATH);
     //  File f=getDatabasePath(DB_NAME);
  } 

  public void createDataBase() throws IOException{
   boolean dbExist = checkDataBase();
   if(dbExist){
    Log.v("log_tag", "database does exist");
    }else{
     Log.v("log_tag", "database does not exist");
     this.getReadableDatabase();
     try {
      copyDataBase();
        } catch (IOException e) {
      throw new Error("Error copying database");
      }
    }
   }

  private void copyDataBase() throws IOException{
  InputStream myInput = myContext.getAssets().open(DB_NAME);
  String outFileName = DB_PATH + DB_NAME;
  OutputStream myOutput = new FileOutputStream(outFileName);
  byte[] buffer = new byte[1024];
  int length;
   while ((length = myInput.read(buffer))>0){
    myOutput.write(buffer, 0, length);
   }
   myOutput.flush();
   myOutput.close();
   myInput.close();
  }

  private boolean checkDataBase(){

     File dbFile = new File(DB_PATH + DB_NAME); 
     //Log.v("dbFile", dbFile + "   "+ dbFile.exists()); 
     return dbFile.exists(); 

 }

 public boolean openDataBase() throws SQLException
 {
    String mPath = DB_PATH + DB_NAME; 
    //Log.v("mPath", mPath); 
    myDataBase = SQLiteDatabase.openDatabase(mPath, null, SQLiteDatabase.CREATE_IF_NECESSARY); 
    //mDataBase = SQLiteDatabase.openDatabase(mPath, null, SQLiteDatabase.NO_LOCALIZED_COLLATORS); 
    return myDataBase != null; 

 }


  @Override
  public synchronized void close() 
  {
     if(myDataBase != null)
      myDataBase.close();
     super.close();
  }

 @Override
 public void onCreate(SQLiteDatabase db) 
 {


  }

 @Override
 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) 
 {
    Log.v(TAG, "Upgrading database, this will drop database and recreate.");
  }
  }