android ormlite简单使用和读取sdcard下db数据库

废话不多说:

1、从http://ormlite.com/releases/点击打开链接版本库下载最新的core和android的jar包,放入你的android工程中

2、使用以下方法得到DAO对象

/**
	 * DAO对象的使用参见OrmLite的API
	 */
	public Dao<T, String> createBeaconTable(String buildId,
			String floor) {
		try {

			// 使用SQLiteDatabase创建或者打开sd或者assets下的db文件
			DTFileUtils.createDirs(DTFileUtils.getDataDir() + buildId
					+ File.separator);
			SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(
					DTFileUtils.getDataDir() + buildId + File.separator
							+ buildId + "_" + floor + ".db", null);
			// ORMLite的android.jar封装SQLiteDatabase
			AndroidConnectionSource connectionSource = new AndroidConnectionSource(
					db);

			/**
			 * 以下为官方首页快速使用方式,没有任何改动的照搬,由于需要ConnectionSource对象,
			 * 通过查看API发现子类AndroidConnectionSource
			 */

			// instantiate the DAO to handle Account with String id
			Dao<T, String> accountDao = DaoManager.createDao(
					connectionSource, T.class);

			// if you need to create the 'accounts' table make this call
			TableUtils.createTableIfNotExists(connectionSource,
					T.class);

			// create an instance of Account
			// connectionSource.close();
			return accountDao;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}

猜你喜欢

转载自blog.csdn.net/vcstrong/article/details/53585421