0

I keep getting error that a column in my table cannot be found. Cannot figure out why this is happening. The column names appear to be spelled correctly.

I'm getting this error : 06-11 21:27:02.701 3703-3703/com.example.luca.contatti E/SQLiteLog﹕ (1) table Contatti has no column named imageUri

This is my DatabaseHandler page:

public class DatabaseHandler extends SQLiteOpenHelper {
    public static final String DB_NAME = "dbContatti",
        TABLE = "Contatti",
        ID = "id",
        NAME = "name",
        PHONE = "phone",
        EMAIL = "email",
        ADDRESS = "address",
        IMAGEURI = "imageUri";
public static final int DB_VERSION = 4;

public DatabaseHandler(Context context) {
    super(context, DB_NAME, null, DB_VERSION);
}


@Override
public void onCreate(SQLiteDatabase db) {
    //Creazione della tabella per il DB
    db.execSQL("CREATE TABLE " + TABLE + "(" + ID + " INTEGER PRIMARY KEY AUTOINCREMENT," + NAME + "TEXT," + PHONE + "TEXT," + EMAIL + "TEXT," + ADDRESS + "TEXT," + IMAGEURI + "TEXT " + ")");

}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL("DROP TABLE IF EXISTS " + TABLE);

    onCreate(db);
}

// CRUD: CREATE

public void createContact (ContactList contact){
    SQLiteDatabase db = getWritableDatabase();

    ContentValues values = new ContentValues();

    values.put(NAME, contact.getName());
    values.put(PHONE, contact.getPhone());
    values.put(EMAIL, contact.getEmail());
    values.put(ADDRESS, contact.getAddress());
    values.put(IMAGEURI, contact.getImageUri().toString());

    db.insert(TABLE, null, values);
    db.close();
}

//CRUD: READ

public ContactList getContact(int id){
    SQLiteDatabase db = getReadableDatabase();

    Cursor cursor = db.query(TABLE, new String[] {ID, NAME, PHONE, EMAIL, ADDRESS, /*IMAGEURI*/}, ID + "=?", new String[] {String.valueOf(id)},null,null,null,null );

    if (cursor != null)
        cursor.moveToFirst();

    ContactList contact = new ContactList(Integer.parseInt(cursor.getString(0)),cursor.getString(1), cursor.getString(2), cursor.getString(3), cursor.getString(4), Uri.parse(cursor.getString(5)));
    db.close();
    cursor.close();
    return contact;
}

//CRUD: UPDATE

public int updateContact(ContactList contact){
    SQLiteDatabase db = getWritableDatabase();

    ContentValues values = new ContentValues();

    values.put(NAME, contact.getName());
    values.put(PHONE, contact.getPhone());
    values.put(EMAIL, contact.getEmail());
    values.put(ADDRESS, contact.getAddress());
    values.put(IMAGEURI, contact.getImageUri().toString());

    return db.update(TABLE, values, ID + "=?", new String[] { String.valueOf(contact.getId()) });
}

//CRUD: DELETE

public void deleteContact(ContactList contact){
    SQLiteDatabase db = getWritableDatabase();

    db.delete(TABLE, ID + "=?", new String[] {String.valueOf(contact.getId())});
    db.close();
}

public int getContactsCount(){
    SQLiteDatabase db = getReadableDatabase();

    Cursor cursor = db.rawQuery("SELECT * FROM " + TABLE, null);
    int count = cursor.getCount();
    db.close();
    cursor.close();
    return count;
}

public List<ContactList> getAllContacts(){
    List<ContactList> contacts = new ArrayList<ContactList>();

    SQLiteDatabase db = getWritableDatabase();
    Cursor cursor = db.rawQuery("SELECT * FROM " + TABLE, null);

    if(cursor.moveToFirst()){
        do {
            ContactList contact = new ContactList(Integer.parseInt(cursor.getString(0)),cursor.getString(1), cursor.getString(2), cursor.getString(3), cursor.getString(4), Uri.parse(cursor.getString(5)));
            contacts.add(contact);
        }
        while (cursor.moveToNext());
    }
    return contacts;
}
}

Also when i remove this statement:

values.put(IMAGEURI, contact.getImageUri().toString());

I've problem with other statements.

What can i do for fix this problem? Please help me!!

2
  • If you have added ImageUri some time later, you may need to immigrate your database. So try uninstalling your app and install again. Commented Jun 11, 2015 at 22:11
  • I've alrady unistalling my app but the reult is the same Commented Jun 11, 2015 at 22:42

1 Answer 1

3

First of all, you have to put whitespaces between each column names and their type :

db.execSQL("CREATE TABLE " + TABLE + "(" + ID + " INTEGER PRIMARY KEY AUTOINCREMENT," + NAME + " TEXT," + PHONE + " TEXT," + EMAIL + " TEXT," + ADDRESS + " TEXT," + IMAGEURI + " TEXT " + ")");

Second, in your Cursor :

Cursor cursor = db.query(TABLE, new String[] {ID, NAME, PHONE, EMAIL, ADDRESS, /*IMAGEURI*/}, ID + "=?", new String[] {String.valueOf(id)},null,null,null,null );

you wrote /*IMAGEURI*/ ! So it's normal that some lines later, the cursor.getString(5) doesn't exist, and it crashes too :

ContactList contact = new ContactList(Integer.parseInt(cursor.getString(0)),cursor.getString(1), cursor.getString(2), cursor.getString(3), cursor.getString(4), Uri.parse(cursor.getString(5)));

Finally make sure your Datamodel is updated. If you just added the imageUri column while the database has been created during previous debug sessions, it will crash. You well overrided onUpgrade method to delete existing tables, but I hope you didn't forget to increment Database version value too ;-)

Sign up to request clarification or add additional context in comments.

7 Comments

I tried to 'clear data' on my emulator and unistalled my app from the emulator but the result didn't change. Every times that i run my application i manually change the data version of the DB. i don't understand how to fix this.. What else I can do?
did you make code modifications I advised you to do, first ?
I uncommented IMAGEURI and i overrided the onUpgrade method. I don't undersand if i run the application the DB should be deleted by the method onUpgrade and recreated with the right number of column or not??
And if I unistalling and reinstalling android studio and i start a new project and after this i copy manually the code, i can fix this problem?
No, your DB is not automatically deleted by onUpgrade ! You have to "+1" your DB_VERSION int first And installing/reinstalling Android studio for such a kind of bug is absolutely overkill and useless ;-)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.