连接两列和搜索结果

时间:2012-08-10 07:11:15

标签: android sqlite

我想在sqlite中连接两列并在concat列上执行搜索。这可能吗?

Cursor mCursor = db.rawQuery(
                "SELECT user_id,firstname,lastname, \"firstname\" || \"lastname\" AS val FROM "
                        + TABLE_DIRECTORY + " where val ='%" + text + "%'",
                null);

使用sqlite和android

3 个答案:

答案 0 :(得分:2)

试试这个

   Cursor mCursor = db.rawQuery(
        "SELECT user_id,firstname,lastname, \"firstname\" || \"lastname\" AS val FROM "
                + TABLE_DIRECTORY + " where \"firstname\" || \"lastname\" LIKE '%" + text + "%'",
        null);

答案 1 :(得分:1)

cursor = db.rawQuery("SELECT _id, firstName, lastName, FROM TABLE_DIRECTORY WHERE firstName || ' ' || lastName LIKE ?", 
                    new String[]{"%" + val + "%"});

答案 2 :(得分:0)

我没试过,但只要我记得你的SQL中有错误:

Cursor mCursor = db.rawQuery(
        "SELECT user_id,firstname,lastname, \"firstname\" || \"lastname\" AS val FROM "
                + TABLE_DIRECTORY + " where val LIKE '%" + text + "%'",
        null);

与通配符的文本比较使用LIKE,而不是=