2011-08-01 13 views
2

私はデータベースのコピーを作成しました。私のデータベースを資産forder.Myのデータベースサイズは4.5 MBです。 データベースをチャンクする方法。私はdatabase.Pleaseは、事前にこのAndroidデータベース1MB以上

おかげで私を助けて分割することができます私はcode.Howを変更する必要がcopyDataBase()メソッドで

package com.xont.db; 

    public class DataBaseMasterHelper extends SQLiteOpenHelper { 

// The Android's default system path of your application database. 
private static String DB_PATH = "/data/data/com.xont.controller/databases/"; 
private static String DB_NAME = "masterventura.db"; 
private SQLiteDatabase ventura; 
private Context myContext; 
private DataBaseHelper myDbHelper; 

public DataBaseMasterHelper(Context context) { 
    super(context, DB_NAME, null, 1); 
    this.myContext = context; 
} 

/** 
* Creates a empty database on the system and rewrites it with your own 
* database. 
**/ 
public void createDataBase() throws IOException { 
    boolean dbExist = checkDataBase(); 
    if (dbExist) { 
    } else { 
     this.getReadableDatabase(); 
     //this.getReadableDatabase().getPath(); 
     System.out.println(" ===== " +  this.getReadableDatabase().getPath()); 
     try { 
      copyDataBase(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 

} 

public boolean checkDataBase() { 
    SQLiteDatabase checkDB = null; 
    try { 
     String myPath = DB_PATH + DB_NAME; 
     checkDB = SQLiteDatabase.openDatabase(myPath, null,SQLiteDatabase.OPEN_READONLY); 
    } catch (SQLiteException e) { 
     e.printStackTrace(); 
    } 

    if (checkDB != null) { 
     checkDB.close(); 
    } 

    return checkDB != null ? true : false; 
} 

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(); 


} 

public void openDataBase() throws SQLException { 
    String myPath = DB_PATH + DB_NAME; 
    ventura = SQLiteDatabase.openDatabase(myPath, null,SQLiteDatabase.OPEN_READONLY); 

} 

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

@Override 
public void onCreate(SQLiteDatabase db) { 
} 

@Override 
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
} 

}

...

+1

私が見る唯一の方法は、 'copyDataBase()'メソッドでデータベースを開き、SQL文でデータを操作することです。その後、それを閉じてsdcardにコピーすることができます。無駄なゴミを投げ捨てることで、データベースのサイズを詰めることができます(開いているデータベースとSQL文との反復)。 –

答えて