Android Study 将DB打包APK

LZ-Say:最近天有点凉了,小风嗖嗖的~各位注意身体~

还记得项目中有相关城市以及区县联动显示,之前老版本是将这些内容保存文本,之后读取,转化,显示。

挺麻烦的,所以打算直接弄成数据库类型,打包进去,读取速度也会有所提升。

一般的话,我们直接将DB文件放在res下的raw或者assets,放在这里,是因为在生成apk的时候不会被压缩~

东西很简单 直接放源码了


import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Environment;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * 数据库管理类 Created by HLQ on 2017/8/14
 */

public class DBManager {

    private static final int BUFFER_SIZE = 1024;

    private Context mContext;

    /**
     * 数据库名称
     */
    private static String DB_NAME = "hlq-test.db";

    /**
     * City名称
     */
    private String TABLE_CITY = "city";

    /**
     * Country名称
     */
    private String TABLE_COUNTRY = "country";

    /**
     * 数据库路径
     */
    private static String PATH_DB;

    public DBManager(Context context) {
        this.mContext = context;
        PATH_SMB_DB = File.separator + "data"
                + Environment.getDataDirectory().getAbsolutePath() + File.separator
                + context.getPackageName() + File.separator + "databases" + File.separator;
        copyDBFile();
    }

    /**
     * 拷贝数据库到data下
     */
    private void copyDBFile() {
        File dir = new File(PATH_DB);
        if (!dir.exists()) {
            dir.mkdirs();
        }
        File dbFile = new File(PATH_DB + DB_NAME);
        if (!dbFile.exists()) {
            InputStream is;
            OutputStream os;
            try {
                is = mContext.getResources().getAssets().open(DB_NAME);
                os = new FileOutputStream(dbFile);
                byte[] buffer = new byte[BUFFER_SIZE];
                int length;
                while ((length = is.read(buffer, 0, buffer.length)) > 0) {
                    os.write(buffer, 0, length);
                }
                os.flush();
                os.close();
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 获取热门城市
     *
     * @return
     */
    public static List<CityModel> getHotCityList() {
        List<CityModel> cityList = new ArrayList<>();
        SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(PATH_DB + DB_NAME, null);
        Cursor cursor = db.query("hot_city", null, null, null, null, null, null);
        while (cursor.moveToNext()) {
            String cityID = cursor.getString(cursor.getColumnIndex("hot_city_id"));
            String cityName = cursor.getString(cursor.getColumnIndex("hot_city_name"));
            String cityCode = cursor.getString(cursor.getColumnIndex("hot_city_code"));
            cityList.add(new CityModel(cityID, cityName, cityCode, 0));
        }
        cursor.close();
        return cityList;
    }

    /**
     * 获取城市
     *
     * @return
     */
    public static List<CityModel> getCityList() {
        List<CityModel> cityList = new ArrayList<>();
        SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(PATH_DB + DB_NAME, null);
        Cursor cursor = db.query("city", null, null, null, null, null, null);
        while (cursor.moveToNext()) {
            String cityID = cursor.getString(cursor.getColumnIndex("city_id"));
            String cityName = cursor.getString(cursor.getColumnIndex("city_short_name"));
            String cityCode = cursor.getString(cursor.getColumnIndex("city_code"));
            smbCityList.add(new CityModel(cityID, cityName, cityCode, 0));
        }
        cursor.close();
        return cityList;
    }

    /**
     * 获取区县
     *
     * @param cityId
     * @return
     */
    public static List<CityModel> getCountryList(String cityId) {
        List<CityModel> cityList = new ArrayList<>();
        SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(PATH_DB + DB_NAME, null);
        Cursor cursor = db.rawQuery("select * from country where city_id=?", new String[]{cityId});
        while (cursor.moveToNext()) {
            String countryID = cursor.getString(cursor.getColumnIndex("country_id"));
            String cityName = cursor.getString(cursor.getColumnIndex("country_name"));
            cityList.add(new CityModel(countryID, cityName));
        }
        cursor.close();
        return cityList;
    }

}

然后再MyApplication进行初始化即可~

主要方法:copyDBFile()

下面简单写了几种调用查询方法,可以参考~

给自己留点东西,方便以后用到直接拿走~

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

HLQ_Struggle

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值