​1:了解什么是ormlite

​ormlite 官网:http://ormlite.com/

​首先要知道的是ormlite 是java web的数据库工具,其次才兼容android 的sqlite3。那么肯定的是,我们是因为android才使用ormlite。

介绍 android 的网址

要注重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 中添加

      
  1. dependencies {
  2. compile 'com.j256.ormlite:ormlite-android:4.48'
  3. compile 'com.j256.ormlite:ormlite-core:4.48'
  4. }

3:在代码中如何使用ormlite

逻辑结构 a:写一个Modle类 b:写一个类继承OrmLiteSqliteOpenHelper c:在Activity中写逻辑(逻辑可另外写个类,Activity调用,类似MVP模式)

4:Modle 类

        
  1. @DatabaseTable(tableName = "test")
  2. public class Test {
  3. Test() {
  4. }//必须有
  5. //注意的是,声明的类型顺序会影响在数据库中的列续
  6. @DatabaseField(generatedId = true)
  7. private long id;
  8. @DatabaseField
  9. private long time;
  10. public long getId() {
  11. return id;
  12. }
  13. public void setId(long id) {
  14. this.id = id;
  15. }
  16. public long getTime() {
  17. return time;
  18. }
  19. public void setTime(long time) {
  20. this.time = time;
  21. }
  22. }

5:写一个DataBaseHelper

这个用于创建数据库和当数据库表有出现修改的时候,可在这里配置当数据表修改和升级后的表格。
        
  1. public class DataBaseHelper_ormlite extends OrmLiteSqliteOpenHelper {
  2. private static final String DATABASE_NAME = "ormlite.db";//数据库名字
  3. private static final int DATABASE_VERSION = 1;//版本
  4. private Dao<TheCacheInfo,Integer> theCacheInfos;
  5. private Dao<Test,Integer> integerDao;
  6. public DataBaseHelper_ormlite(Context context){
  7. super(context,DATABASE_NAME,null,DATABASE_VERSION);
  8. }
  9. @Override
  10. public void onCreate(SQLiteDatabase database, ConnectionSource connectionSource) {
  11. try {
  12. Log.i(DatabaseHelper.class.getName(), "onCreate");
  13. TableUtils.createTable(connectionSource, Test.class);
  14. //在这里可以添加数据进入到数据库,当数据库刚刚创建的时候
  15. // dao.create(simple);
  16. } catch (SQLException e) {
  17. Log.e(DatabaseHelper.class.getName(), "Can't create database", e);
  18. throw new RuntimeException(e);
  19. }
  20. }
  21. @Override
  22. public void onUpgrade(SQLiteDatabase database, ConnectionSource connectionSource, int oldVersion, int newVersion) {
  23. try {
  24. Log.i(DatabaseHelper.class.getName(), "onUpgrade");
  25. TableUtils.dropTable(connectionSource, Test.class, true);
  26. // after we drop the old databases, we create the new ones
  27. // 会终止旧的数据库,会创建一个新的数据库
  28. onCreate(database, connectionSource);
  29. } catch (SQLException e) {
  30. Log.e(DatabaseHelper.class.getName(), "Can't drop databases", e);
  31. throw new RuntimeException(e);
  32. }
  33. }
  34. //获取数据库Dao操作
  35. public Dao<Test,Integer> getintegerDao() throws SQLException {
  36. if (integerDao==null){
  37. integerDao = getDao(Test.class);
  38. }
  39. return integerDao;
  40. }
  41. @Override
  42. public void close() {//关闭Dao
  43. super.close();
  44. integerDao = null;
  45. }
  46. }
在写完 DataBaseHelper之后,我们需要在Activity中使用这个DataBaseHelper的DAO

6:Activity 中的调用

        
  1. public class TestUI extends BaseActivity {
  2. private Dao<Test, Integer> simpleDao = null;
  3. private static String TAG = "TestUI";
  4. @Override
  5. protected void onCreate(Bundle arg0) {
  6. super.onCreate(arg0);
  7. setContentView(R.layout.testui_layout);
  8. init();
  9. }
  10. private void init() {
  11. try {
  12. simpleDao = getHelper().getintegerDao();
  13. } catch (SQLException e) {
  14. e.printStackTrace();
  15. }
  16. }
  17. public void ADD(View view) {
  18. if (simpleDao == null) {
  19. init();
  20. }//判断是否为空
  21. add(System.currentTimeMillis());//添加数据
  22. }
  23. private void add(long time) {
  24. try {
  25. Test test = new Test();
  26. test.setTime(time);
  27. simpleDao.create(test);
  28. Log.w(TAG, "数据添加成功 :" + time);
  29. } catch (SQLException e) {
  30. e.printStackTrace();
  31. }
  32. }
  33. public void Query(View view) {//对添加的数据查询
  34. Log.w(TAG,"Query");
  35. // try {
  36. // List<Test> list = simpleDao.queryForAll();
  37. // for (Test test:list){
  38. // Log.w(TAG,"id:"+test.getId()+" 秒:"+test.getTime());
  39. // }
  40. //
  41. // } catch (SQLException e) {
  42. // e.printStackTrace();
  43. // }
  44. query(5, offest0);
  45. }
  46. private long offest0 = 0;//偏移量
  47. private void query(long number, long offest) {
  48. // Log.w("数据:","query开始");
  49. try {
  50. GenericRawResults<String[]> rawResults =
  51. simpleDao.queryRaw(
  52. "select * from test order by time DESC limit " + number + " offset " + offest);//测试可用

  53. List<String[]> list = rawResults.getResults();
  54. if (list.size()<number){
  55. offest0= offest0+list.size();
  56. }else {
  57. offest0 = offest0+number;
  58. }
  59. for (String[] strings : list) {
  60. for (String item :strings){
  61. Log.w("数据:",""+item);
  62. }
  63. }//判断String 的长度和内容
  64. } catch (SQLException e) {
  65. e.printStackTrace();
  66. }
  67. }
  68. public void DeleteAll(View view) throws SQLException {
  69. simpleDao.delete(simpleDao.queryForAll());
  70. }
  71. private DataBaseHelper_ormlite databaseHelper = null;
  72. private DataBaseHelper_ormlite getHelper() {//通过反射机制,获取databaseHelper 得到Dao
  73. if (databaseHelper == null) {
  74. databaseHelper = OpenHelperManager.getHelper(this, DataBaseHelper_ormlite.class);
  75. }
  76. return databaseHelper;
  77. }
  78. @Override
  79. protected void onDestroy() {//每次Destroy后,销毁资源
  80. super.onDestroy();
  81. databaseHelper.close();
  82. if (databaseHelper != null) {
  83. OpenHelperManager.releaseHelper();
  84. databaseHelper = null;
  85. }
  86. }
  87. }

7:布局文件

        
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="vertical" android:layout_width="match_parent"
  4. android:layout_height="match_parent">
  5. <Button
  6. android:layout_width="match_parent"
  7. android:layout_height="wrap_content"
  8. android:onClick="ADD"
  9. android:text="添加数据"/>
  10. <Button
  11. android:layout_width="match_parent"
  12. android:layout_height="wrap_content"
  13. android:onClick="Query"
  14. android:text="查询数据"/>
  15. <Button
  16. android:onClick="DeleteAll"
  17. android:layout_width="match_parent"
  18. android:layout_height="wrap_content"
  19. android:text="删除全部"/>
  20. </LinearLayout>

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



来自为知笔记(Wiz)

更多相关文章

  1. Android的四大组件
  2. Learning Android(第一季)01-Android平台一日游
  3. 系出名门Android(9) - 数据库支持(SQLite),
  4. android manifest.xml 标签汇总
  5. Android的四大组件
  6. 简析Android对Linux内核的改动-(上)
  7. 【Android】SD卡的安全存储问题
  8. Android自定义视图一:扩展现有的视图,添加新的XML属性
  9. Android(安卓)Media (Audio) Framework Analyse

随机推荐

  1. 精通 Linux 的 ls 命令
  2. linux常用命令--开发调试篇
  3. 玩转 Linux,掌握这些 Linux 命令就够了!
  4. 对 Linux 新手非常有用的 20 个命令
  5. 看看函数库
  6. 普通人打工,用创业心态干好8小时工作
  7. PHP基础:COOKIE/SESSION和命名空间
  8. 大牛干货,MySQL命令1000行整理,收藏学习
  9. 必须掌握的30种SQL语句优化
  10. 用心整理,1000行MySQL命令,很实用,建议收藏