鑒於經常使用 Sqlite 數據庫做數據持久化處理,進行了一點封裝,方便使用。
該封裝類主要支持一下功能
- 支持多用戶數據儲存
- 支持 Sqlite數據庫升級
- 支持傳入 Sql 語句建表
- 支持 SQLiteDatabase 基本操作。比如:execSQL、rawQuery、insert等等
- 解決了數據庫並發問題
先貼上封裝類代碼
/**
*
* @ClassName: DataBaseOpenHelper
* @Description: 數據庫工具類
* @author lhy
* @date 2014-10-9 下午2:36:41
*
*/
public class DataBaseOpenHelper extends SQLiteOpenHelper {
private static Map<String, DataBaseOpenHelper> dbMaps = new HashMap<String, DataBaseOpenHelper>();
private OnSqliteUpdateListener onSqliteUpdateListener;
/**
* 建表語句列表
*/
private List<String> createTableList;
private String nowDbName;
private DataBaseOpenHelper(Context context, String dbName, int dbVersion, List<String> tableSqls) {
super(context, dbName, null, dbVersion);
nowDbName = dbName;
createTableList = new ArrayList<String>();
createTableList.addAll(tableSqls);
}
/**
*
* @Title: getInstance
* @Description: 獲取數據庫實例
* @param @param context
* @param @param userId
* @param @return
* @return DataBaseOpenHelper
* @author lihy
*/
public static DataBaseOpenHelper getInstance(Context context, String dbName, int dbVersion, List<String> tableSqls) {
DataBaseOpenHelper dataBaseOpenHelper = dbMaps.get(dbName);
if (dataBaseOpenHelper == null) {
dataBaseOpenHelper = new DataBaseOpenHelper(context, dbName, dbVersion, tableSqls);
}
dbMaps.put(dbName, dataBaseOpenHelper);
return dataBaseOpenHelper;
};
@Override
public void onCreate(SQLiteDatabase db) {
for (String sqlString : createTableList) {
db.execSQL(sqlString);
}
}
/**
*
* @Title: execSQL
* @Description: Sql寫入
* @param @param sql
* @param @param bindArgs
* @return void
* @author lihy
*/
public void execSQL(String sql, Object[] bindArgs) {
DataBaseOpenHelper dataBaseOpenHelper = dbMaps.get(nowDbName);
synchronized (dataBaseOpenHelper) {
SQLiteDatabase database = dataBaseOpenHelper.getWritableDatabase();
database.execSQL(sql, bindArgs);
}
}
/**
*
* @Title: rawQuery
* @Description:
* @param @param sql查詢
* @param @param bindArgs
* @param @return
* @return Cursor
* @author lihy
*/
public Cursor rawQuery(String sql, String[] bindArgs) {
DataBaseOpenHelper dataBaseOpenHelper = dbMaps.get(nowDbName);
synchronized (dataBaseOpenHelper) {
SQLiteDatabase database = dataBaseOpenHelper.getReadableDatabase();
Cursor cursor = database.rawQuery(sql, bindArgs);
return cursor;
}
}
/**
*
* @Title: insert
* @Description: 插入數據
* @param @param table
* @param @param contentValues 設定文件
* @return void 返回類型
* @author lihy
* @throws
*/
public void insert(String table, ContentValues contentValues) {
DataBaseOpenHelper dataBaseOpenHelper = dbMaps.get(nowDbName);
synchronized (dataBaseOpenHelper) {
SQLiteDatabase database = dataBaseOpenHelper.getWritableDatabase();
database.insert(table, null, contentValues);
}
}
/**
*
* @Title: update
* @Description: 更新
* @param @param table
* @param @param values
* @param @param whereClause
* @param @param whereArgs 設定文件
* @return void 返回類型
* @throws
*/
public void update(String table, ContentValues values, String whereClause, String[] whereArgs) {
DataBaseOpenHelper dataBaseOpenHelper = dbMaps.get(nowDbName);
synchronized (dataBaseOpenHelper) {
SQLiteDatabase database = dataBaseOpenHelper.getWritableDatabase();
database.update(table, values, whereClause, whereArgs);
}
}
/**
*
* @Title: delete
* @Description:刪除
* @param @param table
* @param @param whereClause
* @param @param whereArgs
* @return void
* @author lihy
*/
public void delete(String table, String whereClause, String[] whereArgs) {
DataBaseOpenHelper dataBaseOpenHelper = dbMaps.get(nowDbName);
synchronized (dataBaseOpenHelper) {
SQLiteDatabase database = dataBaseOpenHelper.getWritableDatabase();
database.delete(table, whereClause, whereArgs);
}
}
/**
*
* @Title: query
* @Description: 查
* @param @param table
* @param @param columns
* @param @param selection
* @param @param selectionArgs
* @param @param groupBy
* @param @param having
* @param @param orderBy
* @return void
* @author lihy
*/
public Cursor query(String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having,
String orderBy) {
DataBaseOpenHelper dataBaseOpenHelper = dbMaps.get(nowDbName);
synchronized (dataBaseOpenHelper) {
SQLiteDatabase database = dataBaseOpenHelper.getReadableDatabase();
// Cursor cursor = database.rawQuery("select * from "
// + TableName.TABLE_NAME_USER + " where userId =" + userId, null);
Cursor cursor = database.query(table, columns, selection, selectionArgs, groupBy, having, orderBy);
return cursor;
}
}
/**
*
* @Description:查
* @param table
* @param columns
* @param selection
* @param selectionArgs
* @param groupBy
* @param having
* @param orderBy
* @param limit
* @return
* Cursor
* @exception:
* @author: lihy
* @time:2015-4-3 上午9:37:29
*/
public Cursor query(String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having,
String orderBy,String limit) {
DataBaseOpenHelper dataBaseOpenHelper = dbMaps.get(nowDbName);
synchronized (dataBaseOpenHelper) {
SQLiteDatabase database = dataBaseOpenHelper.getReadableDatabase();
// Cursor cursor = database.rawQuery("select * from "
// + TableName.TABLE_NAME_USER + " where userId =" + userId, null);
Cursor cursor = database.query(table, columns, selection, selectionArgs, groupBy, having, orderBy, limit);
return cursor;
}
}
/**
*
* @Description 查詢,方法重載,table表名,sqlString條件
* @param @return
* @return Cursor
* @author lihy
*/
public Cursor query(String tableName, String sqlString) {
DataBaseOpenHelper dataBaseOpenHelper = dbMaps.get(nowDbName);
synchronized (dataBaseOpenHelper) {
SQLiteDatabase database = dataBaseOpenHelper.getReadableDatabase();
Cursor cursor = database.rawQuery("select * from " + tableName + " " + sqlString, null);
return cursor;
}
}
/**
* @see android.database.sqlite.SQLiteOpenHelper#close()
*/
public void clear() {
DataBaseOpenHelper dataBaseOpenHelper = dbMaps.get(nowDbName);
dataBaseOpenHelper.close();
dbMaps.remove(dataBaseOpenHelper);
}
/**
* onUpgrade()方法在數據庫版本每次發生變化時都會把用戶手機上的數據庫表刪除,然后再重新創建。<br/>
* 一般在實際項目中是不能這樣做的,正確的做法是在更新數據庫表結構時,還要考慮用戶存放於數據庫中的數據不會丟失,從版本幾更新到版本幾。(非
* Javadoc)
*
* @see android.database.sqlite.SQLiteOpenHelper#onUpgrade(android.database.sqlite
* .SQLiteDatabase, int, int)
*/
@Override
public void onUpgrade(SQLiteDatabase db, int arg1, int arg2) {
if (onSqliteUpdateListener != null) {
onSqliteUpdateListener.onSqliteUpdateListener(db, arg1, arg2);
}
}
public void setOnSqliteUpdateListener(OnSqliteUpdateListener onSqliteUpdateListener) {
this.onSqliteUpdateListener = onSqliteUpdateListener;
}
}
數據庫更新接口代碼
public interface OnSqliteUpdateListener {
public void onSqliteUpdateListener(SQLiteDatabase db, int oldVersion, int newVersion);
}
