1

I am saving accounts settings in sqlite database in a Table. In my app I want just 8 entries to be made as max number of accounts I can have is 8. I plan to do that by checking the Number of records in the table.

     @Override
     public void onCreate(SQLiteDatabase db) {
         String q="CREATE TABLE "+ DATABASE_TABLE + " (" +
                KEY_ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
                 + KEY_CTYPE + " TEXT NOT NULL,"
                 +  KEY_SNAME + " TEXT NOT NULL,"
                 +  KEY_SNUMB + " TEXT NOT NULL,"
                 +  KEY_USRN + " TEXT NOT NULL,"
                 +  KEY_PASS + " TEXT NOT NULL);"

My question is I want to be able to delete or edit a certain entry. Will I need to do some extra steps or will the database automaticaly replace the deleted entries with the ones that are newly aded and the NO of records remain less than 8 at all time.

3
  • 1
    There is nothing automatic that will manage the number of entries for you. Just use DELETE to delete, and UPDATE command when editing. INSERT for new ones. Commented Nov 6, 2013 at 8:48
  • suppose I have 6 entries in the table, I delete the 4th One. will the next INSERT add the entry at 4th Row? Commented Nov 6, 2013 at 9:14
  • 1
    Nope the next insert will not add at the 4th row. To do that you should not delete anything, instead just create and set a flag field like "Deleted=1" ... then check when inserting if there are any that are marked as deleted=1, take the first one (lowest id) and update it instead, if not the do your normal insert, which will go to the end. Commented Nov 6, 2013 at 9:29

1 Answer 1

1

There's nothing automatic to provide that kind of functionality. When you do more than one operation of that kind in the database, remember to use transactions.

What you should do when inserting a new account in pseudocode is:

beginTransaction
if (getRecordCount >=8) deleteOneAccount
insertNewAccount
commit transaction
Sign up to request clarification or add additional context in comments.

2 Comments

I am unfamiliar with Transaction.. can u please help me by providing some link
The top answer to this question is good: stackoverflow.com/questions/8147440/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.