18

My code :

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

The column is added to last position :

Column1 Column2 Column3 Column4 Column5 NewAddedColumn6

Is it possible to add a column between Column3 and Column4?

Column1 Column2 Column3 NewAddedColumn6 Column4 Column5
user4157124
  • 2,809
  • 13
  • 27
  • 42
Tower Jimmy
  • 567
  • 1
  • 4
  • 15
  • That's an [XY problem](https://en.wikipedia.org/wiki/XY_problem) (question about an attempted solution rather than the actual problem); describe what you are trying to achieve by doing this instead. – user4157124 Nov 11 '22 at 18:14
  • Consider rearranging column order on `SELECT` instead. – user4157124 Nov 11 '22 at 18:15

1 Answers1

27

As per the SQLite > ALTER TABLE > ADD COLUMN 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, the answer to your question is NO.

However, there is a workaround. See this answer.

Adil Hussain
  • 30,049
  • 21
  • 112
  • 147
Ravinder Reddy
  • 23,692
  • 6
  • 52
  • 82
  • 2
    why do you need to create a column between Column3 and Column4 (whatever it means) ? – pskink Mar 31 '14 at 06:31
  • 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