简体   繁体   中英

Android: How to change value of sqlite column

I am relatively new to using sqlite in the android world and I currently I my sqlite database set up like the following:

db.execSQL("CREATE TABLES " + DATABASE_TABLE + " (" + KEY_ROWID
                    + " INTEGER PRIMARY KEY AUTOINCREMENT, " + KEY_LOCKERNUMBER
                    + " TEXT NOT NULL, " + KEY_STATUS + " INTEGER DEFAULT 0, "
                    + KEY_PIN + " INTEGER DEFAULT 0);");

And I have the following methods to insert data into the database:

// function to add lockers to database
    public long createLockerEntry(String lockerNumber, int status) {
        ContentValues cv = new ContentValues();
        cv.put(KEY_LOCKERNUMBER, lockerNumber);
        cv.put(KEY_STATUS, status);
        return ourDatabase.insert(DATABASE_TABLE, null, cv);
    }

    // function to create a pin for the locker
    public void createPin(String lockerNumber, int pin, int status) {

    }

    // function to remove pin
    public void removePin(String lockerNumber, int pin, int status) {

    }

My intentions are to add a pin number for a certain locker number to the KEY_PIN column within the method Create pin and then change the status value to 1. I would assume I would use the where clause statement but I am not completely sure about the syntax or if that is the write approach. Any suggestions?

The method you want is update()
You can read about it by searching for "update" on this page:

http://developer.android.com/reference/android/database/sqlite/SQLiteDatabase.html

One way to use this would be:

// function to create a pin for the locker
public void createPin(tring lockerNumber, int pin, int status) {
    ContentValues cv = new ContentValues()
    cv.put(KEY_PIN, pin);
    cv.put(KEY_STATUS, status);
    ourDatabase.update(DATABASE_TABLE, cv, KEY_LOCKERNUMBER + " = ?" 
                  , new String[] {lockerNumber});
}

Here's an example where I toggle the value of an entry. It is an int value used as binary (ie 1 or 0). The two methods checkAsWildTest, and uncheckAsWildTest.

public void checkAsWildTest(Cursor c, int val) {

    assertDbOpen();
    String[] _params = { String.valueOf(c.getInt(ID_COLUMN)) };
    //next 4 lines are just for debug
    log(".uncheckAsWildTest(), " + "c.getCount() = " + c.getCount());
    log("row selected = " + c.getInt(ID_COLUMN));
    log("cursor content: " + c.getString(ANSWERS_RESPONSE_COLUMN));
    log("cursor columns" + Arrays.toString(c.getColumnNames()));

    ContentValues cv = new ContentValues();
    cv.put(KEY_ANSWERS_CHECKED, val);
    db.update(ANSWERS_TABLE, cv, "_id=?", _params);
}

public void uncheckAsWildTest(Cursor c) {
    assertDbOpen();
    int unchecked = 0;
    String[] _params = { String.valueOf(c.getInt(ID_COLUMN)) };
    //next 4 lines are just for debug
    log(".uncheckAsWildTest(), " + "c.getCount() = " + c.getCount());
    log("column selected = " + c.getInt(ID_COLUMN));
    log("cursor content: " + c.getString(ANSWERS_RESPONSE_COLUMN));
    log("cursor columns" + Arrays.toString(c.getColumnNames()));

    ContentValues cv = new ContentValues();
    cv.put(KEY_ANSWERS_CHECKED, unchecked);
    db.update(ANSWERS_TABLE, cv, "_id=?", _params);
}

I believe you can work with this, and modify it for your purposes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM