如何在SQLite中获取行的ID



我有一个notes应用程序,它与MySql服务器成对使用。当我想从服务器加载注释时,我会得到注释的JSONArray宽度serverId(sid(、标题和描述。然后我检查本地SQLite数据库中是否存在serverId(sid(。如果sid存在,我只想从服务器更新注释宽度的新标题和描述,但要更新注释,我需要该行的Sqlite数据库id,我不知道如何获取。有什么想法吗?我是个新手,所以我很高兴能得到任何建议。

public void loadServerNotes(String title, String des, String sid){
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = null;
// Here I'm checking if the server id exists in my SQLite database
String findIfServerIdExist = "SELECT * FROM " + TABLE_NAME + " WHERE " + COLUMN_SID + " = " + sid + ";";

cursor= db.rawQuery(findIfServerIdExist,null);

// If server id already exists in my SQLite database I want to update the note with the server title and description
// if the server id doesn't exist I create a new note with the server title and description
boolean exists = (cursor.getCount() > 0);
if (exists){
updateNote(**Id that i need to find**, title, des, Api.NAME_SYNCED_WITH_SERVER, sid);
}else {
createNote(title, des,  Api.NAME_SYNCED_WITH_SERVER, sid);
}
cursor.close();
}

我的SQLite列示例:

COLUMN_ID/COLUMN_TITLE/COLUMN_DES/COLUMN_STATUS/COLUMN_ID

答案:我找到了如何做到这一点,这里的代码有点简单:

if (exists){
if (cursor.moveToFirst()){
int index = cursor.getColumnIndex(SqliteDatabaseNotes.COLUMN_ID);
int id = cursor.getInt(index);
String stid = Integer.toString(id);
updateNote(stid,title, des, Api.NAME_SYNCED_WITH_SERVER, sid);
}

相关内容

  • 没有找到相关文章

最新更新