Simple enough question, but I cant find ant half-decent documentation on it. Right so I’ve created my SQLite db but it has two problems or one depending how you look at it! The db creates fine and I can show the data in a basic listview, however on changing the orientation of the phone/emulator the insert statements run again and duplicate all the records. Im guessing this has something to do with all the code being placed within the OnCreate() method.
My main question however is how to do INSERT UNIQUE or query the db to see if the record exists? I just could find any good documentation on SQLite DB parameters on the Android platform.
Any help is much obliged !
Cheers
You should make use of the DatabaseUtils.stringForQuery() static method that is already in Android SDK to easily retrieve a value, this example is for
Stringbot there is also method forLongUtility method to run the query on the db and return the value in the first column of the first row.
Something like
This example was to get a string value, but you can easily adapt to get a
Longvalue, and your query should be something likeif this returns a number greater the zero, then the field already exists in the database.
Also look into code for handling orientation changes.
UPDATE by @nutone:
On API 11 and higher, you can also use the
DatabaseUtils.queryNumEntriesfunctions to get the number of (filtered) rows directly.