Skip to content
Advertisement

Problems with SQLiteException: no such table

I have a database with two tables and every time I try to add a new item to the second table it says that there is no such table. Please help.

DatabaseHelper:

@Override
    public void onCreate(SQLiteDatabase db) {
        String createTable = "CREATE TABLE " + TableName + " ( "+COL_1 +" INTEGER PRIMARY KEY AUTOINCREMENT, "+COL_2+ " TEXT, "+COL_3+ " TEXT, "+COL_4+" TEXT, "+COL_5+ " INTEGER)";
        String createSubjectsTable = "CREATE TABLE "+SubjectsTableName+" ( "+Subjects_COL_1+" INTEGER PRIMARY KEY AUTOINCREMENT, "+Subjects_COL_2+" TEXT, "+Subjects_COL_3+" TEXT, "+Subjects_COL_4+" TEXT, "+Subjects_COL_5+" TEXT)";
        db.execSQL(createTable);
        db.execSQL(createSubjectsTable);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS "+ TableName);
        db.execSQL("DROP TABLE IF EXISTS "+SubjectsTableName);
        onCreate(db);
    }

The add method from DatabaseHelper:

 public boolean addSubjectData (String name, String letters, String teacher, String color){
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues contentValues = new ContentValues();
        contentValues.put(Subjects_COL_2,name);
        contentValues.put(Subjects_COL_3,letters);
        contentValues.put(Subjects_COL_4,teacher);
        contentValues.put(Subjects_COL_5,color);
        long result = db.insert(SubjectsTableName, null, contentValues);
        if (result==-1){
            return false;
        }
        else{
            return true;
        }
    }

The place where the new item creation is:

saveBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(name!=null && letters!=null && color!=null){
                    mDatabase.addSubjectData(name,letters,teacher,color);
                } 
            }
        });

Advertisement

Answer

you may have changed the schema and did not increase the db version so you may have two options:

  • increase the db version
  • or if you did not publish previous version of your app uninstall app from emulator then run project
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement