importandroid.content.ContentValues;importandroid.content.Context;importcom.aax.exchange.entity.Favorites;importcom.tencent.wcdb.Cursor;importcom.tencent.wcdb.database.SQLiteDatabase;importjava.util.ArrayList;importjava.util.List;public classFavoritesDBManager {privateWcdbHelper mDBHelper;privateSQLiteDatabase mDB;private String name = "favorites.db";private String password = "xxx";publicFavoritesDBManager(Context context) {
mDBHelper= new WcdbHelper(context,name,password.getBytes(),null,1,null);
mDB=mDBHelper.getWritableDatabase();
addTable();
}public booleanaddFavoritesData(String tradingPair) {try{
mDB.beginTransaction();final String sql = "INSERT INTO favorites VALUES(NULL,?)";
Object[] objects= newObject[]{tradingPair};
mDB.execSQL(sql, objects);
mDB.setTransactionSuccessful();
}catch(Exception e){return false;
}finally{
mDB.endTransaction();
}return true;
}public booleanaddFavoritesData(Favorites favorites) {try{
mDB.beginTransaction();final String sql = "INSERT INTO favorites VALUES(NULL,?)";
Object[] objects= newObject[]{favorites.getTradingPair()};
mDB.execSQL(sql, objects);
mDB.setTransactionSuccessful();
}catch(Exception e){return false;
}finally{
mDB.endTransaction();
}return true;
}public boolean addFavoritesList(Listlist) {try{
mDB.beginTransaction();for(Favorites favorites : list) {
Object[] objects= newObject[]{favorites.getTradingPair()};final String sql = "INSERT INTO favorites VALUES(NULL,?)";
mDB.execSQL(sql, objects);
}
mDB.setTransactionSuccessful();
}catch(Exception e) {return false;
}finally{
mDB.endTransaction();
}return true;
}public booleandelFavoritesByTradingPair(String tradingPair) {try{
mDB.beginTransaction();
mDB.delete("favorites","tradingPair=?",newString[]{tradingPair});
mDB.setTransactionSuccessful();
}catch(Exception e) {return false;
}finally{
mDB.endTransaction();
}return true;
}publicFavorites getFavoritesByTradingPair(String tradingPair){
String sql= "select * from favorites where tradingPair=?";
Cursor cursor= mDB.rawQuery(sql, newString[]{tradingPair});while(cursor.moveToNext()){
Favorites favorites= newFavorites();
favorites.setTradingPair(cursor.getString(cursor.getColumnIndex("tradingPair")));returnfavorites;
}return null;
}public ListgetFavoritesListData() {
List listData = new ArrayList<>();
Cursor c=getAllFavoritesInfo();while(c.moveToNext()) {
Favorites favorites= newFavorites();
favorites.setTradingPair(c.getString(c.getColumnIndex("tradingPair")));
listData.add(favorites);
}
c.close();returnlistData;
}privateCursor getAllFavoritesInfo() {return mDB.rawQuery("SELECT * FROM favorites", null);
}public booleanupdateFavoritesByName(String tradingPair){
ContentValues values= newContentValues();
values.put("tradingPair", tradingPair);int flag = mDB.update("favorites", values, "tradingPair=?", newString[]{tradingPair});if (flag > 0) {return true;
}else{return false;
}
}public voidcloseDB() {
mDB.close();
}publicBoolean deleteDatabase(String name) {returnmDBHelper.onDelete(name);
}public voiddeleteFavoritesData() {
mDB.execSQL("DELETE FROM favorites;");
}public voidaddTable() {
String SQL_CREATE= "CREATE TABLE IF NOT EXISTS favorites (_id INTEGER PRIMARY KEY AUTOINCREMENT , tradingPair VARCHAR(50) )";
mDB.execSQL(SQL_CREATE);
}
}