我在android世界中使用sqlite比较新,我目前的sqlite数据库设置如下:
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);");
我有以下方法将数据插入数据库:
// 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) {
}
我的意图是在方法Create pin中的KEY_PIN列中添加一个特定储物柜号码的密码,然后将状态值更改为1.我会假设我会使用where子句语句,但我不完全确定关于语法或者如果是写入方法。有什么建议吗?
答案 0 :(得分:1)
您想要的方法是update()
您可以通过在此页面上搜索“更新”来阅读相关内容:
http://developer.android.com/reference/android/database/sqlite/SQLiteDatabase.html
使用它的一种方法是:
// 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});
}
答案 1 :(得分:0)
这是我切换条目值的示例。它是用作二进制的int值(即1或0)。 两种方法checkAsWildTest和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);
}
我相信您可以使用此功能,并根据您的目的进行修改。