wierzejs
wierzejs

Reputation: 3

table [...] has no column named [...] SQLite java

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

Upvotes: 0

Views: 671

Answers (3)

Pier Giorgio Misley
Pier Giorgio Misley

Reputation: 5351

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

Upvotes: 0

chetan
chetan

Reputation: 681

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.

Upvotes: 0

Ravi
Ravi

Reputation: 35589

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

Upvotes: 2

Related Questions