1

I've been stuck on this for a few hours now. I have an Activity that calls a method to write some values into a database, it works, except for the fact that it overWrites the same row in the database over and over again. My database table does have an _id that is set to autoincrement.

try {
    myDataBase.beginTransaction();
    myDataBase.insert("camera_notes", null, camera_data);
    myDataBase.setTransactionSuccessful();
} catch (SQLException e) {
     Log.i(TAG,"#################Exception thrown from updateDataBaseNotes:################ "+e);
    e.printStackTrace();
}
finally{
    myDataBase.endTransaction();
}

close();

I have just tried adding the transaction code, but no luck so far. Does anyone have any ideas or can point me in the right direction?

Thanks

ContentValues camera_data = new ContentValues();
camera_data.put("note_title", title);
camera_data.put("note_text", note);
camera_data.put("image_source", image_src);
camera_data.put("sound_source", recording_src);
OMG Ponies
  • 325,700
  • 82
  • 523
  • 502
Mal
  • 27
  • 1
  • 6
  • What's in camera_data? You're not accidentally setting the _id in there? – EboMike Apr 12 '11 at 22:01
  • Hello,I just added camera_data, it is an instance of ContentValues. – Mal Apr 13 '11 at 08:16
  • The _id is not being set there. – Mal Apr 13 '11 at 08:16
  • 1
    How many rows are in the table already? Is that the only one? Is it possible that you're destroying the database before inserting each entry? – EboMike Apr 13 '11 at 09:50
  • Hi EboMike, it is possible, I'll go through my code again and check, thanks. – Mal Apr 13 '11 at 10:40
  • Hi EboMike, had a good hunt around my code, you were right, my db was indeed being destroyed. Solved by adding a global class to hold db state, then checking it to avoid db recreation. I'll add the link to the SO resource I used to help with the class. [link](http://stackoverflow.com/questions/708012/android-how-to-declare-global-variables). How do I vote up your answer? – Mal Apr 13 '11 at 11:46
  • That was just a comment. I'll write a quick answer your you to accept. Thanks! – EboMike Apr 13 '11 at 15:24

1 Answers1

0

Is that the only row? If so, it sounds like you're destroying the entire table each time before you're inserting the row.

EboMike
  • 76,846
  • 14
  • 164
  • 167