java - ANDROID SQLITE: How to retrieve primary key of a certain row -
this question has answer here:
pretty simple question, still did not find answer around internet: finished creating new row , inserted database, want save newly created primary key of row. how access it? stands, trying use rest of column's values find row, getting sqliteexception : bind or column index out of range.
here code current function trying keyid:
public string getprimarykey(string gametitle, string gametype, string calories, string hours, string minutes) { cursor cursor = db.query(table_name, null, null,new string[]{gametitle, gametype,calories, hours, minutes},null, null,null,null); if(cursor.getcount()<1) { cursor.close(); return "????"; } cursor.movetofirst(); string keyid = cursor.getstring(cursor.getcolumnindex(key_id)); cursor.close(); return keyid; }
the strings being passed in parameters other columns trying use access primary key.
thank much!
the answer here give sort of need last insert id.
unfortunately, according http://www.sqlite.org/c3ref/last_insert_rowid.html:
if separate thread performs new insert on same database connection while sqlite3_last_insert_rowid() function running , changes last insert rowid, value returned sqlite3_last_insert_rowid() unpredictable , might not equal either old or new last insert rowid.
therefore, grabbing last row id susceptible concurrency issues.
you may want consider generating primary key instead of automatically generating it.
alternatively, if there other field/fields can search uniquely identify row, can select based on -- however, in case, other field(s) should actual primary key.
Comments
Post a Comment