android ormlite的简单使用
1:了解什么是ormlite
ormlite 官网:http://ormlite.com/
首先要知道的是ormlite 是java web的数据库工具,其次才兼容android 的sqlite3。那么肯定的是,我们是因为android才使用ormlite。
要注重pdf文档的阅读 http://ormlite.com/docs/ormlite.pdf 在这个文档中,应该阅读的是关于android的部分。
2:使用在android 中使用 ormlite
(1)eclipse中在maven中下载jar 下载链接 ormlite-android 和ormlite-core 的jar文件
(2)android studio 中
build.gradle 中添加
dependencies {compile 'com.j256.ormlite:ormlite-android:4.48'compile 'com.j256.ormlite:ormlite-core:4.48'}
3:在代码中如何使用ormlite
逻辑结构
a:写一个Modle类
b:写一个类继承OrmLiteSqliteOpenHelper
c:在Activity中写逻辑(逻辑可另外写个类,Activity调用,类似MVP模式)
4:Modle 类
@DatabaseTable(tableName = "test")public class Test {Test() {}//必须有//注意的是,声明的类型顺序会影响在数据库中的列续@DatabaseField(generatedId = true)private long id;@DatabaseFieldprivate long time;public long getId() {return id;}public void setId(long id) {this.id = id;}public long getTime() {return time;}public void setTime(long time) {this.time = time;}}
5:写一个DataBaseHelper
这个用于创建数据库和当数据库表有出现修改的时候,可在这里配置当数据表修改和升级后的表格。
public class DataBaseHelper_ormlite extends OrmLiteSqliteOpenHelper {private static final String DATABASE_NAME = "ormlite.db";//数据库名字private static final int DATABASE_VERSION = 1;//版本private Dao<TheCacheInfo,Integer> theCacheInfos;private Dao<Test,Integer> integerDao;public DataBaseHelper_ormlite(Context context){super(context,DATABASE_NAME,null,DATABASE_VERSION);}@Overridepublic void onCreate(SQLiteDatabase database, ConnectionSource connectionSource) {try {Log.i(DatabaseHelper.class.getName(), "onCreate");TableUtils.createTable(connectionSource, Test.class);//在这里可以添加数据进入到数据库,当数据库刚刚创建的时候// dao.create(simple);} catch (SQLException e) {Log.e(DatabaseHelper.class.getName(), "Can't create database", e);throw new RuntimeException(e);}}@Overridepublic void onUpgrade(SQLiteDatabase database, ConnectionSource connectionSource, int oldVersion, int newVersion) {try {Log.i(DatabaseHelper.class.getName(), "onUpgrade");TableUtils.dropTable(connectionSource, Test.class, true);// after we drop the old databases, we create the new ones// 会终止旧的数据库,会创建一个新的数据库onCreate(database, connectionSource);} catch (SQLException e) {Log.e(DatabaseHelper.class.getName(), "Can't drop databases", e);throw new RuntimeException(e);}}//获取数据库Dao操作public Dao<Test,Integer> getintegerDao() throws SQLException {if (integerDao==null){integerDao = getDao(Test.class);}return integerDao;}@Overridepublic void close() {//关闭Daosuper.close();integerDao = null;}}
在写完DataBaseHelper之后,我们需要在Activity中使用这个DataBaseHelper的DAO
6:Activity 中的调用
public class TestUI extends BaseActivity {private Dao<Test, Integer> simpleDao = null;private static String TAG = "TestUI";@Overrideprotected void onCreate(Bundle arg0) {super.onCreate(arg0);setContentView(R.layout.testui_layout);init();}private void init() {try {simpleDao = getHelper().getintegerDao();} catch (SQLException e) {e.printStackTrace();}}public void ADD(View view) {if (simpleDao == null) {init();}//判断是否为空add(System.currentTimeMillis());//添加数据}private void add(long time) {try {Test test = new Test();test.setTime(time);simpleDao.create(test);Log.w(TAG, "数据添加成功 :" + time);} catch (SQLException e) {e.printStackTrace();}}public void Query(View view) {//对添加的数据查询Log.w(TAG,"Query");// try {// List<Test> list = simpleDao.queryForAll();// for (Test test:list){// Log.w(TAG,"id:"+test.getId()+" 秒:"+test.getTime());// }//// } catch (SQLException e) {// e.printStackTrace();// }query(5, offest0);}private long offest0 = 0;//偏移量private void query(long number, long offest) {// Log.w("数据:","query开始");try {GenericRawResults<String[]> rawResults =simpleDao.queryRaw("select * from test order by time DESC limit " + number + " offset " + offest);//测试可用List<String[]> list = rawResults.getResults();if (list.size()<number){offest0= offest0+list.size();}else {offest0 = offest0+number;}for (String[] strings : list) {for (String item :strings){Log.w("数据:",""+item);}}//判断String 的长度和内容} catch (SQLException e) {e.printStackTrace();}}public void DeleteAll(View view) throws SQLException {simpleDao.delete(simpleDao.queryForAll());}private DataBaseHelper_ormlite databaseHelper = null;private DataBaseHelper_ormlite getHelper() {//通过反射机制,获取databaseHelper 得到Daoif (databaseHelper == null) {databaseHelper = OpenHelperManager.getHelper(this, DataBaseHelper_ormlite.class);}return databaseHelper;}@Overrideprotected void onDestroy() {//每次Destroy后,销毁资源super.onDestroy();databaseHelper.close();if (databaseHelper != null) {OpenHelperManager.releaseHelper();databaseHelper = null;}}}
7:布局文件
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical" android:layout_width="match_parent"android:layout_height="match_parent"><Buttonandroid:layout_width="match_parent"android:layout_height="wrap_content"android:onClick="ADD"android:text="添加数据"/><Buttonandroid:layout_width="match_parent"android:layout_height="wrap_content"android:onClick="Query"android:text="查询数据"/><Buttonandroid:onClick="DeleteAll"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="删除全部"/></LinearLayout>

查询的时候存在偏移量,是为了更好的体验。
仅仅是简单的使用~~哦

浙公网安备 33010602011771号