初學android,達人忽略,歡迎扔石頭.java
android中使用sqlite、複製assets下的數據庫到SD卡、支持大於1M的文件android
若是使用SD卡,須要在AndroidManifest.xml中設置權限web
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission> <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"></uses-permission>
package cn.arthur.common; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteDatabase.CursorFactory; import android.database.sqlite.SQLiteException; import android.database.sqlite.SQLiteOpenHelper; /** * @author Joshua * 用法: * DBHelper dbHelper = new DBHelper(this); * dbHelper.createDataBase(); * SQLiteDatabase db = dbHelper.getWritableDatabase(); * Cursor cursor = db.query() * db.execSQL(sqlString); * 注意:execSQL不支持帶;的多條SQL語句,只能一條一條的執行,暈了好久才明白 * 見execSQL的源碼註釋 (Multiple statements separated by ;s are not supported.) * 將把assets下的數據庫文件直接複製到DB_PATH,但數據庫文件大小限制在1M如下 * 若是有超過1M的大文件,則須要先分割爲N個小文件,而後使用copyBigDatabase()替換copyDatabase() */ public class DBHelper extends SQLiteOpenHelper { //用戶數據庫文件的版本 private static final int DB_VERSION = 1; //數據庫文件目標存放路徑爲系統默認位置,cn.arthur.examples 是你的包名 private static String DB_PATH = "/data/data/cn.arthur.examples/databases/"; /* //若是你想把數據庫文件存放在SD卡的話 private static String DB_PATH = android.os.Environment.getExternalStorageDirectory().getAbsolutePath() + "/arthurcn/drivertest/packfiles/"; */ private static String DB_NAME = "hello.db"; private static String ASSETS_NAME = "hello.db"; private SQLiteDatabase myDataBase = null; private final Context myContext; /** * 若是數據庫文件較大,使用FileSplit分割爲小於1M的小文件 * 此例中分割爲 hello.db.101 hello.db.102 hello.db.103 */ //第一個文件名後綴 private static final int ASSETS_SUFFIX_BEGIN = 101; //最後一個文件名後綴 private static final int ASSETS_SUFFIX_END = 103; /** * 在SQLiteOpenHelper的子類當中,必須有該構造函數 * @param context 上下文對象 * @param name 數據庫名稱 * @param factory 通常都是null * @param version 當前數據庫的版本,值必須是整數而且是遞增的狀態 */ public DBHelper(Context context, String name, CursorFactory factory, int version) { //必須經過super調用父類當中的構造函數 super(context, name, null, version); this.myContext = context; } public DBHelper(Context context, String name, int version){ this(context,name,null,version); } public DBHelper(Context context, String name){ this(context,name,DB_VERSION); } public DBHelper (Context context) { this(context, DB_PATH + DB_NAME); } public void createDataBase() throws IOException{ boolean dbExist = checkDataBase(); if(dbExist){ //數據庫已存在,do nothing. }else{ //建立數據庫 try { File dir = new File(DB_PATH); if(!dir.exists()){ dir.mkdirs(); } File dbf = new File(DB_PATH + DB_NAME); if(dbf.exists()){ dbf.delete(); } SQLiteDatabase.openOrCreateDatabase(dbf, null); // 複製asseets中的db文件到DB_PATH下 copyDataBase(); } catch (IOException e) { throw new Error("數據庫建立失敗"); } } } //檢查數據庫是否有效 private boolean checkDataBase(){ SQLiteDatabase checkDB = null; String myPath = DB_PATH + DB_NAME; try{ checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY); }catch(SQLiteException e){ //database does't exist yet. } if(checkDB != null){ checkDB.close(); } return checkDB != null ? true : false; } /** * Copies your database from your local assets-folder to the just created empty database in the * system folder, from where it can be accessed and handled. * This is done by transfering bytestream. * */ private void copyDataBase() throws IOException{ //Open your local db as the input stream InputStream myInput = myContext.getAssets().open(ASSETS_NAME); // Path to the just created empty db String outFileName = DB_PATH + DB_NAME; //Open the empty db as the output stream OutputStream myOutput = new FileOutputStream(outFileName); //transfer bytes from the inputfile to the outputfile byte[] buffer = new byte[1024]; int length; while ((length = myInput.read(buffer))>0){ myOutput.write(buffer, 0, length); } //Close the streams myOutput.flush(); myOutput.close(); myInput.close(); } //複製assets下的大數據庫文件時用這個 private void copyBigDataBase() throws IOException{ InputStream myInput; String outFileName = DB_PATH + DB_NAME; OutputStream myOutput = new FileOutputStream(outFileName); for (int i = ASSETS_SUFFIX_BEGIN; i < ASSETS_SUFFIX_END+1; i++) { myInput = myContext.getAssets().open(ASSETS_NAME + "." + i); byte[] buffer = new byte[1024]; int length; while ((length = myInput.read(buffer))>0){ myOutput.write(buffer, 0, length); } myOutput.flush(); myInput.close(); } myOutput.close(); } @Override public synchronized void close() { if(myDataBase != null){ myDataBase.close(); } super.close(); } /** * 該函數是在第一次建立的時候執行, * 其實是第一次獲得SQLiteDatabase對象的時候纔會調用這個方法 */ @Override public void onCreate(SQLiteDatabase db) { } /** * 數據庫表結構有變化時採用 */ @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { } }
how copy database from assets to ExternalStorage?sql
public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); File f1=new File(Environment.getExternalStorageDirectory(),"aaa"); f1.mkdir(); InputStream in = null; try { Toast.makeText(getBaseContext(), "error", Toast.LENGTH_LONG).show(); in = getBaseContext().getAssets().open("home"); } catch (IOException e) { // TODO Auto-generated catch block Toast.makeText(getBaseContext(), "error", Toast.LENGTH_LONG).show(); e.printStackTrace(); } OutputStream ou = null; try { ou = new FileOutputStream(Environment.getExternalStorageDirectory()+"aaa"+ "/"+"home" ); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } try { CopyDB(in, ou); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public void CopyDB(InputStream in, OutputStream ou) throws IOException { byte []buffer=new byte[1024]; int length; while((length=in.read(buffer))>0) { try { ou.write(buffer, 0, length); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } in.close(); ou.close(); }
https://stackoverflow.com/questions/26870624/how-copy-database-from-assets-to-externalstorage數據庫