java - Android:从 Assets 文件夹复制数据库

标签 java android sqlite

<分区>

我想将示例数据库从 Assets 文件夹复制到/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();
}

最佳答案

试试下面的代码,让我知道它是否工作正常?

 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.");
  }
  }

关于java - Android:从 Assets 文件夹复制数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20207581/

相关文章:

c# - 如何使用SQLiteCommand进行插入查询?

java - distanceTo() 整数溢出?

java - 方法不能应用于 java.lang.Object(应该接受特定类的对象作为参数)

ios - 如何使用 sqlite 在 ios 应用程序中使用 "read commited"隔离级别?

java - 第二次调用 FragmentTransaction.replace() 时出现 Android Fragment 转换错误

android - 使用带有 web View 的暗模式

iphone - 谁能告诉我为什么我的代码无法创建数据库和表,然后在其中存储数据。

java - 如何将应用程序使用情况记录到网站

Java jar 在运行时找不到类

android R.integer 在创建数组时返回不正确的极大值导致内存不足