-1

While creating a database I face the following error:

E/SQLiteLog: (1) table table_place has no column named Fro E/SQLiteDatabase: Error inserting Toplace =Banglore Fro =Delhi table table_place has no column named Fro (code 1): , while compiling: INSERT INTO table_place(Toplace ,Fro ) VALUES (?,?)

This is the code for db handler:

 private static final int DATABASE_VERSION = 1;
    private static final String DATABASE_NAME = "Place.db";
    private static final String TABLE_PLACE = "table_place";
    private static final String KEY_ID = "id";
    private static final String KEY_FROM="Fro ";
    private static final String KEY_TO ="Toplace ";

    //Calling a constructor

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

    //Overriding methods


    @Override
    public void onCreate(SQLiteDatabase db) {

        String CREATE_PLACE_TABLE = "CREATE TABLE " + TABLE_PLACE + " ("
                + KEY_ID + " INTEGER PRIMARY KEY, " + KEY_FROM + " TEXT, "
                + KEY_TO + " TEXT);";
        db.execSQL(CREATE_PLACE_TABLE);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

        db.execSQL("DROP TABLE IF EXISTS " + TABLE_PLACE);
        onCreate(db);

    }

    //Adding data
    public void save(Place place){
        SQLiteDatabase db=this.getWritableDatabase();
        ContentValues values=new ContentValues();
        values.put(KEY_FROM, place.getFrom());
        values.put(KEY_TO, place.getTo());

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

    //Finding data
    public Place findOne(int id){
        SQLiteDatabase db=this.getReadableDatabase();
        Cursor cursor=db.query(TABLE_PLACE, new String[]{KEY_ID,KEY_FROM,KEY_TO},
                KEY_ID+" =? ", new String[]{String.valueOf(id)}, null, null, null);
        if(cursor!=null){
            cursor.moveToFirst();
        }
        return new Place(Integer.parseInt(cursor.getString(0)),cursor.getString(1),cursor.getString(2));
    }

    //Finding all data
    public List<Place> findAll(){
        List<Place> listplace=new ArrayList<Place>();
        String query="SELECT * FROM "+ TABLE_PLACE;

        SQLiteDatabase db=this.getReadableDatabase();
        Cursor cursor=db.rawQuery(query, null);

        if(cursor.moveToFirst()){
            do{
                Place place=new Place();
                place.setId(Integer.valueOf(cursor.getString(0)));
                place.setFrom(cursor.getString(1));
                place.setTo(cursor.getString(2));
                listplace.add(place);
            }while(cursor.moveToNext());
        }

        return listplace;
    }

    //to update
    public void update(Place place){
        SQLiteDatabase db=this.getWritableDatabase();

        ContentValues values=new ContentValues();
        values.put(KEY_FROM , place.getFrom());
        values.put(KEY_TO, place.getTo());

        db.update(TABLE_PLACE, values, KEY_ID+" =? ", new String[]{String.valueOf(place.getId())});
        db.close();
    }

    //to delete
    public void delete(Place place){
        SQLiteDatabase db=this.getWritableDatabase();
        db.delete(TABLE_PLACE, KEY_ID+" =? ", new String[]{String.valueOf(place.getId())});
        db.close();
    }
}

For main activity:

    Log.d("insert", "inserting data");
    databaseHandler.save(new Place("Delhi", "Banglore"));
    databaseHandler.save(new Place("Chennai", "Mumbai"));

    Log.d("reading", "reading all data");
    List<Place> listplace = databaseHandler.findAll();
    for (Place b : listplace) {
        Log.d("data", "ID :" + b.getId() + " | Fro :" + b.getFrom() + " | Toplace :" + b.getTo());
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Dlord
  • 1
  • 2
  • Possible duplicate of [When is SQLiteOpenHelper onCreate() / onUpgrade() run?](https://stackoverflow.com/questions/21881992/when-is-sqliteopenhelper-oncreate-onupgrade-run) – Phantômaxx Jun 19 '17 at 08:46

1 Answers1

1

Remove the space.

private static final String KEY_FROM="Fro";

Do the same thing for KEY_TO. After changing the code, make sure you re-install your app again.

John Joe
  • 12,412
  • 16
  • 70
  • 135
  • 1
    Spaces aren't harmful. They will be simply ignored. Since the column has been probably added after a previous test run, **this** `make sure you re-install your app again` is the key to make it work. – Phantômaxx Jun 19 '17 at 08:42
  • 1
    @Rotwang Spaces will be ignored in any SQL that's being built, but the values (with spaces) are also being used directly in the `db.update()` call where I expect they _will_ cause problems. – TripeHound Jun 19 '17 at 09:07