(转)Android 使用com.j256.ormlite

在web开发中经常采用的hibernate,在android也提供了一个ormlite

导入所需jar包后

 摘自:http://blog.csdn.net/cuiran/article/details/24722885

[java] view plaincopy在CODE上查看代码片派生到我的代码片
 
  1. /** 
  2.  * SQLiteHelperOrm.java 
  3.  * 版权所有(C) 2014 
  4.  * 创建者:cuiran 2014-2-12 下午3:18:40 
  5.  */  
  6. package com.ghyf.mplay.database;  
  7.   
  8. import java.sql.SQLException;  
  9.   
  10. import android.content.Context;  
  11. import android.database.sqlite.SQLiteDatabase;  
  12.   
  13. import com.ghyf.mplay.application.BaseCookie;  
  14. import com.ghyf.mplay.po.POPriorityText;  
  15. import com.ghyf.mplay.po.POTest;  
  16. import com.ghyf.mplay.util.LogUtil;  
  17. import com.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper;  
  18. import com.j256.ormlite.support.ConnectionSource;  
  19. import com.j256.ormlite.table.TableUtils;  
  20.   
  21.   
  22. /** 
  23.  * TODO  
  24.  * @author cuiran 
  25.  * @version 1.0.0 
  26.  */  
  27. public class SQLiteHelperOrm extends OrmLiteSqliteOpenHelper {  
  28.     private static final String TAG="SQLiteHelperOrm";  
  29.     private static final String DATABASE_NAME = "mplay.db";  
  30.     private static final int DATABASE_VERSION = 3;  
  31.   
  32.     public SQLiteHelperOrm(Context context) {  
  33.         super(context, DATABASE_NAME, null, DATABASE_VERSION);  
  34.     }  
  35.   
  36.     public SQLiteHelperOrm() {  
  37.         super(BaseCookie.getContext(), DATABASE_NAME, null, DATABASE_VERSION);  
  38.     }  
  39.   
  40.     @Override  
  41.     public void onCreate(SQLiteDatabase db, ConnectionSource connectionSource) {  
  42.         try {  
  43.             TableUtils.createTable(connectionSource, POTest.class);  
  44.             TableUtils.createTable(connectionSource, POPriorityText.class);  
  45.         } catch (SQLException e) {  
  46.             LogUtil.e(TAG,"onCreate",e);  
  47.         }  
  48.     }  
  49.   
  50.     @Override  
  51.     public void onUpgrade(SQLiteDatabase db, ConnectionSource connectionSource, int arg2, int arg3) {  
  52.         try {  
  53.             TableUtils.dropTable(connectionSource, POTest.class, true);  
  54.             TableUtils.dropTable(connectionSource, POPriorityText.class, true);  
  55.             onCreate(db, connectionSource);  
  56.         } catch (SQLException e) {  
  57.             LogUtil.e(TAG,"onUpgrade",e);  
  58.         }  
  59.     }  
  60.   
  61. }  

 

[java] view plaincopy在CODE上查看代码片派生到我的代码片
 
  1. /** 
  2.  * DbHelper.java 
  3.  * 版权所有(C) 2014 
  4.  * 创建者:cuiran 2014-2-12 下午3:37:07 
  5.  */  
  6. package com.ghyf.mplay.database;  
  7.   
  8. import java.sql.SQLException;  
  9. import java.util.ArrayList;  
  10. import java.util.List;  
  11. import java.util.Map;  
  12.   
  13. import android.annotation.TargetApi;  
  14. import android.content.ContentValues;  
  15. import android.os.Build;  
  16.   
  17. import com.ghyf.mplay.util.LogUtil;  
  18. import com.j256.ormlite.dao.Dao;  
  19. import com.j256.ormlite.stmt.UpdateBuilder;  
  20.   
  21.   
  22. /** 
  23.  *  DbHelper 数据库操作类 
  24.  * @author cuiran 
  25.  * @version 1.0.0 
  26.  */  
  27. public class DbHelper<T> {  
  28.     private static final String TAG="DbHelper";  
  29.     /** 新增一条记录 */  
  30.     public int create(T po) {  
  31.         SQLiteHelperOrm db = new SQLiteHelperOrm();  
  32.         try {  
  33.             Dao dao = db.getDao(po.getClass());  
  34.             return dao.create(po);  
  35.         } catch (SQLException e) {  
  36.             LogUtil.e(TAG,"create",e);  
  37.         } finally {  
  38.             if (db != null)  
  39.                 db.close();  
  40.         }  
  41.         return -1;  
  42.     }  
  43.   
  44.     public boolean exists(T po, Map<String, Object> where) {  
  45.         SQLiteHelperOrm db = new SQLiteHelperOrm();  
  46.         try {  
  47.             Dao dao = db.getDao(po.getClass());  
  48.             if (dao.queryForFieldValues(where).size() > 0) {  
  49.                 return true;  
  50.             }  
  51.         } catch (SQLException e) {  
  52.             LogUtil.e(TAG,"exists",e);  
  53.         } finally {  
  54.             if (db != null)  
  55.                 db.close();  
  56.         }  
  57.         return false;  
  58.     }  
  59.   
  60.     public int createIfNotExists(T po, Map<String, Object> where) {  
  61.         SQLiteHelperOrm db = new SQLiteHelperOrm();  
  62.         try {  
  63.             Dao dao = db.getDao(po.getClass());  
  64.             if (dao.queryForFieldValues(where).size() < 1) {  
  65.                 return dao.create(po);  
  66.             }  
  67.         } catch (SQLException e) {  
  68.             LogUtil.e(TAG,"createIfNotExists",e);  
  69.         } finally {  
  70.             if (db != null)  
  71.                 db.close();  
  72.         }  
  73.         return -1;  
  74.     }  
  75.   
  76.     /** 查询一条记录 */  
  77.     public List<T> queryForEq(Class<T> c, String fieldName, Object value) {  
  78.         SQLiteHelperOrm db = new SQLiteHelperOrm();  
  79.         try {  
  80.             Dao dao = db.getDao(c);  
  81.             return dao.queryForEq(fieldName, value);  
  82.         } catch (SQLException e) {  
  83.             LogUtil.e(TAG,"queryForEq",e);  
  84.         } finally {  
  85.             if (db != null)  
  86.                 db.close();  
  87.         }  
  88.         return new ArrayList<T>();  
  89.     }  
  90.   
  91.     /** 删除一条记录 */  
  92.     public int remove(T po) {  
  93.         SQLiteHelperOrm db = new SQLiteHelperOrm();  
  94.         try {  
  95.             Dao dao = db.getDao(po.getClass());  
  96.             return dao.delete(po);  
  97.         } catch (SQLException e) {  
  98.             LogUtil.e(TAG,"remove",e);  
  99.         } finally {  
  100.             if (db != null)  
  101.                 db.close();  
  102.         }  
  103.         return -1;  
  104.     }  
  105.   
  106.     /** 
  107.      * 根据特定条件更新特定字段 
  108.      *  
  109.      * @param c 
  110.      * @param values 
  111.      * @param columnName where字段 
  112.      * @param value where值 
  113.      * @return 
  114.      */  
  115.     @TargetApi(Build.VERSION_CODES.HONEYCOMB)  
  116.     public int update(Class<T> c, ContentValues values, String columnName, Object value) {  
  117.         SQLiteHelperOrm db = new SQLiteHelperOrm();  
  118.         try {  
  119.             Dao dao = db.getDao(c);  
  120.             UpdateBuilder<T, Long> updateBuilder = dao.updateBuilder();  
  121.             updateBuilder.where().eq(columnName, value);  
  122.             for (String key : values.keySet()) {  
  123.                 updateBuilder.updateColumnValue(key, values.get(key));  
  124.             }  
  125.             return updateBuilder.update();  
  126.         } catch (SQLException e) {  
  127.             LogUtil.e(TAG,"update",e);  
  128.         } finally {  
  129.             if (db != null)  
  130.                 db.close();  
  131.         }  
  132.         return -1;  
  133.     }  
  134.   
  135.     /** 更新一条记录 */  
  136.     public int update(T po) {  
  137.         SQLiteHelperOrm db = new SQLiteHelperOrm();  
  138.         try {  
  139.   
  140.             Dao dao = db.getDao(po.getClass());  
  141.             return dao.update(po);  
  142.         } catch (SQLException e) {  
  143.             LogUtil.e(TAG,"update",e);  
  144.         } finally {  
  145.             if (db != null)  
  146.                 db.close();  
  147.         }  
  148.         return -1;  
  149.     }  
  150.   
  151.     /** 查询所有记录 */  
  152.     public List<T> queryForAll(Class<T> c) {  
  153.         SQLiteHelperOrm db = new SQLiteHelperOrm();  
  154.         try {  
  155.             Dao dao = db.getDao(c);  
  156.             return dao.queryForAll();  
  157.         } catch (SQLException e) {  
  158.             LogUtil.e(TAG,"queryForAll",e);  
  159.         } finally {  
  160.             if (db != null)  
  161.                 db.close();  
  162.         }  
  163.         return new ArrayList<T>();  
  164.     }  
  165.       
  166. }  


新建一个PO

 

 

[java] view plaincopy在CODE上查看代码片派生到我的代码片
 
    1. /** 
    2.  * POTest.java 
    3.  * 版权所有(C) 2014 
    4.  * 创建者:cuiran 2014-2-12 下午3:25:08 
    5.  */  
    6. package com.ghyf.mplay.po;  
    7.   
    8. import com.j256.ormlite.field.DatabaseField;  
    9. import com.j256.ormlite.table.DatabaseTable;  
    10.   
    11. /** 
    12.  *  POTest DB的测试PO 
    13.  * @author cuiran 
    14.  * @version 1.0.0 
    15.  */  
    16. @DatabaseTable(tableName = "test")  
    17. public class POTest {  
    18.   
    19.     @DatabaseField(generatedId = true)  
    20.     public long _id;  
    21.     /** 标题 */  
    22.     @DatabaseField  
    23.     public String title;  
    24.     /** 标题 */  
    25.     @DatabaseField  
    26.     public int position;  
    27.     public POTest(long _id, String title, int position) {  
    28.         super();  
    29.         this._id = _id;  
    30.         this.title = title;  
    31.         this.position = position;  
    32.     }  
    33.     public POTest() {  
    34.         super();  
    35.     }  
    36.       
    37.       
    38.       
    39. }  

posted on 2015-07-08 11:41  antyi  阅读(990)  评论(0编辑  收藏  举报

导航