zoukankan      html  css  js  c++  java
  • (转)Android中使用ormlite实现持久化(一)--HelloOrmLite

    Android中内置了sqlite,但是常用的开发语言java是面向对象的,而数据库是关系型的,二者之间的转化每次都很麻烦(主 要是我对sql语言不熟悉)。而Java Web开发中有很多orm框架,但是想直接放到Android上用有些麻烦。尝试了一下找Android的orm框架,说实话还有好几个。

      

    实现考虑的是:androrm  
    说实话,这个我实在没有弄懂,一共两个包。  
    一个是依赖包:Apache Commons - Lang (2.6)  
    另外一个就是主包:androrm.jar   不管怎么下载的都不能使用...  
      
    然后有考虑了一下db4o  
    官网上的介绍说是已经支持Android了,但是我一是觉得包有点大,而是觉得速度有点慢  
      
    最后看到的就是ormlite  
    官网:    http://ormlite.com/  
    一共两个包:一个是ormlite-core-4.24.jar,另一个是ormlite-android-4.24.jar   
    从以下网址可以下载到:     http://ormlite.com/releases/  
            
    下面按照惯例来个Hello world   
    新建Android项目:HelloOrmLite   

    Android中使用ormlite实现持久化(一)--HelloOrmLite

         
    添加文件夹:libs,将所需的两个包复制到其中。添加引用   

    Android中使用ormlite实现持久化(一)--HelloOrmLite

         新建一个model:Hello.java    
             
     1 package cn.sdx.model; 
     2 
     3 import com.j256.ormlite.field.DatabaseField; 
     4 
     5 public class Hello { 
     6 @DatabaseField(generatedId = true) 
     7 int id; 
     8 @DatabaseField 
     9 String word; 
    10 
    11 public Hello() { 
    12 } 
    13 
    14 @Override 
    15 public String toString() { 
    16   StringBuilder sb = new StringBuilder(); 
    17   sb.append("id=").append(id); 
    18   sb.append(" ,word=").append(word); 
    19   return sb.toString(); 
    20 } 
    21 
    22 } 
      
     
    @DatabaseField是声明id为数据库字段,generatedId =true声明id为自增长    
         然后重写了toString()    
              
    再添加一个DataHelper.java   
     1 package cn.sdx.utils; 
     2 
     3 import java.sql.SQLException; 
     4 
     5 import android.content.Context; 
     6 import android.database.sqlite.SQLiteDatabase; 
     7 import android.util.Log; 
     8 
     9 
    10 import cn.sdx.model.Hello; 
    11 
    12 import com.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper; 
    13 import com.j256.ormlite.dao.Dao; 
    14 import com.j256.ormlite.support.ConnectionSource; 
    15 import com.j256.ormlite.table.TableUtils; 
    16 
    17 public class DataHelper extends OrmLiteSqliteOpenHelper { 
    18 
    19 private static final String DATABASE_NAME = "HelloOrmlite.db"; 
    20 private static final int DATABASE_VERSION = 1; 
    21 private Dao
    22       
    23       
    24       
    25       
    26       <hello, integer="">
    27        
    28        
    29        
    30        
    31         helloDao = null; 
    32 
    33 public DataHelper(Context context) { 
    34   super(context, DATABASE_NAME, null, DATABASE_VERSION); 
    35 } 
    36 
    37 @Override 
    38 public void onCreate(SQLiteDatabase db, ConnectionSource connectionSource) { 
    39   try { 
    40    TableUtils.createTable(connectionSource, Hello.class); 
    41   } catch (SQLException e) { 
    42    Log.e(DataHelper.class.getName(), "创建数据库失败", e); 
    43    e.printStackTrace(); 
    44   } 
    45 } 
    46 
    47 @Override 
    48 public void onUpgrade(SQLiteDatabase db, ConnectionSource connectionSource, int arg2, 
    49    int arg3) { 
    50   try { 
    51    TableUtils.dropTable(connectionSource, Hello.class, true); 
    52    onCreate(db, connectionSource); 
    53   } catch (SQLException e) { 
    54    Log.e(DataHelper.class.getName(), "更新数据库失败", e); 
    55    e.printStackTrace(); 
    56   } 
    57 } 
    58 
    59 @Override 
    60 public void close() { 
    61   super.close(); 
    62   helloDao = null; 
    63 } 
    64 
    65 public Dao
    66        
    67        
    68        
    69        
    70        <hello, integer="">
    71         
    72         
    73         
    74         
    75          getHelloDataDao() throws SQLException { 
    76   if (helloDao == null) { 
    77    helloDao = getDao(Hello.class); 
    78   } 
    79   return helloDao; 
    80 } 
    81 } 
    82        
    83        
    84        
    85        
    86        </hello,>
    87       
    88       
    89       
    90       
    91       </hello,>
     
    在布局文件中添加一个TextView    
         HelloOrmliteActivity.java中添加对数据库的操作    
         
    代码如下
     1 package cn.sdx; 
     2 
     3 import java.sql.SQLException; 
     4 import java.util.List; 
     5 
     6 import com.j256.ormlite.android.apptools.OrmLiteBaseActivity; 
     7 import com.j256.ormlite.dao.Dao; 
     8 
     9 import android.os.Bundle; 
    10 import android.widget.TextView; 
    11 import cn.sdx.model.Hello; 
    12 import cn.sdx.utils.DataHelper; 
    13 
    14 public class HelloOrmliteActivity extends OrmLiteBaseActivity
    15     
    16     
    17     
    18     
    19     <datahelper>
    20      
    21      
    22      
    23      
    24       { 
    25 /** Called when the activity is first created. */ 
    26 @Override 
    27 public void onCreate(Bundle savedInstanceState) { 
    28   super.onCreate(savedInstanceState); 
    29   setContentView(R.layout.main); 
    30   TextView tv = (TextView) this.findViewById(R.id.output); 
    31   try { 
    32    Dao
    33      
    34      
    35      
    36      
    37      <hello, integer="">
    38       
    39       
    40       
    41       
    42        helloDao = getHelper().getHelloDataDao(); 
    43    // 添加数据 
    44    for (int i = 0; i < 2; i++) { 
    45     Hello hello = new Hello("Hello" + i); 
    46     helloDao.create(hello); 
    47    } 
    48    tv.setText(tv.getText() + "
    " + "添加数据完成"); 
    49    // 查询添加的数据 
    50    List
    51       
    52       
    53       
    54       
    55       <hello>
    56        
    57        
    58        
    59        
    60         hellos = helloDao.queryForAll(); 
    61    for (Hello h : hellos) { 
    62     tv.setText(tv.getText() + "
    " + h.toString()); 
    63    } 
    64    // 删除数据第一条数据 
    65    helloDao.delete(hellos.get(0)); 
    66    tv.setText(tv.getText() + "
    " + "删除数据完成"); 
    67    // 重新查询数据 
    68    hellos = helloDao.queryForAll(); 
    69    for (Hello h : hellos) { 
    70     tv.setText(tv.getText() + "
    " + h.toString()); 
    71    } 
    72    // 修改数据 
    73    Hello h1 = hellos.get(0); 
    74    h1.setWord("这是修改过的数据"); 
    75    tv.setText(tv.getText() + "
    " + "修改数据完成"); 
    76    helloDao.update(h1); 
    77    // 重新查询数据 
    78    hellos = helloDao.queryForAll(); 
    79    for (Hello h : hellos) { 
    80     tv.setText(tv.getText() + "
    " + h.toString()); 
    81    } 
    82 
    83   } catch (SQLException e) { 
    84    // TODO Auto-generated catch block 
    85    e.printStackTrace(); 
    86   } 
    87 
    88 } 
    89 } 
    90       
    91       
    92       
    93       
    94     
    95     
    96     
    97     </datahelper>
    以上实现了数据库操作相关的增删改,下面是效果:    
         

    Android中使用ormlite实现持久化(一)--HelloOrmLite

          
              
       OrmLite的功能非常强大,Model类的声明中非常重要,外键约束,非空检查等等问题都有相对的处理方法。
  • 相关阅读:
    转:CSS设置HTML元素的高度与宽度的各种情况总结
    Java、mysql、html、css、js 注释&大小写
    Dom4j与sax 简单对比
    转:Java properties | FileNotFoundException: properties (系统找不到指定的文件。)
    转:SAX解析的characters方法被多次调用
    转:HashMap实现原理分析(面试问题:两个hashcode相同 的对象怎么存入hashmap的)
    转:Scanner中nextLine()方法和next()方法的区别
    转:JDBC中关于PreparedStatement.setObject的一些细节说明
    转:Eclipse 各种小图标的含义
    转:Mysql float类型where 语句判断相等问题
  • 原文地址:https://www.cnblogs.com/dongweiq/p/3802599.html
Copyright © 2011-2022 走看看