以+运算符开头的字符串与数据库字符串值不匹配

时间:2016-04-30 11:58:10

标签: android sqlite

我想将传入的短信号码与数据库保存的号码进行比较。如果它们相同则显示Toast。它适用于简单的数字,如9或任何其他数字,但不适用于 + 运算符。

当我们从一个号码收到一条消息时,它包含国家代码,例如巴基斯坦+ 92XXXXXXXXXX。我已将其保存到数据库中,但是当收到的邮件数量相同时,它就不匹配了。

我在模拟器上检查过,它可以很好地处理来自9等的简单数字,但不能使用+运算符。肾脏的帮助。

在日志中找到错误: 请求索引0,大小为0

我的数据库tabe:

// Create statement for block_list table
    public static final String CREATE_TABLE_BLOCK_LIST = "CREATE TABLE " + TABLE_BLOCK_LIST
            + "(" + ID_BLOCK_LIST + " INTEGER PRIMARY KEY AUTOINCREMENT DEFAULT 0,"
            + ORIGINATING_ADDRESS_BLOCKlIST + " TEXT,"
            + TIME_BLOCK_LIST + " TEXT)";  

比较声明:

// Select all Blocked Numbers
    public Cursor compareToBlockList(String originatingAddress){
        Cursor cursor = null;
        try {
            SQLiteDatabase db = this.getWritableDatabase();
            cursor = db.rawQuery("SELECT " + ORIGINATING_ADDRESS_BLOCKlIST + " FROM " + TABLE_BLOCK_LIST
                    + " WHERE " + ORIGINATING_ADDRESS_BLOCKlIST + "=" + originatingAddress, null);
        } catch (Exception e){
            Log.d("compare to Block list", " is" + e.getMessage());
        }
        return  cursor;
    }

调用函数:

Cursor cursor = dbManagement.compareToBlockList(originatingAddress);
                        cursor.moveToFirst();
                        String number = cursor.getString(cursor.getColumnIndex(dbManagement.ORIGINATING_ADDRESS_BLOCKlIST));

                        Toast.makeText(context, "found", Toast.LENGTH_LONG).show();

数据库值:

database

在日志中找到错误: 请求索引0,大小为0

1 个答案:

答案 0 :(得分:1)

尝试改为 cursor = db.rawQuery("SELECT " + ORIGINATING_ADDRESS_BLOCKlIST + " FROM " + TABLE_BLOCK_LIST + " WHERE " + ORIGINATING_ADDRESS_BLOCKlIST + "= '" + originatingAddress + "'", null);

(用撇号包围)