我有一张桌子,我正在存储纬度和经度。我的表架构如下
TestId | Latitude | Longitude | TestName |
其中testid是INTEGER PRIMARY KEY AUTOINCREMENT。
现在,如果表存在相同的纬度和经度,则应更新之前的raw。我正在做下面的事。
将所有值添加到内容
ContentValues values = new ContentValues();
values.put(GLNetTestDataContract.TestPinRecord.COLUMN_NAME_TESTNAME, testName);
values.put(GLNetTestDataContract.TestPinRecord.COLUMN_NAME_LATTITUDE, lattitude);
values.put(GLNetTestDataContract.TestPinRecord.COLUMN_NAME_LATTITUDE, lattitude);
/ * 检查表中是否存在纬度和经度值,然后更新测试 /
String selectionCheckLatitudeAndLongitude = Gaurav.TestRecord.COLUMN_NAME_LATTITUDE + "=? AND " +
Gaurav.TestRecord.COLUMN_NAME_LATTITUDE + "=?";
String[] selectionArgslCheckLatitudeAndLongitude = { String.valueOf(lattitude),String.valueOf(longitude)};
try{
newRowId = mDatabase.update(GLNetTestDataContract.TestPinRecord.TABLE_NAME, values, selectionCheckLatitudeAndLongitude, selectionArgslCheckLatitudeAndLongitude);
}
catch(Exception e)
{
mLogger.printLog("pin", "Exception while updating");
mLogger.printLog("pin", e.getMessage());
}
if(newRowId>0)
mLogger.printLog("pin","Test updated with existing geo location");
// Insert the new row, returning the primary key value of the new row
else
{
newRowId = mDatabase.insert(GLNetTestDataContract.TestPinRecord.TABLE_NAME,null, values);
mLogger.printLog("pin","Test inserted with existing geo location");
}
它总是返回0.表示raw不更新。我究竟做错了什么。任何指针都会受到赞赏。
答案 0 :(得分:1)
您要检查两次相同的列
Gaurav.TestRecord.COLUMN_NAME_LATTITUDE + "=? AND " +
Gaurav.TestRecord.COLUMN_NAME_LATTITUDE + "=?";
应该是:
Gaurav.TestRecord.COLUMN_NAME_LATTITUDE + "=? AND " +
Gaurav.TestRecord.COLUMN_NAME_LONGITUDE + "=?";
同样在您的put
声明中:
values.put(GLNetTestDataContract.TestPinRecord.COLUMN_NAME_LATTITUDE, lattitude);
values.put(GLNetTestDataContract.TestPinRecord.COLUMN_NAME_LONGITUDE, longitude);