Android Java SQLite rowid 基于另一列中的数据
Android Java SQLite rowid based on data in another column
解决方案已被选中,但还有另一个有效。我会在下面添加
我整天都在做这个。在问题的另一方面得到了帮助,但我又被难住了。如何获取列中包含特定数据的行的 rowid?我需要 rowid returned 作为 int 或字符串。示例数据:
_id|firstname|lastname
1 |Bob |Dole
2 |Richard |Simmons
3 |Steven |King
那么,如果我想得到rowid if lastname = "Simmons",我应该用什么代码呢?在 select 语句是 运行 之后,我认为 c.getPosition() 会这样做,但那是 returning -1。到目前为止,这是我得到的:
String where = "SELECT rowid, * FROM masterRecord WHERE masNameCol='" + name + "'"; //name is a variable passed from another activity
Cursor c = db.rawQuery(where, null);
String rowid = "_id = " + c.getPosition(); //This always returns -1. In this example, I need it to be 2
ContentValues newValues = new ContentValues();
newValues.put(KEY_MASNAMECOL, rowid);
newValues.put(KEY_MASTIMECOL, newTime);
c.close();
return db.update(masterRecord, newValues, rowid, null) != 0;
请注意,如果我将 return 语句中的 rowid 替换为“_id=2”,它将按预期工作。所以我把它缩小到只需要以某种方式得到那个数字。
其他解决方案
String where = "SELECT rowid, * FROM " + masterName + " WHERE masNameCol='" + name + "'";
int rowid = 0;
Cursor c = db.rawQuery(where, null);
c.moveToFirst(); //Needed this line
if (c.getCount() > 0) { //And this if statement
rowid = c.getInt(c.getColumnIndex("_id"));
}
ContentValues newValues = new ContentValues();
newValues.put(KEY_MASNAMECOL, rowid);
newValues.put(KEY_MASTIMECOL, newTime);
c.close();
return db.update(masterName, newValues, "_id =" + rowid, null) != 0;
您可以更新 table 而无需先获取其 rowid:
String where = "masNameCol = ?";
ContentValues newValues = new ContentValues();
newValues.put(KEY_MASNAMECOL, "somevaluehere"); // what value do you want this column to be updated?
newValues.put(KEY_MASTIMECOL, newTime);
return db.update(masterRecord, newValues, where, new String[] {name}) != 0;
请注意,如果列 masNameCol
不是唯一的,这将使用变量 name
的值更新所有行。
解决方案已被选中,但还有另一个有效。我会在下面添加
我整天都在做这个。在问题的另一方面得到了帮助,但我又被难住了。如何获取列中包含特定数据的行的 rowid?我需要 rowid returned 作为 int 或字符串。示例数据:
_id|firstname|lastname
1 |Bob |Dole
2 |Richard |Simmons
3 |Steven |King
那么,如果我想得到rowid if lastname = "Simmons",我应该用什么代码呢?在 select 语句是 运行 之后,我认为 c.getPosition() 会这样做,但那是 returning -1。到目前为止,这是我得到的:
String where = "SELECT rowid, * FROM masterRecord WHERE masNameCol='" + name + "'"; //name is a variable passed from another activity
Cursor c = db.rawQuery(where, null);
String rowid = "_id = " + c.getPosition(); //This always returns -1. In this example, I need it to be 2
ContentValues newValues = new ContentValues();
newValues.put(KEY_MASNAMECOL, rowid);
newValues.put(KEY_MASTIMECOL, newTime);
c.close();
return db.update(masterRecord, newValues, rowid, null) != 0;
请注意,如果我将 return 语句中的 rowid 替换为“_id=2”,它将按预期工作。所以我把它缩小到只需要以某种方式得到那个数字。
其他解决方案
String where = "SELECT rowid, * FROM " + masterName + " WHERE masNameCol='" + name + "'";
int rowid = 0;
Cursor c = db.rawQuery(where, null);
c.moveToFirst(); //Needed this line
if (c.getCount() > 0) { //And this if statement
rowid = c.getInt(c.getColumnIndex("_id"));
}
ContentValues newValues = new ContentValues();
newValues.put(KEY_MASNAMECOL, rowid);
newValues.put(KEY_MASTIMECOL, newTime);
c.close();
return db.update(masterName, newValues, "_id =" + rowid, null) != 0;
您可以更新 table 而无需先获取其 rowid:
String where = "masNameCol = ?";
ContentValues newValues = new ContentValues();
newValues.put(KEY_MASNAMECOL, "somevaluehere"); // what value do you want this column to be updated?
newValues.put(KEY_MASTIMECOL, newTime);
return db.update(masterRecord, newValues, where, new String[] {name}) != 0;
请注意,如果列 masNameCol
不是唯一的,这将使用变量 name
的值更新所有行。