0

I'm reading a book.. and now I get the problem : "(1) no such column: FAVORITE"

I tried so many things.. it always says : no such column..

I really dunno where the problem is.. cant find it.

  • Could you guys take a look ?

Thanks a lot.

package com.hfed.starbuzz;

import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class StarbuzzDatabaseHelper extends SQLiteOpenHelper {

    private static final String DB_NAME = "starbuzz";
    private static final int DB_VERSION = 2;

    StarbuzzDatabaseHelper(Context context){
        super(context, DB_NAME, null, DB_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db){
        updateMyDatabase(db, 0, DB_VERSION);
    }

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

    private void updateMyDatabase(SQLiteDatabase db, int oldVersion, int newVersion) {
        if (oldVersion < 1){
            db.execSQL("CREATE TABLE DRINK (_id INTEGER PRIMARY KEY AUTOINCREMENT, NAME TEXT, "
                    + "DESCRIPTION TEXT, IMAGE_RESOURCE_ID INTEGER);");
            insertDrink(db, "Latte", "Espresso and steamed milk", R.drawable.latte);
            insertDrink(db, "Cappuccino", "Espresso, hot milk and steamed-milk foam", R.drawable.cappuccino);
            insertDrink(db, "Filter", "Our best drip coffee", R.drawable.filter);
        }
        if(oldVersion < 2){
            db.execSQL("ALTER TABLE DRINK ADD COLUMN FAVORITE NUMERIC;");
        }
    }

    private static void insertDrink(SQLiteDatabase db, String name, String description, int resourceID)
    {
        ContentValues drinkValues = new ContentValues();
        drinkValues.put("NAME", name);
        drinkValues.put("DESCRIPTION", description);
        drinkValues.put("IMAGE_RESOURCE_ID", resourceID);
        db.insert("DRINK", null, drinkValues);
    }
}

The MainActivity.:::::

package com.hfed.starbuzz;

import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Bundle;
import android.content.Intent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.CursorAdapter;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.Toast;

public class TopLevelActivity extends Activity {
    private SQLiteDatabase db;
    private Cursor favoritesCursor;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_top_level);

        AdapterView.OnItemClickListener itemClickListener = new AdapterView.OnItemClickListener(){
            public void onItemClick(AdapterView<?> listView, View v, int position, long id) {
                if(position == 0)
                {
                    Intent intent = new Intent(TopLevelActivity.this, DrinkCategoryActivity.class);
                    startActivity(intent);
                }
            }
        };

        ListView listView = (ListView) findViewById(R.id.list_options);
        listView.setOnItemClickListener(itemClickListener);

        ListView listFavorites = (ListView)findViewById(R.id.list_favorites);
        try{
            SQLiteOpenHelper starbuzzDatabaseHelper = new StarbuzzDatabaseHelper(this);
            db =starbuzzDatabaseHelper.getReadableDatabase();
            favoritesCursor = db.query("DRINK", new String[]{"_id", "NAME"}, "FAVORITE = 1", null, null, null, null);

            CursorAdapter favoriteAdapter = new SimpleCursorAdapter(TopLevelActivity.this,
                    android.R.layout.simple_list_item_1, favoritesCursor, new String[]{"NAME"},
                    new int[]{android.R.id.text1}, 0);
            listFavorites.setAdapter(favoriteAdapter);
        }catch (SQLiteException e){
            Toast.makeText(this, "Database unavailable", Toast.LENGTH_SHORT).show();
        }

        listFavorites.setOnItemClickListener(new AdapterView.OnItemClickListener(){
            @Override
            public void onItemClick(AdapterView<?> listView, View v, int position, long id){
                Intent intent = new Intent(TopLevelActivity.this, DrinkActivity.class);
                intent.putExtra(DrinkActivity.EXTRA_DRINKNO, (int) id);
                startActivity(intent);
            }
        });
    }

    @Override
    public void onDestroy(){
        super.onDestroy();
        favoritesCursor.close();
        db.close();
    }
}
petey
  • 16,914
  • 6
  • 65
  • 97
MisterNowhere
  • 605
  • 1
  • 6
  • 13
  • Are you sure it's getting into the `if(oldVersion < 2)` case when expected? – Daniel Nugent Apr 25 '16 at 19:38
  • Can you please tell at what line the error is happening ? Less code would also be better – David Seroussi Apr 25 '16 at 19:39
  • @DanielNugent Im not sure.. but this line "public void onCreate(SQLiteDatabase db){ updateMyDatabase(db, 0, DB_VERSION); }" tells me, that it has to enter the second if-clausel. – MisterNowhere Apr 25 '16 at 19:52
  • Put a log entry inside that case in order to make sure it's getting in there and executing the line that alters the table. – Daniel Nugent Apr 25 '16 at 19:56
  • @DanielNugent when I put Log.i(...) in the TopLevelActivity in try{} I can see the output in Logcat.. but when I put Log.i in the first code .. I put them everywhere, in every Method.. no one output.. Logcat doesn't include the logs.. so the problem is in the firstCode.. no logs.. Im exhausted --'... – MisterNowhere Apr 25 '16 at 20:38
  • Simply uninstall and rerun your app. – Phantômaxx Apr 25 '16 at 20:40
  • Even the code in their website (the owner of the book "head first") is not working.. I copied all the code.. not working.. I shall contact them. – MisterNowhere Apr 25 '16 at 20:47
  • Hey Guys.. thanks a lot.. I have uninstalled the app and rerun it.. it works now :'D .. thanks a loooot :D – MisterNowhere Apr 25 '16 at 20:57

0 Answers0