【转】Android 纯代码化编码4_访问本地数据库帮助类

来源:互联网 发布:javascrip 和js 编辑:程序博客网 时间:2024/05/17 09:23

安卓自己就提供一个本地数据库供用户使用,它的具体放置的位置我记不住了,大家百度一下就知道,我们实际开发的过程中肯定是要进行一定程度的封装的,方便操作,下面就是我自己封装的一个帮助类,希望对大家有所帮助,方法名仿照的hibernate

 

Java代码  收藏代码
  1. import java.util.ArrayList;  
  2. import java.util.HashMap;  
  3. import java.util.List;  
  4. import java.util.Map;  
  5.   
  6. import android.content.Context;  
  7. import android.database.Cursor;  
  8. import android.database.sqlite.SQLiteDatabase;  
  9. import android.database.sqlite.SQLiteDatabase.CursorFactory;  
  10. import android.database.sqlite.SQLiteOpenHelper;  
  11. import android.database.sqlite.SQLiteStatement;  
  12.   
  13. /** 
  14.  * 安卓本地数据库访问对象 
  15.  *  
  16.  * @author pandong 
  17.  * @date 2012-7-12 上午10:15:20 
  18.  */  
  19. public class SqliteDataBase extends SQLiteOpenHelper {  
  20.   
  21.     private static final int VERSION = 1;  
  22.   
  23.     // 在SQLiteOepnHelper的子类当中,必须有该构造函数  
  24.     public SqliteDataBase(Context context, String name, CursorFactory factory, int version) {  
  25.         // 必须通过super调用父类当中的构造函数  
  26.         super(context, name, factory, version);  
  27.     }  
  28.   
  29.     public SqliteDataBase(Context context, String name) {  
  30.         this(context, name, VERSION);  
  31.     }  
  32.   
  33.     public SqliteDataBase(Context context, String name, int version) {  
  34.         this(context, name, null, version);  
  35.     }  
  36.   
  37.     /** 
  38.      * 执行语句并获取首行首列 
  39.      */  
  40.     public int queryForInt(String sql) {  
  41.         SQLiteDatabase db = getReadableDatabase();  
  42.         String result = null;  
  43.         try {  
  44.             SQLiteStatement stmt = db.compileStatement(sql);  
  45.             result = stmt.simpleQueryForLong() + "";  
  46.         } catch (Exception e) {  
  47.             e.printStackTrace();  
  48.         } finally {  
  49.             close(db, nullnull);  
  50.         }  
  51.   
  52.         return Integer.parseInt(result);  
  53.     }  
  54.   
  55.     /** 
  56.      * 执行语句返回一组元素 
  57.      */  
  58.     public List<Map> queryForList(String sql) {  
  59.         List<Map> resultList = new ArrayList<Map>();  
  60.         SQLiteDatabase db = getReadableDatabase();  
  61.         Cursor cursor = null;  
  62.         try {  
  63.             cursor = db.rawQuery(sql, null);  
  64.             while (cursor.moveToNext()) {  
  65.                 int columnCount = cursor.getColumnCount();  
  66.                 Map columnMap = new HashMap();  
  67.                 for (int i = 0; i < columnCount; i++) {  
  68.                     String columnName = cursor.getColumnName(i);  
  69.                     String columnValue = cursor.getString(i);  
  70.                     columnMap.put(columnName, columnValue);  
  71.                 }  
  72.                 resultList.add(columnMap);  
  73.             }  
  74.         } catch (Exception e) {  
  75.             e.printStackTrace();  
  76.         } finally {  
  77.             close(db, null, cursor);  
  78.         }  
  79.   
  80.         return resultList;  
  81.     }  
  82.   
  83.     /** 
  84.      * 执行语句并返回一个元素 
  85.      */  
  86.     public Map queryForMap(String sql) {  
  87.         Map columnMap = new HashMap();  
  88.         SQLiteDatabase db = getReadableDatabase();  
  89.         Cursor cursor = null;  
  90.         try {  
  91.             cursor = db.rawQuery(sql, null);  
  92.             if (cursor.moveToNext()) {  
  93.                 int columnCount = cursor.getColumnCount();  
  94.                 for (int i = 0; i < columnCount; i++) {  
  95.                     String columnName = cursor.getColumnName(i);  
  96.                     String columnValue = cursor.getString(i);  
  97.                     columnMap.put(columnName, columnValue);  
  98.                 }  
  99.             }  
  100.         } catch (Exception e) {  
  101.             e.printStackTrace();  
  102.         } finally {  
  103.             close(db, null, cursor);  
  104.         }  
  105.         return columnMap;  
  106.     }  
  107.   
  108.     /** 
  109.      * 执行一条insert语句 
  110.      */  
  111.     public int insert(String sql) {  
  112.         return executeUpdate(sql);  
  113.     }  
  114.   
  115.     /** 
  116.      * 事务方式执行一组insert语句 
  117.      */  
  118.     public int insert(String[] sql) {  
  119.         return executeUpdate(sql);  
  120.     }  
  121.   
  122.     /** 
  123.      * 执行一条预格式语句,预格式语句:insert t_test values(?,?,?) 
  124.      */  
  125.     public int insert(String psql, String[] data) {  
  126.         return executeUpdate(psql, data, "update");  
  127.     }  
  128.   
  129.     /** 
  130.      * 执行一条更新语句update/delete 
  131.      */  
  132.     public int update(String sql) {  
  133.         return executeUpdate(sql);  
  134.     }  
  135.   
  136.     /** 
  137.      * 事务执行一组更新语句update/delete 
  138.      */  
  139.     public int update(String[] sql) {  
  140.         return executeUpdate(sql);  
  141.     }  
  142.   
  143.     /** 
  144.      * 执行一条预格式语句,预格式语句:update t_test set t1 = ? 
  145.      */  
  146.     public int update(String psql, String[] data) {  
  147.         return executeUpdate(psql, data, "update");  
  148.     }  
  149.   
  150.     /** 
  151.      * 统一的执行语句的方法 
  152.      *  
  153.      * @param sql 
  154.      * @return 
  155.      */  
  156.     private int executeUpdate(String sql) {  
  157.         SQLiteDatabase db = getWritableDatabase();  
  158.         try {  
  159.             db.execSQL(sql);  
  160.         } catch (Exception e) {  
  161.             e.printStackTrace();  
  162.             return 0;  
  163.         } finally {  
  164.             close(db, nullnull);  
  165.         }  
  166.         return 1;  
  167.     }  
  168.   
  169.     /** 
  170.      * 统一的执行一组语句的方法 
  171.      *  
  172.      * @param sql 
  173.      * @return 
  174.      */  
  175.     private int executeUpdate(String[] sql) {  
  176.         SQLiteDatabase db = getWritableDatabase();  
  177.         try {  
  178.             db.beginTransaction();  
  179.             for (String curSql : sql) {  
  180.                 db.execSQL(curSql);  
  181.             }  
  182.             db.setTransactionSuccessful();  
  183.         } catch (Exception e) {  
  184.             e.printStackTrace();  
  185.             return 0;  
  186.         } finally {  
  187.             db.endTransaction();  
  188.             close(db, nullnull);  
  189.         }  
  190.         return 1;  
  191.     }  
  192.   
  193.     /** 
  194.      * 统一的执行预格式语句的方法 
  195.      *  
  196.      * @param psql 
  197.      * @param data 
  198.      * @param type 
  199.      * @return 
  200.      */  
  201.     private int executeUpdate(String psql, String[] data, String type) {  
  202.         SQLiteDatabase db = getWritableDatabase();  
  203.         SQLiteStatement stmt = null;  
  204.         String reuslt = "";  
  205.         try {  
  206.             stmt = db.compileStatement(psql);  
  207.             db.beginTransaction();  
  208.             for (int i = 1; i <= data.length; i++) {  
  209.                 stmt.bindString(i, data[i - 1]);  
  210.             }  
  211.             if ("insert".equals(type)) {  
  212.                 reuslt = stmt.executeInsert() + "";  
  213.             } else {  
  214.                 reuslt = stmt.executeUpdateDelete() + "";  
  215.             }  
  216.             db.setTransactionSuccessful();  
  217.         } catch (Exception e) {  
  218.             e.printStackTrace();  
  219.             return 0;  
  220.         } finally {  
  221.             db.endTransaction();  
  222.             close(db, stmt, null);  
  223.         }  
  224.         return Integer.parseInt(reuslt);  
  225.     }  
  226.   
  227.     public void onCreate(SQLiteDatabase db) {  
  228.         // 初始化基本表  
  229.         db.execSQL("create table t_sys_user(userId varchar(10) PRIMARY KEY,userName varchar(10), userPass varchar(10))");  
  230.     }  
  231.   
  232.     public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {  
  233.         // 什么也不做  
  234.     }  
  235.   
  236.     /** 
  237.      * 释放数据库资源的方法 
  238.      *  
  239.      * @param db 
  240.      * @param stmt 
  241.      * @param cursor 
  242.      */  
  243.     private void close(SQLiteDatabase db, SQLiteStatement stmt, Cursor cursor) {  
  244.         if (cursor != null) {  
  245.             cursor.close();  
  246.         }  
  247.         if (stmt != null) {  
  248.             stmt.close();  
  249.         }  
  250.         if (db != null) {  
  251.             db.close();  
  252.         }  
  253.     }  
  254. }  
 转自:http://pandong8183.iteye.com/blog/1602337
0 0
原创粉丝点击