2020-11-17

android

读取assets中的数据库db文件

public class DBManager {
    private Context mContext;
    private SQLiteDatabase sqdb;
    private static String dbPath = FileUtil.getDBpath() + "xh.db";
    private static DBManager instance = null;

    

    public static DBManager getInstance() {
        if (instance == null) {
            instance = new DBManager();
        }
        return instance;
    }

    /**
     * 打开数据库
     */
    private void openDB() {
        if (isSDCard()) {
            if (sqdb == null || !sqdb.isOpen())
                sqdb = SQLiteDatabase.openDatabase(dbPath, null,
                        SQLiteDatabase.OPEN_READWRITE);
        }
    }
    private boolean isSDCard() {
        return Environment.getExternalStorageState().equals(
                Environment.MEDIA_MOUNTED);
    }

    //查询
    public List<CompanyBean> querybean() {
        List<CompanyBean> beanList = new ArrayList<>();
        CompanyBean bean= null;
        openDB();
        try {
            String sql = " select a.id,a.title,a.icon,a.address,a.desc,a.name,a.shop,a.phone  from company a";
            Cursor cursor = sqdb.rawQuery(sql, null);
            while (cursor.moveToNext()) {
                String ind = cursor.getString(cursor.getColumnIndex("id"));
                String title = cursor.getString(cursor.getColumnIndex("title"));
                String icon = cursor.getString(cursor.getColumnIndex("icon"));
                String address = cursor.getString(cursor.getColumnIndex("address"));
                String desc = cursor.getString(cursor.getColumnIndex("desc"));
                String phone = cursor.getString(cursor.getColumnIndex("phone"));
                String name = cursor.getString(cursor.getColumnIndex("name"));
                String shop = cursor.getString(cursor.getColumnIndex("shop"));
                bean= new CompanyBean();
                bean.setInd(ind);
                bean.setTitle(title);
                bean.setIcon(icon);
                bean.setAddress(address);
                bean.setDesc(desc);
                bean.setName(name);
                bean.setPhone(phone);
                bean.setShop(shop);
                beanList.add(bean);
            }
            cursor.close();
            return beanList;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

}
public class FileUtil {

    private static String mWorkPath = null;
    private static String mRootPath = null;
    private static Boolean mGetSDpath = false;
    private final static String DB_PATH_NAME = "biubiu";
    public static long copyTime = 0;

    private static Context mContext;

    public static String getRootPath() {
        if (!mGetSDpath || mRootPath == null) {
            mGetSDpath = true;
            boolean sdCardExist = Environment.getExternalStorageState().equals(
                    Environment.MEDIA_MOUNTED); // 判断sd卡是否存在
            if (sdCardExist) {
                File sdDir = Environment.getExternalStorageDirectory();// 获取跟目录
                mRootPath = sdDir.toString();
            } else {
                mRootPath = mContext.getFilesDir().toString();
            }
        }
        if (!mRootPath.endsWith("/"))
            mRootPath += "/";
        return mRootPath;
    }
    
    public static void setWorkPath(Context context, String path) {
        mContext = context;
        if (null != getRootPath()) {
            mWorkPath = mRootPath + path;
        }
        if (!mWorkPath.endsWith("/")) {
            mWorkPath += "/";
        }

        File file = new File(mWorkPath);
        if (!file.exists()){
            boolean b = file.mkdirs();
        }
    }

    public static String getDBpath() {
        File file = new File(mWorkPath + DB_PATH_NAME);
        if (!file.exists())
            file.mkdirs();
        return mWorkPath + DB_PATH_NAME;
    }

    public static void copyAccessDB(Context context) {
        try {
            String[] dbNames = context.getAssets().list("db");
            for (String dbName : dbNames) {
                long startTime = System.currentTimeMillis();
                String filePath = FileUtil.getDBpath() + dbName;
                File dbFile = new File(filePath);
                if (!dbFile.exists()) {
                    FileOutputStream fos = null;
                    try {
                        dbFile.createNewFile();
                    }catch (Exception e){
                    }
                    InputStream is = context.getAssets().open("db/" + dbName);
                    fos = new FileOutputStream(dbFile);

                    byte[] buffer = new byte[2048];
                    int len = -1;
                    while ((len = is.read(buffer)) != -1) {
                        fos.write(buffer, 0, len);
                    }
                    fos.close();
                    long endTime = System.currentTimeMillis();
                    long useTime = endTime - startTime;
                    copyTime += useTime;
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
FileUtil.setWorkPath(this, "人懒的好处,哇哈哈哈!");
FileUtil.copyAccessDB(this);
beanList = DBManager.getInstance().querybean();
textview.setText(beanList.get(0).getAddress());

demo:不甜不要钱

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值