views:

771

answers:

2

Hey guys, I'm pretty new to sqlite 3 and just now I had to add a column to an existing table I had. I went about doing that by doing: ALTER TABLE thetable ADD COLUMN category;.

Of course, I forgot to specify that column's type. The first thing I was thinking about doing was dropping that column and then re-adding it. However, it seems that sqlite does not have a simple way of doing this, and I would have had to backup the table and re-create it without the column.

This seems messy, and I was wondering if there were just a way of modifying/adding a column's type. I would imagine so, but my searching around yielded no results, being new to sqlite, I imagine it was due to my wording being off in the query.

A: 

According to the SQLite ALTER TABLE documentation, there is no alternative. You have to perform the steps you've already mentioned:

  1. Rename the existing table
  2. Create a new table with the correct columns and data types
  3. Select the date from the old table into the new one
  4. Drop the old table
OMG Ponies
+3  A: 

SQLite doesn't support removing or modifying columns, apparently. But do remember that column data types aren't rigid in SQLite, either.

Roger Pate
So if I didn't define a type, I should still be able to insert text into it? Of course later when I deploy to production I will explicitly state the type (text), but for now I should be fine?
Jorge Israel Peña
Yes. You can insert non-text values into that column too: "SQLite uses a more general dynamic type system. In SQLite, the datatype of a value is associated with the value itself, not with its container." (from my second link) Specifying the types of columns has much less significance in SQLite than other RDBMs.
Roger Pate