繁体   English   中英

为什么我的光标计数为0?

[英]Why is my cursor count 0?

我是SQLite的新手,我正在构建一个应用程序,根据成分选择来建议配方,但是当我检查数据库是否包含任何记录时,它不显示任何记录并且将游标数显示为0。我将列数显示为3,但我没有得到行数。 请帮忙。

这是我的DatabaseHelper类:

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) {

    }
}

这是我的光标代码:

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));
}

}

它显示数据库名称和“按钮已按下”消息,但显示计数为0。为什么? 我有一个项目。 请提出建议。

删除以下行,

db = this.getWritableDatabase();

然后,您应该获得40。不应在onCreate调用getWriteableDatabase db参数已经是传递给onCreate方法的可写数据库。

您的onCreate()错误。 您不能在那里调用getWritableDatabase() ,因为它将递归触发onCreate()并引发异常。

由于您没有看到该异常,因此完全不执行带有插入内容的onCreate()版本。 我猜您是在运行已经为您创建数据库的应用程序的早期版本后对其进行编辑的。 请参阅何时运行SQLiteOpenHelper onCreate()/ onUpgrade()?

删除递归getWritableDatabase()调用,并使用给定的SQLiteDatabase作为参数。 您可以卸载并重新安装您的应用程序,然后重新创建数据库。

  1. 从设备/仿真器上卸载应用程序(这样数据库将被删除)
  2. 删除db = this.getWritableDatabase(); 来自onCreate()
  3. 再次运行该应用程序(现在将再次调用onCreate()

如果数据库存在,则不会调用onCreate()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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