gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 10:55:50 24 4
gpt4 key购买 nike

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

}

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

最佳答案

删除以下行,

db = this.getWritableDatabase();

那么您应该得到 40 的计数。 getWriteableDatabase 不应该在 onCreate 中调用。 db 参数已经是传递给 onCreate 方法的可写数据库。

关于java - 为什么我的光标计数为0?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51761200/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com