package com.jyt.ctbj.view.manager;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.AssetManager;
import android.database.sqlite.SQLiteDatabase;
@SuppressLint("SdCardPath")
public class DBManager {
Context context;
@SuppressLint("SdCardPath")
static String path = "/data/data/com.jyt.ctbj/databases";
static String url = "/data/data/com.jyt.ctbj/databases/province.db";
private static SQLiteDatabase db;
public DBManager(Context context) {
super();
this.context = context;
}
public void openDatabase() {
File file = new File(path);
if (!file.exists()) {
file.mkdirs();
}
File f = new File(url);
if (!f.exists()) {
try {
f.createNewFile();
AssetManager assetManager = context.getAssets();
InputStream is = assetManager.open("province.db");
FileOutputStream fos = new FileOutputStream(url);
byte[] buffer = new byte[1024 * 50];
int count = 0;
while ((count = is.read(buffer)) != -1) {
fos.write(buffer, 0, count);
}
fos.close();
is.close();
db = SQLiteDatabase.openOrCreateDatabase(url, null);
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static SQLiteDatabase getDB() {
if (db == null) {
db = SQLiteDatabase.openOrCreateDatabase(url, null);
}
return db;
}
}
读取 assets 下 db文件的内容
最新推荐文章于 2024-07-25 19:04:18 发布