0

I know there have been some topics about that and I tried following all the answers given there however it didn't help with my situation. I keep getting error that column SecondPhone does not exist

E/SQLiteLog: (1) table Contacts has no column named SecondPhone E/SQLiteDatabase: Error inserting SecondPhone= Phone= PhotoUri=android.resource://project.contactmanager/drawable/untitled_1.png Email= Address= Name=Test android.database.sqlite.SQLiteException: table Contacts has no column named SecondPhone (code 1): , while compiling: INSERT INTO Contacts(SecondPhone,Phone,PhotoUri,Email,Address,Name) VALUES (?,?,?,?,?,?)

This is part of the error message I'm getting

And this is part my code. I expect the problem being here but it might elsewhere

public class DatabaseSQL extends SQLiteOpenHelper {

private static final int DATABASE_VERSION = 2;
    //setting variables for future database columns
private static final String DATABASE_NAME = "ContactManagerLite",
        TABLE_CONTACTS = "Contacts",
        KEY_NAME = "Name",
        KEY_PHONE = "Phone",
        KEY_SECONDPHONE = "SecondPhone",
        KEY_EMAIL = "Email",
        KEY_ADDRESS = "Address",
        KEY_PHOTOURI = "PhotoUri",
        KEY_ID = "ID";

public DatabaseSQL(Context context){
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

@Override
public void onCreate(SQLiteDatabase db){                       //creates a table upon starting the application
    db.execSQL("CREATE TABLE " + TABLE_CONTACTS + "(" + KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," + KEY_NAME + " TEXT" + KEY_PHONE + " TEXT" + KEY_SECONDPHONE + " TEXT" + KEY_EMAIL + " TEXT" + KEY_ADDRESS + " TEXT" + KEY_PHOTOURI + " TEXT)");

}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion){                       //if something changes the old table is being deleted and new one is being created with new data
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_CONTACTS);
    onCreate(db);
}
public void createContact(contactList contact){        //variables from getters in contactList are being set as values for database
    SQLiteDatabase db= getWritableDatabase();
    ContentValues values = new ContentValues();
    values.put(KEY_NAME, contact.getListName());
    values.put(KEY_PHONE, contact.getListPhone());
    values.put(KEY_SECONDPHONE, contact.getSecondPhone());
    values.put(KEY_EMAIL, contact.getEmail());
    values.put(KEY_ADDRESS, contact.getAddress());
    values.put(KEY_PHOTOURI, contact.getProfilePhotoUri().toString());

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

I'm working on AndroidStudio

3 Answers 3

2

Its typo, you have not seperated any of your column name. Put (,) after datatype of each column

KEY_NAME + " TEXT," + KEY_PHONE + " TEXT," + KEY_SECONDPHONE + " TEXT," +
KEY_EMAIL + " TEXT," + KEY_ADDRESS + " TEXT," + KEY_PHOTOURI + " TEXT)"

once it is done, reinstall your app

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

Comments

0

You did not put comma after every column. Try this

db.execSQL("CREATE TABLE " + TABLE_CONTACTS + "(" + KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," + KEY_NAME + " TEXT," + KEY_PHONE + " TEXT," + KEY_SECONDPHONE + " TEXT," + KEY_EMAIL + " TEXT," + KEY_ADDRESS + " TEXT," + KEY_PHOTOURI + " TEXT)");

Run your project after clearing data or reinstalling the app

Hope this helps you.

Comments

0

You are missing commas in this create table query

db.execSQL("CREATE TABLE " + TABLE_CONTACTS + "(" + KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," + KEY_NAME + " TEXT" + KEY_PHONE + " TEXT" + KEY_SECONDPHONE + " TEXT" + KEY_EMAIL + " TEXT" + KEY_ADDRESS + " TEXT" + KEY_PHOTOURI + " TEXT)");

This should be

db.execSQL("CREATE TABLE " + TABLE_CONTACTS + "(" + KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," + KEY_NAME + " TEXT," + KEY_PHONE + " TEXT," + KEY_SECONDPHONE + " TEXT," + KEY_EMAIL + " TEXT," + KEY_ADDRESS + " TEXT," + KEY_PHOTOURI + " TEXT)");

(you have to re-run the query to make it updates, so delete app's datas on the phone before running) Hope this helps, let me know

3 Comments

I spend hours looking for solution, I was sure there were commas! xd Thanks a lot
@wierzejs ahahah it's always like this, we waste more time on little invisible errors than on making programs :)
@wierzejs remember to accept one of the answer as the correct one so other ppl having similar issues can solve their problem

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.