2013-11-26 109 views
1

我想將我的樣本數據庫從我的資產文件夾複製到/ data/data/packagename/databases /目錄以便稍後在應用程序中使用它。Android:從資產文件夾複製數據庫

我已經看過很多教程和其他解決方案,像這裏(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

有,你可以用它來從數據庫中的資源文件夾中複製一個開源庫:https://github.com/jgilfelt/android-sqlite-asset-helper – rubenlop

+0

是否有關於如何使用任何樣品從資源文件夾複製數據庫的開源庫?我仍然是這個領域的新手,仍然有很多東西需要學習。 –

+0

也確保你給予mkdir權限 - 即將父目錄創建設置爲true。 –

回答

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

非常感謝你...我已經嘗試過我們的代碼,它運作良好! :) –

+0

@FitriIzuan:最重要的 –

相關問題