简体   繁体   中英

Why is my cursor count 0?

I am new to SQLite and I am building an application to suggest recipes based on ingredients selection but while I am checking if my database contains any records, it does not shows any records and displays cursor count as 0. I am getting the column count as 3 but i am not getting the row count. Please help.

This is my DatabaseHelper class:

public class DatabaseHelper extends SQLiteOpenHelper 
{

    public final static String DATABASE_NAME = "gobbled.db";
    public final static String TABLE1 = "ingredients";
    public final static String TABLE2 = "dishes";
    public final static String TABLE3 = "recipe";
    public final static String COLUMN_1_1 = "ing_id";


    public DatabaseHelper(Context context){
        super(context, DATABASE_NAME, null, 1);
        SQLiteDatabase db=this.getReadableDatabase();
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db = this.getWritableDatabase();
        db.execSQL("CREATE TABLE IF NOT EXISTS " + TABLE1 + "(ing_id INT(4) PRIMARY KEY, ing_name VARCHAR, ing_category VARCHAR)");
        db.execSQL("CREATE TABLE IF NOT EXISTS " + TABLE2 + "(dish_id INT(4) PRIMARY KEY, dish_name VARCHAR, dish_recipe VARCHAR)");
        db.execSQL("CREATE TABLE IF NOT EXISTS " + TABLE3 + "(dish_id INT(4) , ing_id INT(4))");
        db.execSQL("INSERT INTO " + TABLE1 + "(ing_id, ing_name, ing_category) VALUES (1, 'Onion', 'Vegetable')");
        db.execSQL("INSERT INTO " + TABLE1 + "(ing_id, ing_name, ing_category) VALUES (2, 'Garlic', 'Vegetable')");
        db.execSQL("INSERT INTO " + TABLE1 + "(ing_id, ing_name, ing_category) VALUES (3, 'Tomato', 'Vegetable')");
        db.execSQL("INSERT INTO " + TABLE1 + "(ing_id, ing_name, ing_category) VALUES (4, 'Potato', 'Vegetable')");
        db.execSQL("INSERT INTO " + TABLE1 + "(ing_id, ing_name, ing_category) VALUES (5, 'Carrot', 'Vegetable')");
        db.execSQL("INSERT INTO " + TABLE1 + "(ing_id, ing_name, ing_category) VALUES (6, 'Broccoli', 'Vegetable')");
        db.execSQL("INSERT INTO " + TABLE1 + "(ing_id, ing_name, ing_category) VALUES (7, 'Corn', 'Vegetable')");
        db.execSQL("INSERT INTO " + TABLE1 + "(ing_id, ing_name, ing_category) VALUES (8, 'Spinach', 'Vegetable')");
        db.execSQL("INSERT INTO " + TABLE1 + "(ing_id, ing_name, ing_category) VALUES (9, 'Mushroom', 'Vegetable')");
        db.execSQL("INSERT INTO " + TABLE1 + "(ing_id, ing_name, ing_category) VALUES (10, 'GreenBeans', 'Vegetable')");
        db.execSQL("INSERT INTO " + TABLE1 + "(ing_id, ing_name, ing_category) VALUES (11, 'Butter', 'Dairy')");
        db.execSQL("INSERT INTO " + TABLE1 + "(ing_id, ing_name, ing_category) VALUES (12, 'Egg', 'Dairy')");
        db.execSQL("INSERT INTO " + TABLE1 + "(ing_id, ing_name, ing_category) VALUES (13, 'Milk', 'Dairy')");
        db.execSQL("INSERT INTO " + TABLE1 + "(ing_id, ing_name, ing_category) VALUES (14, 'Yogurt', 'Dairy')");
        db.execSQL("INSERT INTO " + TABLE1 + "(ing_id, ing_name, ing_category) VALUES (15, 'Cream', 'Dairy')");
        db.execSQL("INSERT INTO " + TABLE1 + "(ing_id, ing_name, ing_category) VALUES (16, 'Mozarella', 'Dairy')");
        db.execSQL("INSERT INTO " + TABLE1 + "(ing_id, ing_name, ing_category) VALUES (17, 'Cheddar', 'Dairy')");
        db.execSQL("INSERT INTO " + TABLE1 + "(ing_id, ing_name, ing_category) VALUES (18, 'WhippedCream', 'Dairy')");
        db.execSQL("INSERT INTO " + TABLE1 + "(ing_id, ing_name, ing_category) VALUES (19, 'Buttermilk', 'Dairy')");
        db.execSQL("INSERT INTO " + TABLE1 + "(ing_id, ing_name, ing_category) VALUES (20, 'CottageCheese', 'Dairy')");
        db.execSQL("INSERT INTO " + TABLE1 + "(ing_id, ing_name, ing_category) VALUES (21, 'Lemon', 'Fruits')");
        db.execSQL("INSERT INTO " + TABLE1 + "(ing_id, ing_name, ing_category) VALUES (22, 'Apple', 'Fruits')");
        db.execSQL("INSERT INTO " + TABLE1 + "(ing_id, ing_name, ing_category) VALUES (23, 'Banana', 'Fruits')");
        db.execSQL("INSERT INTO " + TABLE1 + "(ing_id, ing_name, ing_category) VALUES (24, 'Strawberry', 'Fruits')");
        db.execSQL("INSERT INTO " + TABLE1 + "(ing_id, ing_name, ing_category) VALUES (25, 'Orange', 'Fruits')");
        db.execSQL("INSERT INTO " + TABLE1 + "(ing_id, ing_name, ing_category) VALUES (26, 'Pineapple', 'Fruits')");
        db.execSQL("INSERT INTO " + TABLE1 + "(ing_id, ing_name, ing_category) VALUES (27, 'Coconut', 'Fruits')");
        db.execSQL("INSERT INTO " + TABLE1 + "(ing_id, ing_name, ing_category) VALUES (28, 'Grape', 'Fruits')");
        db.execSQL("INSERT INTO " + TABLE1 + "(ing_id, ing_name, ing_category) VALUES (29, 'Guava', 'Fruits')");
        db.execSQL("INSERT INTO " + TABLE1 + "(ing_id, ing_name, ing_category) VALUES (30, 'Mango', 'Fruits')");
        db.execSQL("INSERT INTO " + TABLE1 + "(ing_id, ing_name, ing_category) VALUES (31, 'Rice', 'Baking & Grains')");
        db.execSQL("INSERT INTO " + TABLE1 + "(ing_id, ing_name, ing_category) VALUES (32, 'Pasta', 'Baking & Grains')");
        db.execSQL("INSERT INTO " + TABLE1 + "(ing_id, ing_name, ing_category) VALUES (33, 'Flour', 'Baking & Grains')");
        db.execSQL("INSERT INTO " + TABLE1 + "(ing_id, ing_name, ing_category) VALUES (34, 'Bread', 'Baking & Grains')");
        db.execSQL("INSERT INTO " + TABLE1 + "(ing_id, ing_name, ing_category) VALUES (35, 'BreadCrumbs', 'Baking & Grains')");
        db.execSQL("INSERT INTO " + TABLE1 + "(ing_id, ing_name, ing_category) VALUES (36, 'BakingSoda', 'Baking & Grains')");
        db.execSQL("INSERT INTO " + TABLE1 + "(ing_id, ing_name, ing_category) VALUES (37, 'BakingPowder', 'Baking & Grains')");
        db.execSQL("INSERT INTO " + TABLE1 + "(ing_id, ing_name, ing_category) VALUES (38, 'Yeast', 'Baking & Grains')");
        db.execSQL("INSERT INTO " + TABLE1 + "(ing_id, ing_name, ing_category) VALUES (39, 'Buiscuits', 'Baking & Grains')");
        db.execSQL("INSERT INTO " + TABLE1 + "(ing_id, ing_name, ing_category) VALUES (40, 'Cornflour', 'Baking & Grains')");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int i, int i1) {

    }
}

This is my cursor code:

import android.content.Intent;
import android.database.Cursor;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;


public class main_Screen extends AppCompatActivity {
DatabaseHelper mydb;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main_screen);
    mydb = new DatabaseHelper(this);
    ImageView logo = (ImageView)findViewById(R.id.logo);
    logo.animate().alpha(1f).setDuration(500);
    TextView logotext = (TextView)findViewById(R.id.logotext);
    logotext.animate().alpha(1f).setDuration(500);
    Button b1=(Button)findViewById(R.id.b1);
    b1.setTranslationX(50f);
    b1.animate().translationXBy(-50f).setDuration(1000);
    Button b2=(Button)findViewById(R.id.b2);
    b2.setTranslationX(50f);
    b2.animate().translationXBy(-50f).setDuration(1500);
    Button b3=(Button)findViewById(R.id.b3);
    b3.setTranslationX(50f);
    b3.animate().translationXBy(-50f).setDuration(2000);
    TextView t1 = (TextView)findViewById(R.id.t1);
    t1.setTranslationX(-50f);
    t1.animate().translationXBy(50f).setDuration(500);
}
public void ingredientapp(View view)
{
    String name = mydb.getDatabaseName();
    Log.i("info", name);
    Log.i("Info","Button Pressed");
    Cursor c = mydb.getReadableDatabase().rawQuery("SELECT * FROM ingredients", null);
    int n = c.getCount();
    Log.i("info", Integer.toString(n));
    startActivity(new Intent(main_Screen.this,Main2Activity.class));
}
public void recipeapp(View view)
{

    Log.i("Info","Button Pressed");
    startActivity(new Intent(main_Screen.this,Main3Activity.class));
}
public void recommendapp(View view)
{
    Log.i("Info","Button Pressed");
    startActivity(new Intent(main_Screen.this,Main4Activity.class));
}

}

It is displaying the database name and the "Button Pressed" meassage but displaying the count as 0. Why? I have a project . Please suggest.

Remove the following line,

db = this.getWritableDatabase();

Then you should get count as 40. getWriteableDatabase is not supposed to be called in onCreate . The db parameter is already the writeable database passed to the onCreate method.

Your onCreate() is incorrect. You cannot call getWritableDatabase() there since it would trigger onCreate() recursively and throw an exception.

Since you're not seeing that exception, this version of onCreate() with the inserts is not executed at all. I guess you've edited it after running an earlier version of your app that already created the database for you. See When is SQLiteOpenHelper onCreate() / onUpgrade() run?

Remove the recursive getWritableDatabase() call and use the SQLiteDatabase given you as a parameter. You can uninstall and reinstall your app then to recreate the database.

  1. Uninstall the app from the device/emulator (so that the db will be deleted)
  2. Remove db = this.getWritableDatabase(); from onCreate()
  3. Run the app again (now onCreate() will be called again)

If the db exists onCreate() is not invoked

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM