Android SQLite Query Where and Where

北城以北 提交于 2019-11-30 09:44:14

问题


I need to return the ID value from the database where dan == table_column_one and where vrijeme == table_column_two. I don't know how to do this.

public static int returnID(String dan, int vrijeme){
        Cursor cursor;
        int IDRQ;
        cursor = db.query
                (
                        TABLE_NAME,
                        new String[] { TABLE_COLUMN_ID, TABLE_COLUMN_ONE, TABLE_COLUMN_TWO },                       
                        new String[] {TABLE_COLUMN_ONE + " like " + "'%" + dan + "%'", TABLE_COLUMN_TWO + " like " + "'%" + vrijeme + "%'"}, null, null, null, null
                );  
        cursor.moveToFirst();           
        IDRQ = cursor.getInt(0);
        return IDRQ;
    }   );  

This is how I tried, but of course, its wrong.


回答1:


In SQLiteDatabase.query() the selection comes in two parts. The where clause (a String) and the whereArgs (an array of String).

To add more than one condition to the where clause you can use AND or OR, just like && or || in Java.

A question mark in the where clause is bound to one of the Strings in the whereArgs array.

cursor = db.query(TABLE_NAME, new String[] { TABLE_COLUMN_ID, TABLE_COLUMN_ONE, TABLE_COLUMN_TWO },
TABLE_COLUMN_ONE + " LIKE ? AND " + TABLE_COLUMN_TWO + " LIKE ?",                           
new String[] {"%" + dan + "%", "%" + vrijeme + "%"},
null, null, null, null);


来源:https://stackoverflow.com/questions/15594716/android-sqlite-query-where-and-where

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