簡體   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