我的安卓应用程序正在成功地写入SQLlite数据库,但它的_id位置为0。我是否可以调整初始写入代码到数据库,而不是从位置/行0开始呢?我正在使用这种测试方法来验证当前从自旋器/下拉列表中选择的人的位置,以最终删除客户端。
我能像这样测试它的位置
Cursor c = db.getClient(currentlySelected+1);返回位置1的成功测试吐司消息,我这样做是为了验证第一个位置是0或曾经0。Cursor c = db.getClient(currentlySelected);崩溃和DDMS报告一个CursorIndexOutOfBounds错误,请求0或类似的值为0。DB是数据库适配器:
db.open();
long currentlySelected = contactList.getSelectedItemId(); //spinner populated from database
Cursor c = db.getClient(currentlySelected+1);
Toast.makeText(LookupReturn.this,
"Cursor position: " + c.getLong(c.getColumnIndex("_id")),
Toast.LENGTH_LONG).show();
//db.deleteClient(currentlySelected);
c.close();
db.close();下面的部分是我的决议
db.open();
long currentlySelected = contactList.getSelectedItemId()+1;
long currentlySelectedTemp = currentlySelected; //holds initial position value when this value is removed from the main db
Cursor c = db.getClient(currentlySelected);
if(c.moveToFirst())
{
Toast.makeText(LookupReturn.this, "Position: " + currentlySelected, //c.getLong(c.getColumnIndex("_id")),
Toast.LENGTH_LONG).show();
db.deleteClient(currentlySelected);
c.close();
db.close();
Toast.makeText(LookupReturn.this, "Deleted",
Toast.LENGTH_LONG).show();
deleteClient.setChecked(false);
}
//remove from secondary db
DatabaseHandler dbh = new DatabaseHandler(getApplicationContext());
SQLiteDatabase s = dbh.getWritableDatabase();
s.execSQL("DELETE FROM returnvisits WHERE _id = " + currentlySelectedTemp);s.close();
发布于 2013-08-03 15:45:52
Android是从_id 1开始编写的。要从游标读取值,需要执行以下操作
c.moveToNext();执行后
Cursor c = db.getClient(currentlySelected+1);然后,您可以使用游标获取值。
发布于 2013-08-03 15:44:26
可以使用c.moveToNext();或c.moveToPosition(positionIndexHere);移动到下一个位置或数据库中的特定位置(C是游标变量)。
https://stackoverflow.com/questions/18034412
复制相似问题