我有一个问题因为我正在尝试编程的应用程序几天。 这很简单,当用户点击imageview时,数据库必须更新并将此数据设置为“收藏夹”。 这是代码:
主要活动
public void onResume(){
super.onResume();
Pref = getSharedPreferences("DoctorGames", MODE_PRIVATE);
pref = Pref.getString("pref", pref);
try { try {
Log.i("TEST0", String.valueOf(ID));
mDbHelper.open();
String query = "SELECT * FROM tgames WHERE _id = '" + ID + "'; ";
final Cursor cursor = mDbHelper.mDb.rawQuery(query, null);
cursor.moveToFirst();
String prefTEST = cursor.getString(cursor.getColumnIndexOrThrow("favorite"));
Log.i("TEST1", prefTEST + query);
DataHelper.updateFav(ID, pref, mDbHelper.mDb);
prefTEST = cursor.getString(cursor.getColumnIndexOrThrow("favorite"));
cursor.close();
mDbHelper.close();
Log.i("TEST2", pref +" " + prefTEST);
} catch ( CursorIndexOutOfBoundsException i){
Log.i("TEST", "Second");
}
}catch(NullPointerException e){
Log.i("TEST", "FIRST");
};
}
游戏活动
//Remove from favorite
favorite.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
pref = "co";
favorite.setVisibility(View.GONE);
co.setVisibility(View.VISIBLE);
mEditor.putString("pref", pref);
Log.i("TEST", pref);
}
});
//Add to favorite
co.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
pref = "favorite";
favorite.setVisibility(View.VISIBLE);
co.setVisibility(View.GONE);
mEditor.putString("pref", pref);
Log.i("TEST", pref);
}
});
DataHelper类
公共类DataHelper扩展了SQLiteOpenHelper {
public static final String TABLE_ROW_ID = "_id";
public static final String TABLE_ROW_NAME = "name";
public static final String TABLE_ROW_PLACE = "place";
public static final String TABLE_ROW_AGEMIN = "agemin";
public static final String TABLE_ROW_AGEMAX = "agemax";
public static final String TABLE_ROW_NBREMIN = "nbremin";
public static final String TABLE_ROW_NBREMAX = "nbremax";
public static final String TABLE_ROW_TYPE = "type";
public static final String TABLE_ROW_DESCRIPTION = "description";
public static final String TABLE_ROW_FAVORITE = "favorite";
public static final String TABLE_ROW_IMG = "img";
private static final String TABLE_GAMES = "tgames";
boolean exterieur = true;
boolean interieur = false;
boolean co = false;
boolean fav = true;
private static final int DB_VERSION = 1;
private static String TAG = "DataHelper";
private static String DB_PATH = "";
private static String DB_NAME = "games.db";
public static SQLiteDatabase mDB;
private final Context mContext;
File dbFile =new File(DB_PATH + DB_NAME);
public DataHelper(Context context){
super(context, DB_NAME, null, 1);
if (Build.VERSION.SDK_INT >= 17){
DB_PATH = context.getApplicationInfo().dataDir + "/databases/";
//DB_PATH = context.getFilesDir().toString();
}else{
DB_PATH="/data/data/" + context.getPackageName() + "/databases/";
}
this.mContext = context;
}
public void createDataBase() throws IOException
{
boolean mDataBaseExist = checkDataBase();
if(!mDataBaseExist){
this.getReadableDatabase();
}
this.close();
try{
copyDataBase();
Log.e(TAG, "createDatabase data base created");
}catch (IOException mIOException){
//throw new Error("ErroCopyingDatabase");
}
}
private boolean checkDataBase(){
File dbFile =new File(DB_PATH + DB_NAME);
return dbFile.exists();
}
private void copyDataBase() throws IOException{
InputStream mInput = mContext.getAssets().open(DB_NAME);
String outFileName = DB_PATH + DB_NAME;
OutputStream mOutPut = new FileOutputStream(outFileName);
byte[] mBuffer = new byte [1024];
int mLength;
while ((mLength = mInput.read(mBuffer))>0){
mOutPut.write(mBuffer, 0, mLength);
}
mOutPut.flush();
mOutPut.close();
mInput.close();
}
public boolean openDataBase() throws SQLiteException {
String mPath = DB_PATH + DB_NAME;
mDB = SQLiteDatabase.openDatabase(mPath, null, SQLiteDatabase.CREATE_IF_NECESSARY);
//mDB = SQLiteDatabase.openDatabase(mPath, null, SQLiteDatabase.NO_LOCALIZED_COLLATORS);
return mDB != null;
}
public Cursor selectAll(){
Cursor cAll = mDB.rawQuery("SELECT * from tgames", null);
return cAll;
}
public static Cursor search (String name, boolean place, int agemin, int agemax, int nbremin,
int nbremax, String type, String description, boolean favorite){
//A redefinir selon le critère
//peut etre une recherche par colonne, si élément est pas null? (mais les critères s'entrecroiseront pas alors)
String query = "SELECT " +
TABLE_ROW_ID +", " +
TABLE_ROW_NAME + ", " +
TABLE_ROW_PLACE + ", " +
TABLE_ROW_AGEMIN + ", " +
TABLE_ROW_AGEMAX + ", " +
TABLE_ROW_NBREMIN + ", " +
TABLE_ROW_NBREMAX + ", " +
TABLE_ROW_TYPE + ", " +
TABLE_ROW_DESCRIPTION + ", " +
TABLE_ROW_FAVORITE + ", " +
TABLE_ROW_IMG +
" from " +
TABLE_GAMES + " WHERE " +
TABLE_ROW_NAME + " = '" + name + "' or " +
TABLE_ROW_PLACE + " = '" + place + "' or " +
TABLE_ROW_AGEMIN + " = '" + agemin + "' or " +
TABLE_ROW_AGEMAX + " = '" + agemax + "' or " +
TABLE_ROW_NBREMIN + " = '" + nbremin + "' or " +
TABLE_ROW_NBREMAX + " = '" + nbremax + "' or " +
TABLE_ROW_TYPE + " = '" + type + "' or " +
TABLE_ROW_DESCRIPTION + " = '" + description + "' or " +
TABLE_ROW_FAVORITE + " = '" + favorite + "';";
Cursor c = mDB.rawQuery(query, null);
return c;
}
public static Cursor searchTEST(String name, String place, String agemin, String agemax, String nbremin,
String nbremax, String type, String favorite) {
Cursor c = mDB.query(TABLE_GAMES, new String[]{
TABLE_ROW_NAME,
TABLE_ROW_PLACE,
TABLE_ROW_AGEMIN,
TABLE_ROW_AGEMAX,
TABLE_ROW_NBREMIN,
TABLE_ROW_NBREMAX,
TABLE_ROW_TYPE,
TABLE_ROW_DESCRIPTION,
TABLE_ROW_FAVORITE},
TABLE_ROW_NAME + " = ? AND " +
TABLE_ROW_PLACE + " = ? AND " +
TABLE_ROW_AGEMIN + " = ? AND " +
TABLE_ROW_AGEMAX + " = ? AND " +
TABLE_ROW_NBREMIN + " = ? AND " +
TABLE_ROW_NBREMAX + " = ? AND " +
TABLE_ROW_TYPE + " = ? AND " +
TABLE_ROW_FAVORITE + " = ?",
new String[]{name, place, agemin, agemax, nbremin, nbremax, type, favorite},
null,
null,
null);
return c;
}
//MTHODE TEST RECHERCHE
public static Cursor searchNAME(String name){
String query = "SELECT " +
TABLE_ROW_NAME + ", " +
TABLE_ROW_PLACE + ", " +
TABLE_ROW_AGEMIN + ", " +
TABLE_ROW_AGEMAX + ", " +
TABLE_ROW_NBREMIN + ", " +
TABLE_ROW_NBREMAX + ", " +
TABLE_ROW_TYPE + ", " +
TABLE_ROW_DESCRIPTION + ", " +
TABLE_ROW_FAVORITE +
" from " +
TABLE_GAMES + " WHERE " +
TABLE_ROW_NAME + " = '" + name + "';";
Cursor c = mDB.rawQuery(query, null);
return c;
}
//Update FAVORI
public static void updateFav(int id, String fav, SQLiteDatabase DB){
ContentValues values = new ContentValues();
values.put("favorite", fav);
String selection = "_id" +" = '" + id + "'";
//String[]selectionArgs = {String.valueOf(id)};
DB.update("tgames",
values,
selection,
null);
Log.i("TEST55", values + selection );
}
@Override
public void onCreate(SQLiteDatabase mDB) {
}
@Override
public void onUpgrade(SQLiteDatabase mDB, int oldVersion, int newVersion) {
}
}
任何帮助将不胜感激:)
答案 0 :(得分:0)
您的UPDATE功能不在事务中,因此更改将在db.close()
回滚。试试这个......
SQLiteDatabase db = this.getWritableDatabase();
db.beginTransaction()
...insert, update, delete, etc
if (ok) {
db.setTransactionSucessful();
}
db.endTransaction();
db.close();
答案 1 :(得分:-1)
您的更新方法就像
public static void updateFav
虽然您将此方法设为静态,但您的databasehelper类在方法为静态时保持对object的引用。
请像这样做
public void updateFav(args..){}
并将其用作
mhelper.updateFav(args);
它应该工作。希望它有所帮助。