2014-03-30 68 views
0

大家好:在應用啓動時,我提出這要求大量的數據進行初始化一個應用程序,所以我認爲,在資產apk文件導出初始分貝夾。我可以將該數據庫複製到我的工作目錄,但是當我嘗試寫入/更新任何數據庫時出現問題,我不知道爲什麼它說它不可寫。我知道資產中的db不能被重寫。所以現在我想用我的內部創建的數據庫(通過代碼編寫)附加數據庫,以便我可以一次性複製包含數據的表。我沒有得到任何代碼幫助。附加資產文件夾中的數據庫文件,以我自己的數據庫SQLite的安卓

請讓我知道,如果有人知道的過程。

回答

0

,你可以嘗試使用此:

database = SQLiteDatabase.openDatabase(path, null, 
       SQLiteDatabase.OPEN_READWRITE); 
+0

複製到工作目錄後?如果是的話,然後嘗試沒有結果 – Saty

+0

我只是試圖在http://stackoverflow.com/questions/513084/how-to-ship-an-android-application-with-a-database張​​貼的代碼,它似乎工作thanx幫助.... – Saty

1

可以關注該代碼可以幫助您:

public class PrepopulatedDBOpenHelper extends SQLiteOpenHelper { 
    public static final String DB_NAME = "task_management"; 
    public static String DB_PATH; 
    private SQLiteDatabase database; 
    private Context context; 

    public static final String EMPLOYEE_TABLE = "employee"; 
    public static final String ID_FIELD = "_id"; 
    public static final String NAME_FIELD = "name"; 
    public static final String EMAIL_FIELD = "email"; 
    public static final String PHONE_FIELD = "phone"; 
    public static final String DESIGNATION_FIELD = "designation"; 

    public PrepopulatedDBOpenHelper(Context context) { 
     super(context, DB_NAME, null, 1); 
     this.context = context; 

     // database path /data/data/pkg-name/databases/ 
     String packageName = context.getPackageName(); 
     DB_PATH = "/data/data/" + packageName + "/databases/"; 
     this.database = openDatabase(); 

    } 

    public SQLiteDatabase getDatabase() { 
     return this.database; 
    } 

    public SQLiteDatabase openDatabase() { 
     String path = DB_PATH + DB_NAME; 
     if (database == null) { 
      createDatabase(); 
      database = SQLiteDatabase.openDatabase(path, null, 
        SQLiteDatabase.OPEN_READWRITE); 
     } 
     return database; 
    } 

    private void createDatabase() { 
     boolean dbExists = checkDB(); 
     if (!dbExists) { 
      this.getReadableDatabase(); 
      Log.e(getClass().getName(), 
        "Database doesn't exist. Copying database from assets..."); 
      copyDatabase(); 
     } else { 
      Log.e(getClass().getName(), "Database already exists"); 
     } 
    } 

    private void copyDatabase() { 
     try { 
      InputStream dbInputStream = context.getAssets().open(DB_NAME); 
      String path = DB_PATH + DB_NAME; 
      OutputStream dbOutputStream = new FileOutputStream(path); 
      byte[] buffer = new byte[4096]; 
      int readCount = 0; 
      while ((readCount = dbInputStream.read(buffer)) > 0) { 
       dbOutputStream.write(buffer, 0, readCount); 
      } 

      dbInputStream.close(); 
      dbOutputStream.close(); 

     } catch (IOException e) { 
      e.printStackTrace(); 
     } 

    } 

    private boolean checkDB() { 
     String path = DB_PATH + DB_NAME; 
     File file = new File(path); 
     if (file.exists()) { 
      Log.e(getClass().getName(), "Database already exists"); 
      return true; 
     } 
     Log.e(getClass().getName(), "Database does not exists"); 
     return false; 
    } 

    public synchronized void close() { 
     if (this.database != null) { 
      this.database.close(); 
     } 
    } 
    } 
相關問題