先簡單說下步驟:
1.將格式為.db的數(shù)據(jù)庫文件放到android項目assets目錄中跛璧;
2.在程序必要的時候,將其“拷貝”(文件讀饶环)到Android 程序默認的數(shù)據(jù)庫存儲目錄中,一般路徑為“/data/data/項目包名/databases/“;
3.自定義SQLiteOpenHelper類各薇,創(chuàng)建一個名字跟步驟1中.db名稱一樣的數(shù)據(jù)庫;
4.按照平常邏輯君躺,增刪改查數(shù)據(jù)庫峭判。
步驟1:如下圖,將數(shù)據(jù)庫文件copy到assets目錄中
步驟2:將db文件讀取拷貝到databases目錄中棕叫,代碼如下:
package com.kinth.youdian.activity.boti.util;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import android.annotation.SuppressLint;
import android.content.Context;
/**
* 將assets中的db文件拷貝到databases中
* @author Botision.Huang
* @Date: 2015-8-18 下午4:11:24
* @Descp: TODO
*/
public class DatabaseUtil {
@SuppressLint("SdCardPath")
public static void packDataBase(Context context){
// com.kinth.youdian 是程序的包名林螃,請根據(jù)自己的程序調(diào)整
// /data/data/com.kinth.youdian/databases目錄是準備放 SQLite 數(shù)據(jù)庫的地方,也是 Android 程序默認的數(shù)據(jù)庫存儲目錄
// 數(shù)據(jù)庫名為 db_youdian.db
String DB_PATH = "/data/data/com.kinth.youdian/databases/";
String DB_NAME = "db_youdian.db";
// 檢查 SQLite 數(shù)據(jù)庫文件是否存在
if (!(new File(DB_PATH + DB_NAME)).exists()) {
// 如 SQLite 數(shù)據(jù)庫文件不存在俺泣,再檢查一下 database 目錄是否存在
File f = new File(DB_PATH);
// 如 database 目錄不存在疗认,新建該目錄
if (!f.exists()) {
f.mkdir();
}
try {
// 得到 assets 目錄下我們實現(xiàn)準備好的 SQLite 數(shù)據(jù)庫作為輸入流
InputStream is = context.getAssets().open(DB_NAME);
// 輸出流,在指定路徑下生成db文件
OutputStream os = new FileOutputStream(DB_PATH + DB_NAME);
// 文件寫入
byte[] buffer = new byte[1024];
int length;
while ((length = is.read(buffer)) > 0) {
os.write(buffer, 0, length);
}
// 關(guān)閉文件流
os.flush();
os.close();
is.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
步驟3:代碼如下
package com.kinth.youdian.activity.boti.dbdao;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
/**
* 【省市區(qū)】數(shù)據(jù)庫幫助類
* @author Botision.Huang
* @Date: 2015-8-18 下午4:21:33
* @Descp: TODO
*/
public class ProvinceDataHelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "db_youdian.db";
private static final int DATABASE_VERSION = 1;
/** Create a helper object for the Events database */
public ProvinceDataHelper(Context ctx) {
super(ctx, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {}
}
步驟4:做完以上3個步驟之后,下面我們就可以按照正常邏輯伏钠,去操作數(shù)據(jù)庫啦横漏,譬如下代碼:
package com.kinth.youdian.activity.boti.dbdao;
import java.util.ArrayList;
import java.util.List;
import com.kinth.youdian.activity.boti.dbdao.bean.ProvinceBean;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
public class ProvinceDao {
private ProvinceDataHelper helper;
private SQLiteDatabase db = null;
public ProvinceDao(Context mContext){
helper = new ProvinceDataHelper(mContext);
}
/**
* 從本地數(shù)據(jù)庫獲取省份列表
* @return
*/
public List<ProvinceBean> getProvinceList(){
List<ProvinceBean> proList = new ArrayList<ProvinceBean>();
try{
db = helper.getReadableDatabase();
Cursor cursor = db.rawQuery("select * from province", null);
if(null != cursor){
while(cursor.moveToNext()){
int id = cursor.getInt(cursor.getColumnIndex("id"));
String name = cursor.getString(cursor.getColumnIndex("name"));
ProvinceBean bean = new ProvinceBean();
bean.setId(id);
bean.setName(name);
proList.add(bean);
}
}
cursor.close();
}catch(Exception e){
e.printStackTrace();
}finally{
if(null != db){
db.close();
}
}
return proList;
}
}