SQlite查询问题,导致强制关闭

时间:2010-12-06 20:37:07

标签: android sqlite spinner

我正在编写我的第一个应用程序,而且我也在尝试合并一个数据库,而我真的很挣扎。我想要做的是有多个微调器,那些微调器从数据库中获取数据。每个微调器都会执行不同的查询。

到目前为止,这就是我所拥有的:DBUtility

import android.content.Context;

import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; import android.database.sqlite.SQLiteDatabase.CursorFactory; import android.widget.SimpleCursorAdapter;

公共类DbUtility {

static final String DB_NAME="food";
static final String BEEF_TABLE="beef";
static final String CHICKEN_TABLE="chicken";

SQLiteDatabase db=null; 
Context context;

public static class DatabaseHelper extends SQLiteOpenHelper
{
    public DatabaseHelper(Context context, String name, CursorFactory factory, int version) 
    {
        super(context, name, factory, version);
        // TODO Auto-generated constructor stub
    }
    @Override
    public void onCreate(SQLiteDatabase db) 
    {           
        db.execSQL("CREATE TABLE IF NOT EXISTS "+BEEF_TABLE+" (_id INT PRIMARY KEY, cookTime INT PRIMARY KEY ,name VARCHAR);");
        db.execSQL("INSERT INTO "+BEEF_TABLE+" values(5000,'Skirt Steak');");
        db.execSQL("INSERT INTO "+BEEF_TABLE+" values(10000,'Flank Steak');");
        db.execSQL("INSERT INTO "+BEEF_TABLE+" values(15000,'Filet Mignon');");
        db.execSQL("CREATE TABLE IF NOT EXISTS "+CHICKEN_TABLE+" (_id INT PRIMARY KEY, cookTime INT PRIMARY KEY ,name VARCHAR);");
        db.execSQL("INSERT INTO "+CHICKEN_TABLE+" values(5000,'Breast');");
        db.execSQL("INSERT INTO "+CHICKEN_TABLE+" values(10000,'Wings');");
        db.execSQL("INSERT INTO "+CHICKEN_TABLE+" values(15000,'Burger');");
    }
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {  
    }   
}
public DbUtility(Context context) {
    this.context=context;
}
public SimpleCursorAdapter getBeefAdapter()
{
    DatabaseHelper dbhelper=new DatabaseHelper(this.context, DB_NAME, null, 1);
    db=dbhelper.getWritableDatabase();

    Cursor beefCursor=db.rawQuery("SELECT * FROM "+BEEF_TABLE, null);
    while(!beefCursor.isLast())
    beefCursor.moveToNext(); //I don't understand why but it's necessary (alternative call c.getCount() )
    String[] from=new String[1];
    from[0]="name";
    int[] to=new int[1];
    to[0]=android.R.id.text1;
    SimpleCursorAdapter beefAdapter=new SimpleCursorAdapter(this.context, android.R.layout.simple_spinner_item, beefCursor, from, to);
    beefAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    db.close();
    return beefAdapter; 
}   
public SimpleCursorAdapter getChickenAdapter()
{
    DatabaseHelper dbhelper=new DatabaseHelper(this.context, DB_NAME, null, 1);
    db=dbhelper.getWritableDatabase();

    Cursor chickenCursor=db.rawQuery("SELECT * FROM "+CHICKEN_TABLE, null);//If I change this to BEEF_TABLE it doesn't force close...
    while(!chickenCursor.isLast())
    chickenCursor.moveToNext(); //I don't understand why but it's necessary (alternative call c.getCount() )
    String[] from=new String[1];
    from[0]="name";
    int[] to=new int[1];
    to[0]=android.R.id.text1;
    SimpleCursorAdapter chickenAdapter=new SimpleCursorAdapter(this.context, android.R.layout.simple_spinner_item, chickenCursor, from, to);
    chickenAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    db.close();
    return chickenAdapter;  
}   

}

我也有我的鸡肉活动,这是我的纺纱师所在的地方:

package com.tsilo.grillbuddy;

import android.app.Activity; import android.os.Bundle; import android.widget.SimpleCursorAdapter; import android.widget.Spinner;

公共类ChickenActivity扩展Activity {     /** 在第一次创建活动时调用。 * /     @覆盖     public void onCreate(Bundle savedInstanceState){         super.onCreate(savedInstanceState);         的setContentView(R.layout.spinner);

    DbUtility db=new DbUtility(this);

    SimpleCursorAdapter beefAdapter=db.getBeefAdapter();
    Spinner beefSpinner=(Spinner)this.findViewById(R.id.spinner);
    beefSpinner.setAdapter(beefAdapter);

    SimpleCursorAdapter chickenAdapter=db.getChickenAdapter();
    Spinner chickenSpinner=(Spinner)this.findViewById(R.id.spinner2);
    chickenSpinner.setAdapter(chickenAdapter);            
}

}

这是我注意到的:
如果我更改了我的getChickenAdapter()以便它查询BEEF_TABLE,它就可以工作,只有当我将它切换到CHICKEN_TABLE时才会强行关闭。


我的DDMS错误:

12-06 16:03:12.473:INFO / Database(11856):sqlite返回:错误代码= 1,msg =没有这样的表:chicken 12-06 16:03:12.473:DEBUG / AndroidRuntime(11856):关闭VM 12-06 16:03:12.473:WARN / dalvikvm(11856):threadid = 1:线程退出,未捕获异常(group = 0x4001d7f0) 12-06 16:03:12.483:ERROR / AndroidRuntime(11856):致命异常:主要 12-06 16:03:12.483:ERROR / AndroidRuntime(11856):java.lang.RuntimeException:无法启动活动ComponentInfo {com.tsilo.grillbuddy / com.tsilo.grillbuddy.ChickenActivity}:android.database.sqlite.SQLiteException :没有这样的表:鸡:,编译时:SELECT * FROM chicken

1 个答案:

答案 0 :(得分:1)

我想知道在运行应用时是否正在调用DatabaseHelper.onCreate()。如果您不确定,那么在那里设置断点并查看。如果没有被调用,那么您的数据库的旧版本正在使用,也许该版本没有“鸡”表。尝试卸载应用程序并再次运行它(它将自动安装。)

如果结果是问题,您可能希望将任何将来的CREATE TABLE IF NOT EXISTS调用移动到将从onCreateonUpgrade调用的公共方法,并确保在您这样做时增加数据库版本号。