在sqlite数据库android中增加一个字段

时间:2015-01-07 10:08:38

标签: java android sqlite

我有一个带有表格的数据库,代表我的游戏。问题是,当我使用该方法购买时,它不会将值增加1,而只是将其设置为零。那么,每当它调用特定ID时,如何改进将ItemNumberOfPurchases的值递增1的方法呢?这是我的代码。

public class MonsterTapDatabase extends SQLiteOpenHelper {
static final String dbName = "MonsterTapDb";
static final int version = 1;
static final String tTableName = "Shop";
static final String fItemID = "ItemID";
static final String fItemName = "ItemName";
static final String fItemNumberOfPurchases = "NumberOfPurchases";
static final String fItemIsLocked = "IsItemLocked";


public MonsterTapDatabase(Context context) {
    super(context, dbName, null, version);
}

@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
    sqLiteDatabase.execSQL("CREATE TABLE IF NOT EXISTS "+tTableName+" (" +
            fItemID + " INTEGER PRIMARY KEY , " +
            fItemName + " TEXT , " +
            fItemNumberOfPurchases + " INT, " +
            fItemIsLocked + " TEXT" +
            ")");
    ContentValues cv = new ContentValues();
    cv.put(fItemID, 1);
    cv.put(fItemName, "Lives");
    cv.put(fItemNumberOfPurchases, 0);
    cv.put(fItemIsLocked, "true");
    sqLiteDatabase.insert(tTableName, null, cv);
    cv.put(fItemID, 2);
    cv.put(fItemName, "Hardmode");
    cv.put(fItemNumberOfPurchases, 0);
    cv.put(fItemIsLocked, "true");
    sqLiteDatabase.insert(tTableName, null, cv);
    cv.put(fItemID, 3);
    cv.put(fItemName, "Reversed");
    cv.put(fItemNumberOfPurchases, 0);
    cv.put(fItemIsLocked, "true");
    sqLiteDatabase.insert(tTableName, null, cv);
    cv.put(fItemID, 4);
    cv.put(fItemName, "Reversed Hardmode");
    cv.put(fItemNumberOfPurchases, 0);
    cv.put(fItemIsLocked, "true");
    sqLiteDatabase.insert(tTableName, null, cv);
}

@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
    sqLiteDatabase.execSQL("DROP TABLE IF EXISTS " + tTableName);
    onCreate(sqLiteDatabase);
}
public void purchase(int ID){

    SQLiteDatabase myDB = this.getWritableDatabase();
    myDB.execSQL("UPDATE " + tTableName+
            " SET "+fItemNumberOfPurchases+"=IFNULL("+fItemNumberOfPurchases+",0)+1"+
            " WHERE "+fItemID+"="+ID);
    myDB.close();

}
public void lockItem(int ID){
    SQLiteDatabase myDB = this.getWritableDatabase();
    ContentValues cv = new ContentValues();
    cv.put(fItemIsLocked, false);
    myDB.update(tTableName, cv, fItemID+"=?", new String []{String.valueOf(ID)});
}
public String isPurchased(int ID){
    SQLiteDatabase myDB = this.getReadableDatabase();
    String[] mySearch = new String[]{String.valueOf(ID)};
    Cursor myCursor = myDB.rawQuery("SELECT "+ fItemIsLocked +" FROM "+ tTableName +" WHERE "+ fItemID +"=?",mySearch);
    myCursor.moveToFirst();
    int index = myCursor.getColumnIndex(fItemID);
    String myAnswer = myCursor.getString(index);
    myCursor.close();
    return myAnswer;
}
public int numberOfLives(){
    int ID = 1;
    SQLiteDatabase myDB = this.getReadableDatabase();
    String[] mySearch = new String[]{String.valueOf(ID)};
    Cursor myCursor = myDB.rawQuery("SELECT "+ fItemNumberOfPurchases +" FROM "+ tTableName +" WHERE "+ fItemID +"=?",mySearch);
    myCursor.moveToFirst();
    int index = myCursor.getColumnIndex(fItemNumberOfPurchases);
    myCursor.close();
    return index;
}

}

1 个答案:

答案 0 :(得分:0)

  1. 您使用fItemNumberOfPurchases代替fItemID;
  2. 为什么要将ID与不平等进行比较?
  3. 如果fItemNumberOfPurchasesNULL,则数字算术将始终返回NULL,可以使用IFNULL处理。
  4. 修正:

    SQLiteDatabase myDB = this.getWritableDatabase();
    myDB.execSQL("UPDATE " + tTableName+
        " SET "+fItemNumberOfPurchases+"=IFNULL("+fItemNumberOfPurchases+",0)+1"+
        " WHERE "+fItemID+"="+ID);
    myDB.close();