在SQLite数据库查询中设置多个选择参数

Mr.*_*tto 3 sqlite android multipleselection

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

我的数据库

这是我用来搜索只有一个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;
}
Run Code Online (Sandbox Code Playgroud)

但这仅按KEY_TYPE搜索,我如何设置它以便按KEY_TYPE,KEY_ALCOHOL和KEY_PRICE进行搜索?

Raj*_*dra 15

这可能对你有所帮助

  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;
}
Run Code Online (Sandbox Code Playgroud)


Mr.*_*tto 8

弄清楚了!!感谢@Rajendra为我提供了一些代码来构建!你只能在选择args中添加字符串,所以我这样做:

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;
}
Run Code Online (Sandbox Code Playgroud)

它的工作原理!!