0

我正在尝试对我的数据库进行简单查询,其中存储了一个 PendingIntent 的唯一标识号。允许我在需要时取消由 AlarmManager 设置的通知。值的插入工作正常,但我无法克服错误:

java.lang.IllegalArgumentException:无法在索引 1 处绑定参数,因为索引超出范围。该语句有 0 个参数。

数据库结构:公共类 DBAdapter {

private static final String TAG = "DBAdapter";

public static final String KEY_ROWID = "_id";
public static final String TASK = "task";
public static final String NOTIFICATION = "notification";


public static final int COL_ROWID = 0;
public static final int COL_TASK = 1;
public static final int COL_NOTIFICATION = 2;


public static final String[] ALL_KEYS = new String[] {KEY_ROWID, TASK, NOTIFICATION};

// DB info: it's name, and the table.
public static final String DATABASE_NAME = "TaskDB";
public static final String DATABASE_TABLE = "CurrentTasks";
public static final int DATABASE_VERSION = 3;

private static final String DATABASE_CREATE_SQL =
        "create table " + DATABASE_TABLE
                + " (" + KEY_ROWID + " integer primary key, "
                + TASK + " text not null, "
                + NOTIFICATION + " integer"
                + ");";

现在我创建了一个方法,可以根据需要从数据库中提取通知 ID,使用以下代码:

    public int getNotifID(long notifID){
    String[] x = {ALL_KEYS[2]};
    String[]args = new String[]{NOTIFICATION};
    String where = NOTIFICATION + "=" + notifID;
    int y = 0;
    //String select = "SELECT "+NOTIFICATION+" FROM "+DATABASE_TABLE+" WHERE "+notifID+"="+NOTIFICATION;
    //Cursor c = db.rawQuery(select,new String[]{});
    Cursor c = db.query(true,DATABASE_TABLE,x,Long.toString(notifID),args,null,null,null,null,null);
    if (c!= null && c.moveToFirst()){
        y = c.getInt(COL_NOTIFICATION);
    }

return y;
}

如您所见,我尝试使用 rawQuery 和常规查询来执行此操作,但没有成功。

4

1 回答 1

0

将您的原始查询重写为:

String select = "SELECT "+NOTIFICATION+" FROM "+DATABASE_TABLE+" WHERE "+NOTIFICATION+"=?";
Cursor c = db.rawQuery(select,new String[]{""+notifId});
if(c!=null && c.getCount()>0) {
   c.moveToFirst();
}
c.close();
于 2013-07-07T22:49:00.810 回答