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.

dmSherazi
  • 3,743
  • 5
  • 37
  • 62
  • 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. – sprocket12 Nov 06 '13 at 08:48
  • suppose I have 6 entries in the table, I delete the 4th One. will the next INSERT add the entry at 4th Row? – dmSherazi Nov 06 '13 at 09: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. – sprocket12 Nov 06 '13 at 09:29

1 Answers1

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
Szymon
  • 42,577
  • 16
  • 96
  • 114