我是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));
}
}
它显示数据库名称和“ Pressed Pressed”消息,但显示计数为0。为什么? 我有一个项目。请提出建议。
答案 0 :(得分:1)
删除以下行
db = this.getWritableDatabase();
这时您应该得到40。getWriteableDatabase
中不应调用onCreate
。 db
参数已经是传递给onCreate
方法的可写数据库。
答案 1 :(得分:0)
您的onCreate()
不正确。您不能在那里调用getWritableDatabase()
,因为它会递归触发onCreate()
并引发异常。
由于您没有看到该异常,因此完全不会执行带有插入内容的onCreate()
这个版本。我猜您是在运行已经为您创建数据库的应用程序的早期版本后对其进行编辑的。参见When is SQLiteOpenHelper onCreate() / onUpgrade() run?
删除递归getWritableDatabase()
调用,并使用给定的SQLiteDatabase
作为参数。您可以卸载并重新安装您的应用,然后重新创建数据库。
答案 2 :(得分:0)
db = this.getWritableDatabase();
移除onCreate()
onCreate()
)如果数据库存在onCreate()
不会被调用