Android数据库(SqlLite)操作和db文件查看

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

企鹅博客
  • 本文由 发表于 2019年9月8日 05:58:30
  • 转载请务必保留本文链接:https://www.qieseo.com/175002.html

发表评论