- 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.
-
-
-
-
-
- 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.
-
-
-
-
- 57. public void closeConnection() {
- 58. if (mDb != null && mDb.isOpen())
- 59. mDb.close();
- 60. if (mDbHelper != null)
- 61. mDbHelper.close();
- 62. }
- 63.
- 64.
-
-
-
-
-
-
-
-
-
- 74. public long insert(String tableName, ContentValues initialValues) {
- 75.
- 76. return mDb.insert(tableName, null, initialValues);
- 77. }
- 78.
- 79.
-
-
-
-
-
-
-
-
-
-
-
- 91. public boolean delete(String tableName, String deleteCondition, String[] deleteArgs) {
- 92.
- 93. return mDb.delete(tableName, deleteCondition, deleteArgs) > 0;
- 94. }
- 95.
- 96.
-
-
-
-
-
-
-
-
-
-
-
-
-
- 110. public boolean update(String tableName, ContentValues initialValues, String selection, String[] selectArgs) {
- 111. return mDb.update(tableName, initialValues, selection, selectArgs) > 0;
- 112. }
- 113.
- 114.
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- 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.
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- 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.
-
-
-
-
-
-
-
- 185. public void execSQL(String sql, Object[] args) {
- 186. mDb.execSQL(sql, args);
- 187.
- 188. }
- 189.
- 190.
-
-
-
-
-
- 196. public void execSQL(String sql) {
- 197. mDb.execSQL(sql);
- 198.
- 199. }
- 200.
- 201.
-
-
-
-
-
-
- 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.
-
-
-
-
-
-
-
-
- 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.}
-