SQLite Query不接受where子句

时间:2013-11-04 01:05:46

标签: android sqlite where-clause

我有一个我创建的数据库和一个使用列表视图的列表类。我遇到的问题是,当我想从列表中选择一个项目时,它会崩溃并给我错误:11-03 19:57:16.064:E / AndroidRuntime(27491):致命异常:主要 11-03 19:57:16.064:E / AndroidRuntime(27491):android.database.CursorIndexOutOfBoundsException:请求索引0,大小为0

我已经打印到日志中以查看光标是否获取任何信息并返回零,所以我知道它不是。我尝试了很多形式的where子句,并返回相同的错误。我们将非常感谢您提供的任何帮助。

这是数据库代码:

import java.util.ArrayList;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;


public class DataBaseWish extends SQLiteOpenHelper
{
    public static final int VERSION = 1;
    public static final String TABLE_NAME = "WishList";
    public static final String DBNAME = "wishList.sqlite";
    public static final String ID = "id"; 
    public static final String BOOK = "book";
    static SQLiteDatabase db1;

    public DataBaseWish(Context context)
    {
        super(context, DBNAME, null, VERSION);
        // TODO Auto-generated constructor stub
    }


    @Override
    public void onCreate(SQLiteDatabase db1)
    {
        // TODO Auto-generated method stub
        createDatabase(db1);
    }
    private void createDatabase(SQLiteDatabase db1)
    {
        db1.execSQL("create table " + TABLE_NAME + "(" + ID + " integer primary key autoincrement not null, " + BOOK + " text " + ");");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db1, int oldVersion, int newVersion) 
    {
        // TODO Auto-generated method stub
        db1.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
        onCreate(db1);
    }
    public void Insert(String book)
    {
        ContentValues values = new ContentValues();
        values.put(BOOK, book);
        db1.insert(TABLE_NAME, null, values);
    }
    public void deleteItem(String book)
    {
        db1.delete(TABLE_NAME, ID, new String[] {book});
    }
    public ArrayList<String> get()
    {
        String[] column = new String[] {BOOK};
        Cursor c = db1.query(TABLE_NAME, column, null, null, null, null, null);
        ArrayList<String> result = new ArrayList<String>();
        c.moveToFirst();
        while(!c.isAfterLast())
        {
            result.add(c.getString(0));
            c.moveToNext();
        }
        return result;
    }
    public String getBook(Cursor c)
    {
        return(c.getString(0));
    }
    public Cursor getID(String book)
    {
        String[] args = {book};
        String[] column = new String[] {BOOK};
        Log.w("Print",args[0]);
        return(db1.query(TABLE_NAME, column, "id='0'", null, null, null, null));
        //return(db1.rawQuery("SELECT id, book FROM WishList WHERE id="+ book, null));
    }
    public void close()
    {
        db1.close();
    }
    public void open()
    {
        db1 = this.getWritableDatabase();
    }
}

以下是我访问列表中选择点击的方式:

lv.setOnItemClickListener(new OnItemClickListener()
        {
            public void onItemClick(AdapterView<?> adapter, View view, int position, long id)
            {
                dbo.open();
                dbID = String.valueOf(id);
                Cursor c = dbo.getID(dbID);
                Log.w("whishlist", "Cursor size is:"+c.getCount());
                c.moveToFirst();
                item.setText(dbo.getBook(c));
                dbo.close();
            }
        });

1 个答案:

答案 0 :(得分:1)

ID列从1开始,而不是0,但在你的getID()方法中你硬编码检查id = 0,这就是返回的游标为空的原因。 顺便说一句,你可以通过查看Cursor的moveToFirst()方法返回的值来检查游标是否为空。如果为false,则光标为空。 注释行有效,但您必须删除select查询中的“id”列,否则getBook方法将失败,因为它将访问“id”列而不是“book”。