实现思路:
1)创建实体类/数据库模型.
2)封装数据操作
具体实现:
1)创建模型:
public class BaseBean implements Serializable { ...... //具体属性根据业务确定}
2)创建数据库(DBHelper) + 数据库管理类(DBManager) + 数据库操作工具类(DBTools)
/** * Created by milo on 15/7/28. * 数据库辅助类 */public class DBHelper extends SQLiteOpenHelper { private static final String DATABASE_NAME = "PictureAirGuide.db"; private static final int DATABASE_VERSION = 1; private static DBHelper dbHelper; public synchronized static DBHelper getInstance(Context context) { if (dbHelper == null) { LogUtil.i("DBHelper", "getInstance()" + "dbHelper == null"); dbHelper = new DBHelper(context); } return dbHelper; } public DBHelper(Context context) { super(context, DATABASE_NAME, null, DATABASE_VERSION); LogUtil.i("DBHelper", "DBHelper()"); } @Override public void onOpen(SQLiteDatabase db) { super.onOpen(db); // TODO 每次成功打开数据库后首先被执行 LogUtil.i("DBHelper", "onOpen()"); } @Override public void onCreate(SQLiteDatabase db) { db.beginTransaction(); try { LogUtil.i("DBHelper", "onCreate()"); //创建数据表 db.execSQL("....."); db.setTransactionSuccessful(); } catch (Exception e) { e.printStackTrace(); } finally { db.endTransaction(); } } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { //db.execSQL("ALTER TABLE picture ADD COLUMN uploadTime TEXT"); LogUtil.i("DBHelper", "onUpgrade()"); }}
/** * 线程安全并发操作数据库 * * @author milo */public class DBManager { private static AtomicInteger mOpenCounter = new AtomicInteger(); private static DBManager dbManager; private static DBHelper dbHelper; private static SQLiteDatabase db; public static synchronized void initializeInstance(DBHelper helper) { if (dbManager == null) { dbManager = new DBManager(); dbHelper = helper; } } /** * 获取DB * * @return */ public static synchronized DBManager getInstance() { if (dbManager == null) { throw new IllegalStateException(DBManager.class.getSimpleName() + " is not initialized, call initializeInstance(..) method first."); } return dbManager; } /** * 写入数据DB * * @return */ public synchronized SQLiteDatabase writData() { if (mOpenCounter.incrementAndGet() == 1) { // Opening new database db = dbHelper.getWritableDatabase(); } return db; } /** * 读取数据DB * * @return */ public synchronized SQLiteDatabase readData() { if (mOpenCounter.incrementAndGet() == 1) { // Opening new database //db = dbHelper.getReadableDatabase(); //API原文:This will be the same object returned by getWritableDatabase() unless some problem, such as a full disk, requires the database to be opened read-only. db = dbHelper.getWritableDatabase(); } return db; } /** * 关闭DB */ public synchronized void closeDatabase() { if (mOpenCounter.decrementAndGet() == 0) { // Closing database db.close(); } } /** * 删除DB */ public synchronized void deleteDatabase(String DBName) { if (db != null) { // delete database db.delete(DBName, null, null); } } /** * 删除所有DB */ public synchronized void deleteAllDatabase() { if (db != null) { // delete all database db.delete("", null, null); } }}
/** * Created by milo on 15/8/19. * DB增删改查工具类 */public class DBTools { /** * 添加数据 * replace 不存在添加、存在更新 * * @param table 表名 * @param contentValues 值 */ public synchronized static void insert(String table, ContentValues contentValues) { if (contentValues != null) { SQLiteDatabase db = DBManager.getInstance().writData(); //使用事务提交 db.beginTransaction(); try { DBManager.getInstance().writData().replace(table, null, contentValues); db.setTransactionSuccessful(); } catch (Exception e) { e.printStackTrace(); } finally { db.endTransaction(); } DBManager.getInstance().closeDatabase();//关闭DB } } /** * 添加数据 * * @param table 表名 * @param contentValuesList 值 */ public synchronized static void insert(String table, ListcontentValuesList) { if (contentValuesList != null && contentValuesList.size() > 0) { SQLiteDatabase db = DBManager.getInstance().writData(); //使用事务提交 db.beginTransaction(); try { for (ContentValues contentValues : contentValuesList) { DBManager.getInstance().writData().replace(table, null, contentValues); } db.setTransactionSuccessful(); } catch (Exception e) { e.printStackTrace(); } finally { db.endTransaction(); } DBManager.getInstance().closeDatabase();//关闭DB } } /** * 修改数据 * * @param table 表名 * @param values 修改后的值 * @param whereClause 修改条件 * @param whereArgs 修改的参数值 */ public synchronized static void update(String table, ContentValues values, String whereClause, String[] whereArgs) { DBManager.getInstance().writData().update(table, values, whereClause, whereArgs); DBManager.getInstance().closeDatabase();//关闭DB } /** * 查询数据 * * @param table 表名 * @param columns 要查询出来的列名,为 null 查询所有 * @param selection 查询条件,允许使用占位符"?" * @param selectionArgs 对应于selection语句中占位符的值 * @param groupBy 分组 * @param having 过滤器 * @param orderBy 排序 * @param limit 限制查询返回的行数, * @return Cursor 注意使用完要关闭 */ public static Cursor query(String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy, String limit) { Cursor c = DBManager.getInstance().readData().query(table, columns, selection, selectionArgs, groupBy, having, orderBy, limit);// DBManager.getInstance().closeDatabase();//关闭DB //游标必须要在关闭数据库之前关闭,不能放在后面 return c; }}
3)在实体类中对具体数据进行增删改查: