在SQLite数据库查询中设置多个Selection args [英] Setting multiple Selections args in SQLite database query

查看:197
本文介绍了在SQLite数据库查询中设置多个Selection args的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将如何查询多个选择arg?例如,这是我数据库的格式

How would i go about querying more than one selection arg? For example here is how my database is formatted

这是我只用一个选择参数arg进行搜索的代码:

Here is the code i used to search with just one seletion arg:

public Cursor getType(String type) throws SQLException 
{
    Cursor mCursor =
            db.query(true, DB_TABLE, new String[] {
                    KEY_ROWID,
                    KEY_ALCOHOL, 
                    KEY_TYPE,
                    KEY_BRAND,
                    KEY_PRICE
                    }, 
                    KEY_TYPE + "=?", 
                    new String[] { type },
                    null, 
                    null, 
                    null, 
                    null);
    if (mCursor != null) {
        mCursor.moveToFirst();
    }
    return mCursor;
}

但这仅按KEY_TYPE搜索,我将如何设置它使其按KEY_TYPE,KEY_ALCOHOL和KEY_PRICE搜索?

But this only searches by KEY_TYPE, how would I set it so it searches by KEY_TYPE, KEY_ALCOHOL, and KEY_PRICE?

推荐答案

想通了!感谢@Rajendra给了我一些代码!您只能将字符串添加到选择参数中,所以我这样做了:

Figured it out!! Thanks to @Rajendra for giving me some code to build off of! You can only add Strings into the selections args so i did this:

public Cursor getTest(String alcohol, String type, long price) throws SQLException 
{
    Cursor mCursor =
            myDataBase.query(true, DB_TABLE, new String[] {
                    KEY_ROWID,
                    KEY_ALCOHOL, 
                    KEY_TYPE,
                    KEY_BRAND,
                    KEY_PRICE
                    }, 
                    KEY_ALCOHOL + "=?" + " AND " + KEY_TYPE + "=?" + " AND " + KEY_PRICE + "<=" + price,
                    new String[] { alcohol, type},
                    null, 
                    null, 
                    null, 
                    null);
    if (mCursor != null) {
        mCursor.moveToFirst();
    }
    return mCursor;
}

它有效!!

这篇关于在SQLite数据库查询中设置多个Selection args的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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