Android 读取assets的数据库文件

<span style="font-size:18px;">public class DBManger {


	public static final String DB_NAME = "festival.db";//数据库
	public static final String PACKAGE_NAME = "com.example.viewpager_homework2";
	// 在手机存放数据库的位置
	public final String DB_PATH = "/data"
			+ Environment.getDataDirectory().getAbsolutePath() + "/"
			+ PACKAGE_NAME + "/" + DB_NAME; // 在手机里存放数据库的位置


	private SQLiteDatabase database;
	private Context context;


	public DBManger(Context context) {
		super();
		this.context = context;
	}


	public void openDatabase() {


		this.database = this.openDatabase(DB_PATH);


	}
	
	public Cursor infor(String string){
		
		SQLiteDatabase db = this.openDatabase(DB_PATH);
		
		return db.rawQuery("select * from smstb where fes_id = "+string, null);
		
	}
	


	private SQLiteDatabase openDatabase(String dbfile) {
		try {
			if (!(new File(dbfile).exists())) {// 判断数据库文件是否存在,若不存在则执行导入,否则直接打开数据库


				Log.i("info", "qq");
				InputStream is = this.context.getResources().getAssets()
						.open("festival.db"); // 欲导入的数据库
				// InputStream is = this.context.getResources().openRawResource(
				// R.raw.);
				Log.i("info", "qq1");
				FileOutputStream fos = new FileOutputStream(dbfile);
				byte[] buffer = new byte[1024];
				int count = 0;
				Log.i("info", "qq2");
				while ((count = is.read(buffer)) != -1) {
					fos.write(buffer, 0, count);
					Log.i("info", buffer.length + "");
					fos.flush();
				}
				fos.close();
				is.close();
			}


			SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(dbfile,
					null);


			return db;
		} catch (FileNotFoundException e) {
			Log.e("Database", "File not found");
			e.printStackTrace();
		} catch (IOException e) {
			Log.e("Database", "IO exception");
			e.printStackTrace();
		}
		return null;
	}


	public void closeDatabase() {
		this.database.close();
	}


}</span>


检查数据库文件有没有写入,可以点击DDMS按钮, 在data文件夹下点击data文件夹找到对应的报名。



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值