마스터Q&A 안드로이드는 안드로이드 개발자들의 질문과 답변을 위한 지식 커뮤니티 사이트입니다. 안드로이드펍에서 운영하고 있습니다. [사용법, 운영진]

alter 질문드립니다.

0 추천
db.execSQL("ALTER TABLE data"
                + " ADD test TEXT NOT NULL DEFAULT 0;");

sqllite 에 data table에 컬럼을 추가 하고싶은데 만약 컬럼이 존재하면 추가하지 않도록

 

수정하고 싶습니다.

 

어떻게 쿼리문을 적용해야할까요 ?

 

 

now882002 (3,860 포인트) 님이 2015년 3월 30일 질문
if not exsits는 사용하지 못한다네요

1개의 답변

0 추천

쿼리문으로는 방법이 있는지는 모르겠군요..

제 생각에는 가장 쉬운방법으로는 db_version을 이용하는 방법이 있겠구요

아니면 쿼리를 해서 컬럼 유무을 체크해야 할 듯 합니다.private boolean existsColumnInTable(SQLiteDatabase inDatabase, String inTable, String columnToCheck) {

    try{
        //query 1 row
        Cursor mCursor  = inDatabase.rawQuery( "SELECT * FROM " + inTable + " LIMIT 0", null );
 
        //getColumnIndex gives us the index (0 to ...) of the column - otherwise we get a -1
        if(mCursor.getColumnIndex(columnToCheck) != -1)
            return true;
        else
            return false;
 
    }catch (Exception Exp){
        //something went wrong. Missing the database? The table?
        Log.d("... - existsColumnInTable","When checking whether a column exists in the table, an error occurred: " + Exp.getMessage());
        return false;
    }
}
Gradler (109,780 포인트) 님이 2015년 3월 30일 답변
...