Android SQLite无法绑定参数

时间:2013-07-07 22:40:04

标签: android-sqlite

我正在尝试对我的数据库进行简单查询,其中为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和常规查询来完成此操作,但没有成功。

1 个答案:

答案 0 :(得分: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();