Setting multiple Selections args in SQLite database query

落爺英雄遲暮 提交于 2019-12-05 04:52:38

问题


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

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;
}

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


回答1:


This may help you

  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 + "=?" + " AND " + KEY_ALCOHOL + "=?" " AND " + KEY_PRICE + "=?", 

                new String[] { type,alcohol,price },
                null, 
                null, 
                null, 
                null);
if (mCursor != null) {
    mCursor.moveToFirst();
 }
 return mCursor;
}



回答2:


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;
}

And it works!!



来源:https://stackoverflow.com/questions/12422977/setting-multiple-selections-args-in-sqlite-database-query

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!