1

Given a list of items that need to either update or add records to the database, how can I do/replicate an UPSERT operation using SQLJet? Below I have a minor solution, but the problem is if the order of games in the list changes, then I might skip over a record in the database, and add duplicates for the items in the Game list.

for (Game g : games) {
    if (!updateCursor.eof() &&
         updateCursor.getString(GAME_TABLE_GID).equals(g.getGameID())) {
         updateCursor.update(
             updateCursor.getInteger(GAME_TABLE_PID),
             updateCursor.getString(GAME_TABLE_GID),
             gson.toJson(g)
         );
         updateCursor.next();
     }
     else {
         gameTable.insert(g.getGameID(), gson.toJson(g));
     }
}

The other solutions I'm thinking of includes iterating over the list, and then going through the entire table each time, comparing against each record's given game id in the row. I'd really rather not do this.

I've tried finding documentation for this, but of the 10 (now 11) questions tagged SQLJet on Stack Overflow and a seeming lack of documentation with the project, I haven't been able to find anything.

Greenstack
  • 11
  • 1
  • 3

0 Answers0