Search code examples
androidandroid-sqlitesqliteopenhelper

Android SQLite can't find column that exists


I am creating a table and using insert or replace sql in local database android but it giving me error :

*

table employeeTable has no column named activationCode (code 1): , while compiling: insert or replace into employeeTable(activationCode,userName,userType,lastSeen)VALUES ('6131313313' , 'testingcheck' , 'Employer' , 'Tue Oct 27 10:04:36 GMT+05:30 2015');

*

My creating table code :

sql = "CREATE TABLE " + EMPLOYEE_TABLE + "(" + COMMON_ID_COLUMN + " INTEGER PRIMARY KEY AUTOINCREMENT, "
            + ACTIVATION_CODE_COLUMN + "TEXT ," + USER_NAME_COLUMN + "TEXT ," + USER_TYPE_COLUMN + "TEXT,"+LAST_SEE_COLUMN + "Text);";

    db.execSQL(sql);

}

My insert or replace code :

public void insertEmployeeDetails(String activationCode1 , String userName , String userType , String lastSeen) {
    String sql1 = "insert or replace into " + EMPLOYEE_TABLE + "(" + ACTIVATION_CODE_COLUMN + " , " + USER_NAME_COLUMN + " , " +
USER_TYPE_COLUMN +" , "+  LAST_SEE_COLUMN + ")" + "VALUES ('" +activationCode1 +"' , '" + userName + "' , '" + userType + "' , '"+ lastSeen + "');";

    getWritableDatabase().execSQL(sql1);
}

and callback code:

myDbHelper.insertEmployeeDetails(employerConstants.activationCode,employerConstants.userName,
                employerConstants.userType , employerConstants.lastSeenon);

Column activation code exists but not able to figure out what causing a problem ?


Solution

  • Use following code for insert data.

    dataBase = getWritableDatabase();
    cValues = new ContentValues();
    cValues.put(ACTIVATION_CODE_COLUMN, activationCode1);
    cValues.put(USER_NAME_COLUMN, userName);
    cValues.put(USER_TYPE_COLUMN, userType);
    cValues.put( LAST_SEE_COLUMN, lastSeen);
    dataBase.insert(EMPLOYEE_TABLE, null, cValues);
    dataBase.close();
    

    Also your create query contain error

    sql = "CREATE TABLE " + EMPLOYEE_TABLE + "(" + COMMON_ID_COLUMN + "  INTEGER PRIMARY KEY AUTOINCREMENT, "
            + ACTIVATION_CODE_COLUMN + " TEXT, " + USER_NAME_COLUMN + " TEXT , " + USER_TYPE_COLUMN + " TEXT, "+LAST_SEE_COLUMN + " Text);";
    

    Copy above query and replace with your query Feel free for comment