Android的SQLite的查询在哪里,在哪里Android、SQLite

2023-09-08 00:01:41 作者:初遇|

我需要从数据库,在那里丹== table_column_one并在vrijeme == table_column_two返回ID值。我不知道如何做到这一点。

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.

推荐答案

在SQLiteDatabase.query()的选择有两个部分。在where子句(一个字符串)和whereArgs。(字符串的数组)

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

要添加多个条件,where子句可以使用,就像&功放;&安培; || 中的Java

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

在where子句中的问号绑定到字符串的whereArgs阵列中的一个。

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