国产福利在线播放|久久精品福利网站免费|国产呻吟视频在线观看|日韩一区二区三区免费高清|久996视频精品免费观看|欧美日本在线一区二区三区|在线最新无码经典无码免費資訊|国产午夜亚洲精品国产成人最大

始創(chuàng)于2000年 股票代碼:831685
咨詢熱線:0371-60135900 注冊有禮 登錄
  • 掛牌上市企業(yè)
  • 60秒人工響應(yīng)
  • 99.99%連通率
  • 7*24h人工
  • 故障100倍補(bǔ)償
全部產(chǎn)品
您的位置: 網(wǎng)站首頁 > 幫助中心>文章內(nèi)容

android導(dǎo)入外部已存在的數(shù)據(jù)庫大于1M的數(shù)據(jù)庫文件方法

發(fā)布時間:  2012/7/28 17:19:18
 
1.如果數(shù)據(jù)庫文件大于1M,就用Filesplit工具切割。先去下載這個軟件工具
2.首先把已有的數(shù)據(jù)庫放到assets文件夾下面,如果沒有這個文件就先在android項(xiàng)目中建立這個文件夾。
代碼如下:
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.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ù)庫文件直接復(fù)制到DB_PATH,但數(shù)據(jù)庫文件大小限制在1M以下
* 如果有超過1M的大文件,則需要先分割為N個小文件,然后使用copyBigDatabase()替換copyDatabase()
*/
public class DBManager extends SQLiteOpenHelper {
//用戶數(shù)據(jù)庫文件的版本
private static final int DB_VERSION = 1;
//數(shù)據(jù)庫文件目標(biāo)存放路徑為系統(tǒng)默認(rèn)位置,com.rys.lb 是你的包名
private static String DB_PATH = "/data/data/com.rys.lb/databases/";

//如果你想把數(shù)據(jù)庫文件存放在SD卡的話
// 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ù)庫文件較大,使用FileSplit分割為小于1M的小文件
* 此例中分割為 data.db.100 data.db.101 data.db.102....
*/
//第一個文件名后綴
private static final int ASSETS_SUFFIX_BEGIN = 100;
//最后一個文件名后綴
private static final int ASSETS_SUFFIX_END = 110;

/**
* 在SQLiteOpenHelper的子類當(dāng)中,必須有該構(gòu)造函數(shù)
* @param context 上下文對象
* @param name 數(shù)據(jù)庫名稱
* @param factory 一般都是null
* @param version 當(dāng)前數(shù)據(jù)庫的版本,值必須是整數(shù)并且是遞增的狀態(tài)
*/
public DBManager(Context context, String name, CursorFactory factory, int version) {
//必須通過super調(diào)用父類當(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ù)庫已存在,do nothing.

System.out.println("數(shù)據(jù)庫已經(jīng)存在");

}else{
//創(chuàng)建數(shù)據(jù)庫
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ù)庫創(chuàng)建失敗");
}
}
}

//檢查數(shù)據(jù)庫是否有效
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ù)庫已經(jīng)...");
myDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
System.out.println("數(shù)據(jù)庫打開");
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ù)庫文件時用這個
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ù)庫已經(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)建的時候執(zhí)行,
* 實(shí)際上是第一次得到SQLiteDatabase對象的時候才會調(diào)用這個方法
*/
@Override
public void onCreate(SQLiteDatabase db) {
}

/**
* 數(shù)據(jù)庫表結(jié)構(gòu)有變化時采用
*/
@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)限
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"></uses-permission>

本文出自:億恩科技【www.riomediacenter.com】

服務(wù)器租用/服務(wù)器托管中國五強(qiáng)!虛擬主機(jī)域名注冊頂級提供商!15年品質(zhì)保障!--億恩科技[ENKJ.COM]

  • 您可能在找
  • 億恩北京公司:
  • 經(jīng)營性ICP/ISP證:京B2-20150015
  • 億恩鄭州公司:
  • 經(jīng)營性ICP/ISP/IDC證:豫B1.B2-20060070
  • 億恩南昌公司:
  • 經(jīng)營性ICP/ISP證:贛B2-20080012
  • 服務(wù)器/云主機(jī) 24小時售后服務(wù)電話:0371-60135900
  • 虛擬主機(jī)/智能建站 24小時售后服務(wù)電話:0371-60135900
  • 專注服務(wù)器托管17年
    掃掃關(guān)注-微信公眾號
    0371-60135900
    Copyright© 1999-2019 ENKJ All Rights Reserved 億恩科技 版權(quán)所有  地址:鄭州市高新區(qū)翠竹街1號總部企業(yè)基地億恩大廈  法律顧問:河南亞太人律師事務(wù)所郝建鋒、杜慧月律師   京公網(wǎng)安備41019702002023號
      0
     
     
     
     

    0371-60135900
    7*24小時客服服務(wù)熱線