0

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 ....

5
  • Best include values of PlaceContract.PlaceEntry .. Commented May 17, 2019 at 16:35
  • what did you said ??? Commented May 17, 2019 at 16:37
  • please show how you define PlaceContract so we can see what's in PlaceContract.PlaceEntry.COLUMN_PLACE_NAME Commented May 17, 2019 at 16:40
  • If you made changes to the column names after you first ran the app, uninstall the app from the device so the database is deleted and rerun to recreate the database with the new names. Commented May 17, 2019 at 16:41
  • let me test it...... Commented May 17, 2019 at 16:43

1 Answer 1

1

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.

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

Comments

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.