Check if a column exists in SQLite

前端 未结 15 492
我寻月下人不归
我寻月下人不归 2020-12-08 07:02

I need to check to see if a column exists and if it doesn\'t exist add it. From my research it looks like sqlite doesn\'t support IF statements and case statement should be

15条回答
  •  -上瘾入骨i
    2020-12-08 07:19

    // This method will check if column exists in your table
    public boolean isFieldExist(String tableName, String fieldName)
    {
         boolean isExist = false;
         SQLiteDatabase db = this.getWritableDatabase();
         Cursor res = db.rawQuery("PRAGMA table_info("+tableName+")",null);
        res.moveToFirst();
        do {
            String currentColumn = res.getString(1);
            if (currentColumn.equals(fieldName)) {
                isExist = true;
            }
        } while (res.moveToNext());
         return isExist;
    }
    

提交回复
热议问题