Check if a column exists in SQLite

前端 未结 15 495
我寻月下人不归
我寻月下人不归 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条回答
  • 2020-12-08 07:29

    Update the DATABASE_VERSION so onUpgrade function is called then if Column is already exists then nothing happen if not then it will add new column.

     private static class OpenHelper extends SQLiteOpenHelper {
    
    OpenHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }
    
    @Override
    public void onCreate(SQLiteDatabase db) {
    }
    
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    
    
        if (!isColumnExists(db, "YourTableName", "YourColumnName")) {
    
            try {
    
                String sql = "ALTER TABLE " + "YourTableName" + " ADD COLUMN " + "YourColumnName" + "TEXT";
                db.execSQL(sql);
    
            } catch (Exception localException) {
                db.close();
            }
    
        }
    
    
    }
    

    }

     public static boolean isColumnExists(SQLiteDatabase sqliteDatabase,
                                         String tableName,
                                         String columnToFind) {
        Cursor cursor = null;
    
        try {
            cursor = sqliteDatabase.rawQuery(
                    "PRAGMA table_info(" + tableName + ")",
                    null
            );
    
            int nameColumnIndex = cursor.getColumnIndexOrThrow("name");
    
            while (cursor.moveToNext()) {
                String name = cursor.getString(nameColumnIndex);
    
                if (name.equals(columnToFind)) {
                    return true;
                }
            }
    
            return false;
        } finally {
            if (cursor != null) {
                cursor.close();
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-08 07:29

    I updated the function of a friend... tested and working now

        public boolean isFieldExist(String tableName, String fieldName)
    {
        boolean isExist = false;
        SQLiteDatabase db = this.getWritableDatabase();
        Cursor res = db.rawQuery("PRAGMA table_info(" + tableName + ")", null);
    
    
        if (res.moveToFirst()) {
            do {
                int value = res.getColumnIndex("name");
                if(value != -1 && res.getString(value).equals(fieldName))
                {
                    isExist = true;
                }
                // Add book to books
    
            } while (res.moveToNext());
        }
    
        return isExist;
    }
    
    0 讨论(0)
  • 2020-12-08 07:29

    I am really sorry for posting it late. Posting in the intention of may be helpful in someone's case.

    I tried fetching the column from the database. If it returns a row, it contains that column otherwise not...

    -(BOOL)columnExists { 
     BOOL columnExists = NO;
    
    //Retrieve the values of database
    const char *dbpath = [[self DatabasePath] UTF8String];
    if (sqlite3_open(dbpath, &database) == SQLITE_OK){
    
        NSString *querySQL = [NSString stringWithFormat:@"SELECT lol_10 FROM EmployeeInfo"];
        const char *query_stmt = [querySQL UTF8String];
    
        int rc = sqlite3_prepare_v2(database ,query_stmt , -1, &statement, NULL);
        if (rc  == SQLITE_OK){
            while (sqlite3_step(statement) == SQLITE_ROW){
    
                //Column exists
                columnExists = YES;
                break;
    
            }
            sqlite3_finalize(statement);
    
        }else{
            //Something went wrong.
    
        }
        sqlite3_close(database);
    }
    
    return columnExists; 
    }
    
    0 讨论(0)
提交回复
热议问题