abdul salam ali
abdul salam ali

Reputation: 21

Error while inserting data into Database using content values

I am trying to insert data in database but it is giving me following error.

table places has no column named PLACE_NAME (code 1): , while compiling: INSERT INTO places(PLACE_NAME,IS_SELECTED,placeID,LONGITUDE,LATITUDE) VALUES (?,?,?,?,?)

here is how I am creating my Database Table

 // Create a table to hold the places data
    final String SQL_CREATE_PLACES_TABLE = "CREATE TABLE IF NOT EXISTS " + PlaceContract.PlaceEntry.TABLE_NAME + " (" +
            PlaceContract.PlaceEntry._ID + " INTEGER PRIMARY KEY AUTOINCREMENT," + PlaceContract.PlaceEntry.COLUMN_PLACE_NAME + " VARCHAR, " +
            PlaceContract.PlaceEntry.COLUMN_PLACE_LATITUDE +  " VARCHAR, " +  PlaceContract.PlaceEntry.COLUMN_PLACE_LONGITUDE +  " VARCHAR, "+
            PlaceContract.PlaceEntry.COLUMN_PLACE_IS_SELECTED +  " VARCHAR, " +
            PlaceContract.PlaceEntry.COLUMN_PLACE_ID + " TEXT NOT NULL, " +
            "UNIQUE (" + PlaceContract.PlaceEntry.COLUMN_PLACE_ID + ") ON CONFLICT REPLACE" +
            "); ";

please help me what I am doing wrong ....

Upvotes: 0

Views: 88

Answers (1)

MikeT
MikeT

Reputation: 56938

The most common causes of column not found are typing errors and a misconception in regards to the onCreate method.

The former is unlikely if you are consistently using a single source for the column name e.g. if you use PlaceContract.PlaceEntry.COLUMN_PLACE_NAME to refer to the place_name column.

With the latter, the onCreate method only runs automatically when the database is created, any changes made to the schema, such as adding columns, will not be applied. Thus if you changed the CREATE SQL string to add the PLACE_NAME column that column will not be added.

When developing an App and when the data can be lost then then there are three quick ways to rectify the situation.

  1. Delete the App's data and rerun (the database will be deleted (unless the database is stored outside of the App (not recommended and not the typical scenario))).
  2. Uninstall the App and rerun (also delete's the App's data).
  3. IF the onUpgrade will drop the said table or tables and then recreate the tables (generally by calling the onCreate method) then the database version can be increased (this is the 4th parameter of the super call when constructing the Database Helper class (i.e. the class that extends SQLiteOpenHelper)).

If the data in the database cannot be lost, then the alternative is to use the ALTER to add the column or to create another table, copy the data from the original table and then drop the original table and use ALTER to rename the new table to be the original table.

Upvotes: 1

Related Questions