0

im using greenDAO to generate my DAO classes, everything works fine till i try to insert duplicated values for primary key.

It fails when i try to insert duplicated primery key (as expected), but i would like to send the exception to the user and not o explode with the app.

For now the only solution i have, and i think its not the best, is to do a query before insert, and check if the result count is greater than 0 or not.

There is a better way to do it?

the method .insert returns a Long value, but when it fails to insert it doesn't return anything, just explodes with the app.

Bugdr0id
  • 2,962
  • 6
  • 35
  • 59

3 Answers3

1

Best would be to keep your primary key incremental away from what the user supplies as a key. However that is my personal choice, but aids when I have to check the last inserted ID, Ill do something like:

public int getPrimaryId() {
        final String MY_QUERY = "SELECT MAX(_id) FROM " + DATABASE_TABLE0;
        Cursor cur = mDb.rawQuery(MY_QUERY, null);
        cur.moveToFirst();
        int ID = cur.getInt(0);
        cur.close();
        return ID;
    }

In your case you can preload an array list with primaryID's once user supplies a new key just check it with the elements in the arraylist. Faster!

Update: To Check if ID exists.

SELECT EXISTS(SELECT 1 FROM myTbl WHERE ID="anyID" LIMIT 1);

The LIMIT will ensure that once found there is no need to go further. This would be super fast. Plus Exists will always return a result.

Also suggested would be the use of proper indexes

Community
  • 1
  • 1
Skynet
  • 7,820
  • 5
  • 44
  • 80
  • i understand your point, but what i would like to know, is if there is any other way to do it, without doing a select. I was wondering if it would be possible to simply catch the error, and take care of it, instead of just explode with the app. – Bugdr0id Feb 24 '14 at 15:52
  • To match an existing ID with a user supplied ID, I think the only way would be to use a Select query. But, if you use a Where clause, things become much faster. – Skynet Feb 24 '14 at 15:53
  • this is a solution but i dont think its a good approach. Imagine that u have a pair of keys (field_A, field_B, field_C) and those 3 fields must be a unique combination. greenDAO lets you implement an index to make sure this index is unique. If i'll controll this index on users side (programming, like your solution) why would i need to create it on greenDAO side? why should i create an index on sqlite? – Bugdr0id Feb 24 '14 at 16:01
  • You don't have to manage the index on the users side, that is why my first question was that are you using an incremental PrimaryKey. Your scenario might be different although. – Skynet Feb 24 '14 at 16:04
  • i did override for the .insert() method, and it's working like i would like it too. Although i think it should be implemented by greenDAO generator. Thanks – Bugdr0id Feb 24 '14 at 16:10
1

Surround your insert-statement with a try-catch-block

try {
    dao.insert (entity);
} catch (Exception ex) {
    // Do your handling here
} catch (Error e) {
    // and here
}
AlexS
  • 5,295
  • 3
  • 38
  • 54
0

I thought it would be another way to control it, but i found my way without doing this SELECT. Here are two solutions:

  1. With SELECT

With SELECT (proposed by other user on this post - check it for more details)

public int getPrimaryId() {
        final String MY_QUERY = "SELECT MAX(_id) FROM " + DATABASE_TABLE0;
        Cursor cur = mDb.rawQuery(MY_QUERY, null);
        cur.moveToFirst();
        int ID = cur.getInt(0);
        cur.close();
        return ID;
    }
  1. Override method

    Override .insert() method, inside myObjectDAO.java

@Override
  public long insert(Stud_Course entity) {
      long ret = 0;
      try {
          ret = super.insert(entity);
      } catch (Exception e) {
          ret = 0;
          Log.e("LOG", "fail to insert!!!");
      }
      return ret;
  }
Bugdr0id
  • 2,962
  • 6
  • 35
  • 59
  • 2
    You shouldn't override generated methods. Your modifications will be lost upon regeneration, i.e. when your schema gets modified/extended. Put the try-catch outside. – AlexS Feb 26 '14 at 06:59