0

我使用数据库来加密/解密 sqlcipher。现在我编写函数来检查这张卡是否存在于表卡中。当我调试我的方法时,总是返回 false。

public boolean checkIfExistCard(String numberCard, String expirationDate) {
    net.sqlcipher.database.SQLiteDatabase db = this.getReadableDatabase(DATABASE_PASSWORD);
    Cursor cursor = db.query(CardColumn.CardEntry.TABLE_CARD,
            new String[]{CardColumn.CardEntry.CARD_NUMBER, CardColumn.CardEntry.CARD_EXPIRATION_DATA},
            CardColumn.CardEntry.CARD_NAME + " = ? and " + CardColumn.CardEntry.CARD_EXPIRATION_DATA + " = ?",
            new String[]{numberCard, expirationDate},
            null, null, null, null);

    if (cursor != null) {
        if (cursor.getCount() > 0) {
            cursor.close();
            db.close();
            return true;
        }
        cursor.close();
        db.close();
    }
    return false;
}
4

3 回答 3

0
 public boolean checkIfExistCard(String numberCard, String expirationDate) {
    boolean isAvailable=false;
    net.sqlcipher.database.SQLiteDatabase db = this.getReadableDatabase(DATABASE_PASSWORD);
    Cursor cursor = db.query(CardColumn.CardEntry.TABLE_CARD,
            new String[]{CardColumn.CardEntry.CARD_NUMBER, CardColumn.CardEntry.CARD_EXPIRATION_DATA},
            CardColumn.CardEntry.CARD_NAME + " = ? and " + CardColumn.CardEntry.CARD_EXPIRATION_DATA + " = ?",
            new String[]{numberCard, expirationDate},
            null, null, null, null);

    if (cursor != null) {
        if (cursor.getCount() > 0) {

            isAvailable=true;
        }
        else {

            isAvailable=false;
        }
        cursor.close();
        db.close();
    }
    return isAvailable;
}
于 2017-09-18T10:35:10.950 回答
0

也许您在查询中错过了一个空格。在这一行:

CardColumn.CardEntry.CARD_NAME + " = ? and " + CardColumn.CardEntry.CARD_EXPIRATION_DATA + " = ?",

最后之后?符号在“符号之前放置一个空格。

于 2017-09-18T10:44:19.813 回答
0

我解决了我的问题:) 我在 Cursor 中犯的错误应该是 CardColumn.CardEntry.CARD_NUMBER 而不是 CardColumn.CardEntry.CARD_NAME 它的作品:)

    Cursor cursor = db.query(CardColumn.CardEntry.TABLE_CARD,
            new String[]{CardColumn.CardEntry.CARD_NUMBER, CardColumn.CardEntry.CARD_EXPIRATION_DATA},
            CardColumn.CardEntry.CARD_NUMBER + " = ? and " + CardColumn.CardEntry.CARD_EXPIRATION_DATA + " = ?",
            new String[]{numberCard, expirationDate},
            null, null, null, null);
于 2017-09-26T10:36:27.177 回答