15

This is the code that works for adding a column.

mDb.execSQL("ALTER TABLE " + table_name + 
            " ADD COLUMN " + column_name + " text");

My problem the column is created at the last position of the table.

Column1|Column2|Column3|Column4|Column5|NewAddedColumn6

E.g. Is is possible to add a column between Column3 and Column4???

Column1|Column2|Column3|NewAddedColumn6|Column4|Column5
Ravinder Reddy
  • 23,042
  • 6
  • 48
  • 79
Tower Jimmy
  • 526
  • 1
  • 4
  • 15

1 Answers1

24

As per documentation

The ADD COLUMN syntax is used to add a new column to an existing table. The new column is always appended to the end of the list of existing columns.

Hence, answer to your question is NO

But, there is a work around way.
Read this answer on SO.

Refer to: SQLite: ALTER TABLE: ADD COLUMN

Community
  • 1
  • 1
Ravinder Reddy
  • 23,042
  • 6
  • 48
  • 79
  • thanks for the quick answer. I thought I could avoid creating and deleting tables just to add a column -_- – Tower Jimmy Mar 31 '14 at 00:41
  • why do you need to create a column between Column3 and Column4 (whatever it means) ? – pskink Mar 31 '14 at 06:31
  • 4
    @pskink: Literally, I don't think it is any *technical* requirement but a *satisfaction* by feeling. – Ravinder Reddy Mar 31 '14 at 08:04
  • sorry, but i still dont understand, do you need some particular column order in a Cursor or what? – pskink Mar 31 '14 at 08:14
  • @pskink: No. It was OP's requirement. And, as I mentioned, I don't think it has any importance to worry about. – Ravinder Reddy Mar 31 '14 at 08:16
  • ooops sorry i overlooked this – pskink Mar 31 '14 at 08:20
  • though it's really late and probably nobody cares but here is the reason why I needed this. User can create dynamically tables and list/render them with all columns. If a user later on decides this table need one additional column it will get displayed at the last position. But maybe this information was much more useful if it was next to another column. This issue became even worse when you add new items/references into the table, the workflow was very inconveniently interrupted, scrolling up/down. Last but not least same issue applied to the Excel export. – Tower Jimmy Mar 27 '15 at 02:38
  • Position of a field might require only for display purposes. This can be easily be achieved by selecting specific columns in specific positions. `select * from emp` will result the default column position defined. Where as `select empno, empname, deptno, salary, etc from emp` will re-order the resulting column position, which would be very handy for *reporting* type of views. For `Excel` type of exports too explicit selection of columns will help to get your desired result. – Ravinder Reddy Mar 27 '15 at 06:10
  • Hmmm... No comment by DownVoter on 2018-03-21 ???? Won't help readers unless one expresses their comment on down voting. – Ravinder Reddy Mar 23 '18 at 07:32