我已经有好几个星期处理这个问题了,我无法找到解决方案。这里是 : 当用户点击imageview时,我想更新数据库以将游戏添加到“收藏夹”。但我不知道为什么,它不会这样做。 (我使用用DB浏览器创建的数据库)。 这是我的代码:
更新代码
favorite.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
pref = "co";
favorite.setVisibility(View.GONE);
co.setVisibility(View.VISIBLE);
mDBHelper = new DataHelper(getApplication());
mDBHelper.openDataBase();
SQLiteDatabase mDB = mDBHelper.getWritableDatabase();
//mDB.open();
mDB.beginTransaction();
ContentValues values = new ContentValues();
values.put("pref", pref);
//String selection = "_id" +" = '" + ID + "'";
String[]selectionArgs = new String[1];
selectionArgs[0] = "" + ID;
mDB.update("tgames",
values,
"_id= ?",
selectionArgs);
mDB.setTransactionSuccessful();
mDB.endTransaction();
mDB.close();
DATA助手代码
public class DataHelper extends 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_PREF = "pref";
public static final String TABLE_ROW_IMG = "img";
private static final String TABLE_GAMES = "tgames";
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, DB_VERSION);
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 static Cursor selectAll(){
Cursor cAll = mDB.rawQuery("SELECT * from tgames", null);
return cAll;
}
public static Cursor searchSIMPLE (String name, String place, int agemin, int agemax, int nbre, String type, String 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 * from " +
TABLE_GAMES + " WHERE " +
TABLE_ROW_AGEMIN + " <= '" + agemin + "' AND " +
TABLE_ROW_AGEMAX + " >= '" + agemax + "' AND " +
TABLE_ROW_NBREMIN + " <= '" + nbre + "' AND " +
TABLE_ROW_NBREMAX + " >= '" + nbre + "' AND " +
TABLE_ROW_PLACE + " = '" + place + "';";
Cursor c = mDB.rawQuery(query, null);
return c;
}
public static Cursor searchADV (String name, String place, int agemin, int agemax, int nbre, String type, String favorite){
String query = "SELECT * from " +
TABLE_GAMES + " WHERE " +
TABLE_ROW_AGEMIN + " <= '" + agemin + "' AND " +
TABLE_ROW_AGEMAX + " >= '" + agemax + "' AND " +
TABLE_ROW_NBREMIN + " <= '" + nbre + "' AND " +
TABLE_ROW_NBREMAX + " >= '" + nbre + "' AND " +
TABLE_ROW_PLACE + " = '" + place + "' AND " +
/*TABLE_ROW_PREF + " = '" + favorite + "'AND " +*/
TABLE_ROW_TYPE + " = '" + type + "' ;";
Cursor c = mDB.rawQuery(query, null);
return c;
}
public static Cursor searchADVName (String name, String place, int agemin, int agemax, int nbre, String type, String favorite){
String query = "SELECT * from " +
TABLE_GAMES + " WHERE " +
TABLE_ROW_AGEMIN + " <= '" + agemin + "' AND " +
TABLE_ROW_AGEMAX + " >= '" + agemax + "' AND " +
TABLE_ROW_NBREMIN + " <= '" + nbre + "' AND " +
TABLE_ROW_NBREMAX + " >= '" + nbre + "' AND " +
TABLE_ROW_PLACE + " = '" + place + "' AND " +
TABLE_ROW_TYPE + " = '" + type + "' AND " +
/*TABLE_ROW_PREF + " = '" + favorite + "'AND " +*/
TABLE_ROW_NAME + " LIKE '%" + name + "%' ; ";
Cursor c = mDB.rawQuery(query, null);
return c;
}
public static Cursor randomMachine(){
int cnt = getCount(selectAll());
Random r = new Random();
int r1 = r.nextInt(cnt)+1;
String r2 = String.valueOf(r1);
String sql ="SELECT * FROM tgames WHERE _id = '" + r2 + "';" ;
Log.i("TEST", sql);
Cursor mCur = mDB.rawQuery(sql, null);
return mCur;
}
public static Cursor getType(String type)
{
String sql ="SELECT * FROM tgames WHERE type = '" +type + "';";
Cursor mCur = mDB.rawQuery(sql, null);
return mCur;
}
private static int getCount(Cursor c){
int cnt = c.getCount();
return cnt;
}
@Override
public void onCreate(SQLiteDatabase mDB) {
}
@Override
public void onUpgrade(SQLiteDatabase mDB, int oldVersion, int newVersion) {
Log.v("TEST", "Upgrade 1");
mDB.execSQL("DROP TABLE IF EXISTS " + "tgames");
try {
createDataBase();
}catch (Exception e){
Log.v("TEST", "Upgrade FAILED");
}
}
}
感谢您的帮助。
编辑: 我忘了,我在主要活动的儿童活动中做这个操作。当我关闭它并重新打开它时,初始状态(最喜欢与否)没有改变。 。
答案 0 :(得分:0)
感谢所有试图帮助的人! 我想我找到了答案: 这是数据库创建方法的错误,每次打开新活动时都会重新创建。 所以我改变了:
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");
}
}
到此:
public void createDataBase() throws IOException
{
boolean mDataBaseExist = checkDataBase();
if(!mDataBaseExist) {
this.getWritableDatabase();
this.close();
try {
copyDataBase();
Log.e(TAG, "createDatabase data base created");
} catch (IOException mIOException) {
//throw new Error("ErroCopyingDatabase");
}
}
}
谢谢大家,继续做好工作!
答案 1 :(得分:-1)
你可以阅读我的代码,将新记录插入到sqlite DB下面: 注意:我只编写短代码,以帮助您了解如何使用某些字段和值向DB插入新数据。
public class DataHelper extends SQLiteOpenHelper {
public void insertData(String name, String img){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(TABLE_ROW_NAME, name);
contentValues.put(TABLE_ROW_IMG, img);
//add more value here ......
db.insert("tgames", null, contentValues);
}
}
所以你可以随时调用insertData函数添加到farvorite