young_08
young_08

Reputation: 1216

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 ?

Upvotes: 0

Views: 2803

Answers (4)

AbdulBasit
AbdulBasit

Reputation: 1409

Make sure you have proper spaces in the query also, You can fetch the entire data from your emulator and can check what is wrong, I had the same issue then I searched and was able to export the database from my emulator after which I came to know that my field name was not correct due to spacing in the query, I was using genymotion. I can paste the procedure for exporting the data from emulator if you need it.

Upvotes: 0

Yogesh Nikam
Yogesh Nikam

Reputation: 633

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

Upvotes: 3

Ranjit
Ranjit

Reputation: 5150

I think the issue is in your create query..Try this SQL Query for Create Table

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);";

Then Uninstall and Install the app.. It should work.

Upvotes: 3

myoungjin
myoungjin

Reputation: 915

make your sqliteHelperClass extends SQLiteOpenHelper.

example

public SqliteHelper extends SQLiteOpenHelper {
    public static final int VERSION = 1;
    public static final String NAME = "your db name";

    public SqliteHelper(Context context) {
        super(context, NAME, null, VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        // your db creates here
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // your db upgrade here
        // ex) drop & create
    }

}

with this helper class, if you need db upgrade, just change Version values + 1

i hope this is helpful for you.

Upvotes: -1

Related Questions