Android SQLite Alter Table

-- Android 2012. 10. 11. 13:55
336x280(권장), 300x250(권장), 250x250, 200x200 크기의 광고 코드만 넣을 수 있습니다.

DB업데이트 시 이전 DB에 대한 처리를 어떻게 할지 고민이었는데 아래의 글을 참고하여 처리하면 될 듯 하다.

When upgrading SQLite database sometimes you need to change the tables scheme. The problem is that SQLite Alter Table SQL formatdoes not support all the options you might need (specifically - dropping columns, changing column types, renaming columns).

 

One way around this issue is to create a new table & copy the data to the new table. There are few options here, but the best way (I think) is as follows:

1. Create new table in the new format.

2. Copy the data.

3. Drop the original table

4. Rename the new table

 

For example:

BEGIN TRANSACTION;

CREATE TABLE t_new(a,b);

INSERT INTO t_new SELECT a,b FROM t;

DROP TABLE t;

ALTER TABLE t_new RENAME TO t;

COMMIT;

 

 

IMPORTANT: As far as I know 'execSQL' of 'SQLiteDatabase' will NOT run multiple commands. You should run each SQL command in a separate 'execSQL' call (the transaction can be handled from the SQLiteDatabase, not need for explicit SQL commands).

출처 : http://sagistech.blogspot.kr/2011/01/android-sqlite-alter-table.html

posted by 어린왕자악꿍