Android数据库(SqlLite)操作

 

  
  
  
  
  1. 1.package com.xiaoshan.udp.client.db;    
  2.  2.    
  3.  3.import Android.content.ContentValues;    
  4.  4.import Android.content.Context;    
  5.  5.import Android.database.Cursor;    
  6.  6.import Android.database.SQLException;    
  7.  7.import Android.database.sqlite.SQLiteDatabase;    
  8.  8.import Android.database.sqlite.SQLiteOpenHelper;    
  9.  9.    
  10.  10./**   
  11.  11. * 数据库常用操作的封装类   
  12.  12. *    
  13.  13. *   
  14.  14. *    
  15.  15. */    
  16.  16.public class DBHelper {    
  17.  17.    
  18.  18.    private static DatabaseHelper mDbHelper;    
  19.  19.    private static SQLiteDatabase mDb;    
  20.  20.    
  21.  21.    private static final String DATABASE_NAME = "shanhy.db";    
  22.  22.    
  23.  23.    private static final int DATABASE_VERSION = 1;    
  24.  24.    
  25.  25.    private final Context mCtx;    
  26.  26.    
  27.  27.    private static class DatabaseHelper extends SQLiteOpenHelper {    
  28.  28.    
  29.  29.        DatabaseHelper(Context context) {    
  30.  30.            super(context, DATABASE_NAME, null, DATABASE_VERSION);    
  31.  31.        }    
  32.  32.    
  33.  33.        @Override    
  34.  34.        public void onCreate(SQLiteDatabase db) {    
  35.  35.        }    
  36.  36.    
  37.  37.        @Override    
  38.  38.        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {    
  39.  39.        }    
  40.  40.    }    
  41.  41.    
  42.  42.    public DBHelper(Context ctx) {    
  43.  43.        this.mCtx = ctx;    
  44.  44.    }    
  45.  45.    
  46.  46.    public DBHelper open() throws SQLException {    
  47.  47.        mDbHelper = new DatabaseHelper(mCtx);    
  48.  48.        mDb = mDbHelper.getWritableDatabase();    
  49.  49.        return this;    
  50.  50.    }    
  51.  51.    
  52.  52.    /**   
  53.  53.     * 关闭数据源   
  54.  54.     *    
  55.  55.     * @author SHANHY   
  56.  56.     */    
  57.  57.    public void closeConnection() {    
  58.  58.        if (mDb != null && mDb.isOpen())    
  59.  59.            mDb.close();    
  60.  60.        if (mDbHelper != null)    
  61.  61.            mDbHelper.close();    
  62.  62.    }    
  63.  63.    
  64.  64.    /**   
  65.  65.     * 插入数据 参数   
  66.  66.     *    
  67.  67.     * @param tableName   
  68.  68.     *            表名   
  69.  69.     * @param initialValues   
  70.  70.     *            要插入的列对应值   
  71.  71.     * @return   
  72.  72.     * @author SHANHY   
  73.  73.     */    
  74.  74.    public long insert(String tableName, ContentValues initialValues) {    
  75.  75.    
  76.  76.        return mDb.insert(tableName, null, initialValues);    
  77.  77.    }    
  78.  78.    
  79.  79.    /**   
  80.  80.     * 删除数据   
  81.  81.     *    
  82.  82.     * @param tableName   
  83.  83.     *            表名   
  84.  84.     * @param deleteCondition   
  85.  85.     *            条件   
  86.  86.     * @param deleteArgs   
  87.  87.     *            条件对应的值(如果deleteCondition中有“?”号,将用此数组中的值替换,一一对应)   
  88.  88.     * @return   
  89.  89.     * @author SHANHY   
  90.  90.     */    
  91.  91.    public boolean delete(String tableName, String deleteCondition, String[] deleteArgs) {    
  92.  92.    
  93.  93.        return mDb.delete(tableName, deleteCondition, deleteArgs) > 0;    
  94.  94.    }    
  95.  95.    
  96.  96.    /**   
  97.  97.     * 更新数据   
  98.  98.     *    
  99.  99.     * @param tableName   
  100.  100.     *            表名   
  101.  101.     * @param initialValues   
  102.  102.     *            要更新的列   
  103.  103.     * @param selection   
  104.  104.     *            更新的条件   
  105.  105.     * @param selectArgs   
  106.  106.     *            更新条件中的“?”对应的值   
  107.  107.     * @return   
  108.  108.     * @author SHANHY   
  109.  109.     */    
  110.  110.    public boolean update(String tableName, ContentValues initialValues, String selection, String[] selectArgs) {    
  111.  111.        return mDb.update(tableName, initialValues, selection, selectArgs) > 0;    
  112.  112.    }    
  113.  113.    
  114.  114.    /**   
  115.  115.     * 取得一个列表   
  116.  116.     *    
  117.  117.     * @param distinct   
  118.  118.     *            是否去重复   
  119.  119.     * @param tableName   
  120.  120.     *            表名   
  121.  121.     * @param columns   
  122.  122.     *            要返回的列   
  123.  123.     * @param selection   
  124.  124.     *            条件   
  125.  125.     * @param selectionArgs   
  126.  126.     *            条件中“?”的参数值   
  127.  127.     * @param groupBy   
  128.  128.     *            分组   
  129.  129.     * @param having   
  130.  130.     *            分组过滤条件   
  131.  131.     * @param orderBy   
  132.  132.     *            排序   
  133.  133.     * @return   
  134.  134.     * @author SHANHY   
  135.  135.     */    
  136.  136.    public Cursor findList(boolean distinct, String tableName, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy, String limit) {    
  137.  137.    
  138.  138.        return mDb.query(distinct, tableName, columns, selection, selectionArgs, groupBy, having, orderBy, limit);    
  139.  139.    }    
  140.  140.    
  141.  141.    /**   
  142.  142.     * 取得单行记录   
  143.  143.     *    
  144.  144.     * @param tableName   
  145.  145.     *            表名   
  146.  146.     * @param columns   
  147.  147.     *            获取的列数组   
  148.  148.     * @param selection   
  149.  149.     *            条件   
  150.  150.     * @param selectionArgs   
  151.  151.     *            条件中“?”对应的值   
  152.  152.     * @param groupBy   
  153.  153.     *            分组   
  154.  154.     * @param having   
  155.  155.     *            分组条件   
  156.  156.     * @param orderBy   
  157.  157.     *            排序   
  158.  158.     * @param limit   
  159.  159.     *            数据区间   
  160.  160.     * @param distinct   
  161.  161.     *            是否去重复   
  162.  162.     * @return   
  163.  163.     * @throws SQLException   
  164.  164.     * @author SHANHY   
  165.  165.     */    
  166.  166.    public Cursor findOne(boolean distinct,String tableName, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy, String limit) throws SQLException {    
  167.  167.    
  168.  168.        Cursor mCursor = findList(distinct, tableName, columns, selection, selectionArgs, groupBy, having, orderBy, limit);    
  169.  169.    
  170.  170.        if (mCursor != null) {    
  171.  171.            mCursor.moveToFirst();    
  172.  172.        }    
  173.  173.        return mCursor;    
  174.  174.    
  175.  175.    }    
  176.  176.    
  177.  177.    /**   
  178.  178.     * 执行SQL(带参数)   
  179.  179.     *    
  180.  180.     * @param sql   
  181.  181.     * @param args   
  182.  182.     *            SQL中“?”参数值   
  183.  183.     * @author SHANHY   
  184.  184.     */    
  185.  185.    public void execSQL(String sql, Object[] args) {    
  186.  186.        mDb.execSQL(sql, args);    
  187.  187.    
  188.  188.    }    
  189.  189.    
  190.  190.    /**   
  191.  191.     * 执行SQL   
  192.  192.     *    
  193.  193.     * @param sql   
  194.  194.     * @author SHANHY   
  195.  195.     */    
  196.  196.    public void execSQL(String sql) {    
  197.  197.        mDb.execSQL(sql);    
  198.  198.    
  199.  199.    }    
  200.  200.    
  201.  201.    /**   
  202.  202.     * 判断某张表是否存在   
  203.  203.     *    
  204.  204.     * @param tabName   
  205.  205.     *            表名   
  206.  206.     * @return   
  207.  207.     */    
  208.  208.    public boolean isTableExist(String tableName) {    
  209.  209.        boolean result = false;    
  210.  210.        if (tableName == null) {    
  211.  211.            return false;    
  212.  212.        }    
  213.  213.    
  214.  214.        try {    
  215.  215.            Cursor cursor = null;    
  216.  216.            String sql = "select count(1) as c from sqlite_master where type ='table' and name ='" + tableName.trim() + "'";    
  217.  217.            cursor = mDb.rawQuery(sql, null);    
  218.  218.            if (cursor.moveToNext()) {    
  219.  219.                int count = cursor.getInt(0);    
  220.  220.                if (count > 0) {    
  221.  221.                    result = true;    
  222.  222.                }    
  223.  223.            }    
  224.  224.    
  225.  225.            cursor.close();    
  226.  226.        } catch (Exception e) {    
  227.  227.        }    
  228.  228.        return result;    
  229.  229.    }    
  230.  230.    
  231.  231.    /**   
  232.  232.     * 判断某张表中是否存在某字段(注,该方法无法判断表是否存在,因此应与isTableExist一起使用)   
  233.  233.     *    
  234.  234.     * @param tabName   
  235.  235.     *            表名   
  236.  236.     * @param columnName   
  237.  237.     *            列名   
  238.  238.     * @return   
  239.  239.     */    
  240.  240.    public boolean isColumnExist(String tableName, String columnName) {    
  241.  241.        boolean result = false;    
  242.  242.        if (tableName == null) {    
  243.  243.            return false;    
  244.  244.        }    
  245.  245.    
  246.  246.        try {    
  247.  247.            Cursor cursor = null;    
  248.  248.            String sql = "select count(1) as c from sqlite_master where type ='table' and name ='" + tableName.trim() + "' and sql like '%" + columnName.trim() + "%'";    
  249.  249.            cursor = mDb.rawQuery(sql, null);    
  250.  250.            if (cursor.moveToNext()) {    
  251.  251.                int count = cursor.getInt(0);    
  252.  252.                if (count > 0) {    
  253.  253.                    result = true;    
  254.  254.                }    
  255.  255.            }    
  256.  256.    
  257.  257.            cursor.close();    
  258.  258.        } catch (Exception e) {    
  259.  259.        }    
  260.  260.        return result;    
  261.  261.    }    
  262.  262.    
  263.  263.}    
  264.  

 

你可能感兴趣的:(数据库,android)