1.如果數(shù)據(jù)庫(kù)文件大于1M,就用Filesplit工具切割。先去下載這個(gè)軟件工具 2.首先把已有的數(shù)據(jù)庫(kù)放到assets文件夾下面,如果沒(méi)有這個(gè)文件就先在Android項(xiàng)目中建立這個(gè)文件夾。 代碼如下: import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; 
創(chuàng)新互聯(lián)自2013年創(chuàng)立以來(lái),是專(zhuān)業(yè)互聯(lián)網(wǎng)技術(shù)服務(wù)公司,擁有項(xiàng)目成都網(wǎng)站制作、網(wǎng)站設(shè)計(jì)、外貿(mào)網(wǎng)站建設(shè)網(wǎng)站策劃,項(xiàng)目實(shí)施與項(xiàng)目整合能力。我們以讓每一個(gè)夢(mèng)想脫穎而出為使命,1280元米脂做網(wǎng)站,已為上家服務(wù),為米脂各地企業(yè)和個(gè)人服務(wù),聯(lián)系電話(huà):13518219792 import android.content.Context; import android.database.Cursor; import android.database.SQLException; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteDatabase.CursorFactory; import android.database.sqlite.SQLiteException; import android.database.sqlite.SQLiteOpenHelper; * 將把a(bǔ)ssets下的數(shù)據(jù)庫(kù)文件直接復(fù)制到DB_PATH,但數(shù)據(jù)庫(kù)文件大小限制在1M以下 * 如果有超過(guò)1M的大文件,則需要先分割為N個(gè)小文件,然后使用copyBigDatabase()替換copyDatabase() */ public class DBManager extends SQLiteOpenHelper { //用戶(hù)數(shù)據(jù)庫(kù)文件的版本 private static final int DB_VERSION = 1; //數(shù)據(jù)庫(kù)文件目標(biāo)存放路徑為系統(tǒng)默認(rèn)位置,com.rys.lb 是你的包名 private static String DB_PATH = "/data/data/com.rys.lb/databases/"; //如果你想把數(shù)據(jù)庫(kù)文件存放在SD卡的話(huà) // private static String DB_PATH = android.os.Environment.getExternalStorageDirectory().getAbsolutePath() // + "/arthurcn/drivertest/packfiles/"; private static String DB_NAME = "data.db"; private static String ASSETS_NAME = "data.db"; private SQLiteDatabase myDataBase; private final Context myContext; /** * 如果數(shù)據(jù)庫(kù)文件較大,使用FileSplit分割為小于1M的小文件 * 此例中分割為 data.db.100 data.db.101 data.db.102.... */ //第一個(gè)文件名后綴 private static final int ASSETS_SUFFIX_BEGIN = 100; //最后一個(gè)文件名后綴 private static final int ASSETS_SUFFIX_END = 110; /** * 在SQLiteOpenHelper的子類(lèi)當(dāng)中,必須有該構(gòu)造函數(shù) * @param context 上下文對(duì)象 * @param name 數(shù)據(jù)庫(kù)名稱(chēng) * @param factory 一般都是null * @param version 當(dāng)前數(shù)據(jù)庫(kù)的版本,值必須是整數(shù)并且是遞增的狀態(tài) */ public DBManager(Context context, String name, CursorFactory factory, int version) { //必須通過(guò)super調(diào)用父類(lèi)當(dāng)中的構(gòu)造函數(shù) super(context, name, null, version); this.myContext = context; } public DBManager(Context context, String name, int version){ this(context,name,null,version); } public DBManager(Context context, String name){ this(context,name,DB_VERSION); } public DBManager (Context context) { this(context, DB_PATH + DB_NAME); } public void createDataBase() throws IOException{ boolean dbExist = checkDataBase(); if(dbExist){ //數(shù)據(jù)庫(kù)已存在,do nothing. System.out.println("數(shù)據(jù)庫(kù)已經(jīng)存在"); }else{ //創(chuàng)建數(shù)據(jù)庫(kù) 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); // 復(fù)制asseets中的db文件到DB_PATH下 //copyDataBase(); copyBigDataBase(); } catch (IOException e) { throw new Error("數(shù)據(jù)庫(kù)創(chuàng)建失敗"); } } } //檢查數(shù)據(jù)庫(kù)是否有效 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(); System.out.println("關(guān)閉"); } return checkDB != null ? true : false; } public DBManager open1(){ String myPath = DB_PATH + DB_NAME; System.out.println("數(shù)據(jù)庫(kù)已經(jīng)..."); myDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY); System.out.println("數(shù)據(jù)庫(kù)打開(kāi)"); return this; } /** * 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(); } //復(fù)制assets下的大數(shù)據(jù)庫(kù)文件時(shí)用這個(gè) 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(); System.out.println("數(shù)據(jù)庫(kù)已經(jīng)復(fù)制"); } @Override public synchronized void close() { if(myDataBase != null){ myDataBase.close(); System.out.println("關(guān)閉成功1"); } super.close(); System.out.println("關(guān)閉成功2"); } /** * 該函數(shù)是在第一次創(chuàng)建的時(shí)候執(zhí)行, * 實(shí)際上是第一次得到SQLiteDatabase對(duì)象的時(shí)候才會(huì)調(diào)用這個(gè)方法 */ @Override public void onCreate(SQLiteDatabase db) { } /** * 數(shù)據(jù)庫(kù)表結(jié)構(gòu)有變化時(shí)采用 */ @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { } public void open(){ SQLiteDatabase DataBase=this.openOrCreateDatabase("data.db", null); } private SQLiteDatabase openOrCreateDatabase(String string, Object object) { // TODO Auto-generated method stub return null; } 3.用SD卡要加權(quán)限 |