歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android數據庫(SqlLite)操作和db文件查看

Android數據庫(SqlLite)操作和db文件查看

日期:2017/3/1 10:22:28   编辑:Linux編程

Android數據庫(SqlLite)操作和db文件查看操作步驟很簡單,首先導入sqlLite 的DB文件(即File Explorer /data /data/),然後進行各種sql操作。

下面是我的代碼:

  1. package com.xiaoshan.udp.client.db;
  2. import android.content.ContentValues;
  3. import android.content.Context;
  4. import android.database.Cursor;
  5. import android.database.SQLException;
  6. import android.database.sqlite.SQLiteDatabase;
  7. import android.database.sqlite.SQLiteOpenHelper;
  8. /**
  9. * 數據庫常用操作的封裝類
  10. *
  11. * @author 單紅宇
  12. *
  13. */
  14. public class DBHelper {
  15. private static DatabaseHelper mDbHelper;
  16. private static SQLiteDatabase mDb;
  17. private static final String DATABASE_NAME = "shanhy.db";
  18. private static final int DATABASE_VERSION = 1;
  19. private final Context mCtx;
  20. private static class DatabaseHelper extends SQLiteOpenHelper {
  21. DatabaseHelper(Context context) {
  22. super(context, DATABASE_NAME, null, DATABASE_VERSION);
  23. }
  24. @Override
  25. public void onCreate(SQLiteDatabase db) {
  26. }
  27. @Override
  28. public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
  29. }
  30. }
  31. public DBHelper(Context ctx) {
  32. this.mCtx = ctx;
  33. }
  34. public DBHelper open() throws SQLException {
  35. mDbHelper = new DatabaseHelper(mCtx);
  36. mDb = mDbHelper.getWritableDatabase();
  37. return this;
  38. }
  39. /**
  40. * 關閉數據源
  41. *
  42. * @author SHANHY
  43. */
  44. public void closeConnection() {
  45. if (mDb != null && mDb.isOpen())
  46. mDb.close();
  47. if (mDbHelper != null)
  48. mDbHelper.close();
  49. }
  50. /**
  51. * 插入數據 參數
  52. *
  53. * @param tableName
  54. * 表名
  55. * @param initialValues
  56. * 要插入的列對應值
  57. * @return
  58. * @author SHANHY
  59. */
  60. public long insert(String tableName, ContentValues initialValues) {
  61. return mDb.insert(tableName, null, initialValues);
  62. }
  63. /**
  64. * 刪除數據
  65. *
  66. * @param tableName
  67. * 表名
  68. * @param deleteCondition
  69. * 條件
  70. * @param deleteArgs
  71. * 條件對應的值(如果deleteCondition中有“?”號,將用此數組中的值替換,一一對應)
  72. * @return
  73. * @author SHANHY
  74. */
  75. public boolean delete(String tableName, String deleteCondition, String[] deleteArgs) {
  76. return mDb.delete(tableName, deleteCondition, deleteArgs) > 0;
  77. }
  78. /**
  79. * 更新數據
  80. *
  81. * @param tableName
  82. * 表名
  83. * @param initialValues
  84. * 要更新的列
  85. * @param selection
  86. * 更新的條件
  87. * @param selectArgs
  88. * 更新條件中的“?”對應的值
  89. * @return
  90. * @author SHANHY
  91. */
  92. public boolean update(String tableName, ContentValues initialValues, String selection, String[] selectArgs) {
  93. return mDb.update(tableName, initialValues, selection, selectArgs) > 0;
  94. }
  95. /**
  96. * 取得一個列表
  97. *
  98. * @param distinct
  99. * 是否去重復
  100. * @param tableName
  101. * 表名
  102. * @param columns
  103. * 要返回的列
  104. * @param selection
  105. * 條件
  106. * @param selectionArgs
  107. * 條件中“?”的參數值
  108. * @param groupBy
  109. * 分組
  110. * @param having
  111. * 分組過濾條件
  112. * @param orderBy
  113. * 排序
  114. * @return
  115. * @author SHANHY
  116. */
  117. public Cursor findList(boolean distinct, String tableName, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy, String limit) {
  118. return mDb.query(distinct, tableName, columns, selection, selectionArgs, groupBy, having, orderBy, limit);
  119. }
  120. /**
  121. * 取得單行記錄
  122. *
  123. * @param tableName
  124. * 表名
  125. * @param columns
  126. * 獲取的列數組
  127. * @param selection
  128. * 條件
  129. * @param selectionArgs
  130. * 條件中“?”對應的值
  131. * @param groupBy
  132. * 分組
  133. * @param having
  134. * 分組條件
  135. * @param orderBy
  136. * 排序
  137. * @param limit
  138. * 數據區間
  139. * @param distinct
  140. * 是否去重復
  141. * @return
  142. * @throws SQLException
  143. * @author SHANHY
  144. */
  145. public Cursor findOne(boolean distinct,String tableName, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy, String limit) throws SQLException {
  146. Cursor mCursor = findList(distinct, tableName, columns, selection, selectionArgs, groupBy, having, orderBy, limit);
  147. if (mCursor != null) {
  148. mCursor.moveToFirst();
  149. }
  150. return mCursor;
  151. }
  152. /**
  153. * 執行SQL(帶參數)
  154. *
  155. * @param sql
  156. * @param args
  157. * SQL中“?”參數值
  158. * @author SHANHY
  159. */
  160. public void execSQL(String sql, Object[] args) {
  161. mDb.execSQL(sql, args);
  162. }
  163. /**
  164. * 執行SQL
  165. *
  166. * @param sql
  167. * @author SHANHY
  168. */
  169. public void execSQL(String sql) {
  170. mDb.execSQL(sql);
  171. }
  172. /**
  173. * 判斷某張表是否存在
  174. *
  175. * @param tabName
  176. * 表名
  177. * @return
  178. */
  179. public boolean isTableExist(String tableName) {
  180. boolean result = false;
  181. if (tableName == null) {
  182. return false;
  183. }
  184. try {
  185. Cursor cursor = null;
  186. String sql = "select count(1) as c from sqlite_master where type ='table' and name ='" + tableName.trim() + "'";
  187. cursor = mDb.rawQuery(sql, null);
  188. if (cursor.moveToNext()) {
  189. int count = cursor.getInt(0);
  190. if (count > 0) {
  191. result = true;
  192. }
  193. }
  194. cursor.close();
  195. } catch (Exception e) {
  196. }
  197. return result;
  198. }
  199. /**
  200. * 判斷某張表中是否存在某字段(注,該方法無法判斷表是否存在,因此應與isTableExist一起使用)
  201. *
  202. * @param tabName
  203. * 表名
  204. * @param columnName
  205. * 列名
  206. * @return
  207. */
  208. public boolean isColumnExist(String tableName, String columnName) {
  209. boolean result = false;
  210. if (tableName == null) {
  211. return false;
  212. }
  213. try {
  214. Cursor cursor = null;
  215. String sql = "select count(1) as c from sqlite_master where type ='table' and name ='" + tableName.trim() + "' and sql like '%" + columnName.trim() + "%'";
  216. cursor = mDb.rawQuery(sql, null);
  217. if (cursor.moveToNext()) {
  218. int count = cursor.getInt(0);
  219. if (count > 0) {
  220. result = true;
  221. }
  222. }
  223. cursor.close();
  224. } catch (Exception e) {
  225. }
  226. return result;
  227. }
  228. }
Copyright © Linux教程網 All Rights Reserved