Android的SQLite的选择的args []为int值 [英] Android Sqlite selection args[] with int values

查看:122
本文介绍了Android的SQLite的选择的args []为int值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我DBHelper的getEvents方法,这行code。

Have this line of code in the getEvents method of my DBHelper.

    int year, month, day;
    String[] columns = new String[] { KEY_EVENTNAME, KEY_DESCRIPTION,
            KEY_HOUR, KEY_MINUTE, KEY_DAY, KEY_MONTH, KEY_YEAR,
            KEY_REMINDER };
    Cursor c = database.query(DATABASE_TABLE, columns, 
            KEY_YEAR + "=?"+ " AND " + KEY_MONTH + "=?" + " AND "+ KEY_DAY + "=?",
            new String[] {String.valueOf(year), String.valueOf(month), String.valueOf(day)}, 
            null, null, KEY_MONTH + " AND "
            + KEY_DAY);

它总是没有返回值。当我删除以下

It always returns nothing. When I remove the following

    int year, month, day;
    String[] columns = new String[] { KEY_EVENTNAME, KEY_DESCRIPTION,
            KEY_HOUR, KEY_MINUTE, KEY_DAY, KEY_MONTH, KEY_YEAR,
            KEY_REMINDER };
    Cursor c = database.query(DATABASE_TABLE, columns, 
            KEY_YEAR + "=?",
            new String[] {String.valueOf(year)}, 
            null, null, KEY_MONTH + " AND "
            + KEY_DAY);

运行正常。什么是与我的code中的问题?现在看来似乎不接受多个值where子句。 任何一个可以帮助我?谢谢你。

it runs correctly. What's the problem with my code? It seems like it doesn't accept multiple values as where clause. Can any one help me with this? Thanks.

推荐答案

答案是在标题:您想通过整数值到查询,但实际上你到底是什么了是的字符串值。

The answer is in the title: you are trying to pass integer values into the query, but what you actually end up with are string values.

这是Android数据库API中的一个可怕的设计缺陷;您可以使用参数仅供字符串。

This is a horrible design bug in the Android database API; you can use parameters only for strings.

整数数字不具有字符串值将有格式和SQL注入的问题,所以你可以直接插入数字到SQL EX pression:

Integer numbers do not have the formatting and SQL injection problems that string values would have, so you can just insert the numbers directly into the SQL expression:

Cursor c = database.query(
    DATABASE_TABLE, columns, 
    KEY_YEAR  + "=" + year  + " AND " +
    KEY_MONTH + "=" + month + " AND " +
    KEY_DAY   + "=" + day,
    null, null, null,
    KEY_MONTH + "," + KEY_DAY);

(和排序依据语法错了。)

这篇关于Android的SQLite的选择的args []为int值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆