0

I am newbie to android SQLite and i had a doubt about inserting records in sqlite. How we can insert a record at the pirticular position into the sqlite i,e. if i am inserting first record it will goes to the first position,Then i'll skip the second record and then insert the third record and it will store as record 3 with ID 3 as shown below

 ID value 1 abc 2 3 def 4 ghi 5 jkl 6 

Like this can anyone tell me how to achieve this

4
  • 1
    What about using a 'position' column? In the select you can then order by position like 'select * from table order by position' Commented Dec 11, 2015 at 14:11
  • can you help me in detail better with code @MimmoGrottoli Commented Dec 11, 2015 at 14:36
  • do you have a fix number of rows in your table? Commented Dec 11, 2015 at 14:46
  • Yup i am having the fixed num of rows that is 7 if count was greater than 7 then data in table will be deleted and updated @Opiatefuchs Commented Dec 11, 2015 at 14:49

2 Answers 2

1

Create a method in your database helper class and pass the parameter . Giving you a code sample . Hope it will help you

 public void updateTable(String catID, String data) { try { String sql = "UPDATE data_table SET column_data = " + data + " WHERE cat_id = " + catID; mDb.execSQL(sql); } catch (SQLException mSQLException) { Log.e(TAG, "updateTable >>" + mSQLException.toString()); throw mSQLException; } } 
Sign up to request clarification or add additional context in comments.

Comments

0

Your best bet would be to create your table with 3 columns:

  • _id -> unique identifier of that entry
  • slot -> this is your 1 - 6 slot, you have to take care about overflow etc.
  • value -> your value "abc" etc.

e.g.:

CREATE TABLE slots (_id INTEGER PRIMARY KEY AUTOINCREMENT, slot INTEGER NOT NULL, value TEXT);

Basically you could skip the _id column, but it is used in the CursorAdapter implementation, so you would run into trouble there and it doesn't hurt you. As text can be null, you can create a scenario as the one you described above, only your slot's integer value has to be set.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.