在SQLite和Android中使用列作为选择参数

时间:2018-03-04 03:07:15

标签: android sqlite

如何在查询中使用列。

我在SQliteStudio中使用原始SQLite命令完成了这项工作。

select _id, docholder, notes, docnumber, validto, doctype from documentslist where replaced <> 1 and validto < (((strftime('%s', 'now')*1000)) + (86400000 * warningin))

但Android Studio中的一个不起作用。

    private void showList(){
    mDbHelper = new DocumentsDbHelper(this);
    db = mDbHelper.getReadableDatabase();

    String[] projection = {
            DocumentEntry._ID,
            DocumentEntry.COLUMN_DOC_HOLDER,
            DocumentEntry.COLUMN_NOTES,
            DocumentEntry.COLUMN_DOC_NUMBER,
            DocumentEntry.COLUMN_VALID_TO,
            DocumentEntry.COLUMN_DOC_TYPE
    };


    String selection = DocumentEntry.COLUMN_EXPIRED + " <> ? AND " + DocumentEntry.COLUMN_VALID_TO + " < ";
    String[] selectionArg = {"1", String.valueOf(((System.currentTimeMillis()) + (86400000 * Integer.parseInt(DocumentEntry.COLUMN_WARN_IN))))};

    String setOrder = DocumentEntry.COLUMN_VALID_TO + " ASC";

    Cursor cursor = db.query(
            DocumentEntry.TABLE_NAME,
            projection,
            selection,
            selectionArg,
            null,
            null,
            setOrder
    );

1 个答案:

答案 0 :(得分:1)

我相信您的问题可能是您可能尝试将字符串转换为指定Integer.parseInt(DocumentEntry.COLUMN_WARN_IN)的整数(除非DocumentEntry.COLUMN_WARN_IN解析为整数,否则会出现它可以解决警告

此外,您已经省略了第二个参数的包含,因此无法包含,并且可能会导致参数不匹配问题,因为您提供了两个参数,但只要求使用一个参数。

我建议使用: -

String selection = DocumentEntry.COLUMN_EXPIRED + " <> ? AND " + DocumentEntry.COLUMN_VALID_TO + " <  (((strftime('%s', 'now')*1000)) + (86400000 * warningin))";
String[] selectionArg = {"1"};

或者更准确地说,假设DocumentEntry.COLUMN_WARN_IN解析为 warningin ,使用: -

String selection = DocumentEntry.COLUMN_EXPIRED + 
    " <> ? AND " + 
    DocumentEntry.COLUMN_VALID_TO + 
    " <  (((strftime('%s', 'now')*1000)) + (86400000 * " +
    DocumentEntry.COLUMN_WARN_IN + 
    "))";
String[] selectionArg = {"1"};
  • 注意 这是原则上的代码,尚未经过测试,因此可能存在输入错误