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:不甜不要钱