检查数据库中的列字符串是否是sqlite中查询的子字符串 [英] Check if column string in database is a substring of a query in sqlite

查看:194
本文介绍了检查数据库中的列字符串是否是sqlite中查询的子字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当前的Sqlite语法可以检查查询是否是列字符串的子字符串. 我可以做一个黑客,以在%后面添加一个%?在 https://stackoverflow.com/a/5752671/908821

the current Sqlite syntax can check if a query is a substring of a column string. and I can do a hack to add a % behind the ? described in https://stackoverflow.com/a/5752671/908821

final String whereClause = DbHelper.COLUMN_URL + " LIKE ? " ;  

有没有我可以做相反的语法?我想检查列字符串是否是查询的子字符串.

is there a syntax where i can do the opposite? I want to check if the column string is a substring of a query.

以下代码似乎无效.

final String whereClause = "? LIKE "  +  DbHelper.COLUMN_URL ;  

以下是我的其余代码:

String[] whereArg = {url};

ContentValues values = new ContentValues();
values.put(DbHelper.COLUMN_LAST_URL, lastUrl);

database.updateWithOnConflict(DbHelper.TABLE_BOOKMARK,
                values,
                whereClause,
                whereArg,
                SQLiteDatabase.CONFLICT_IGNORE);

如果基本网址是查询的子字符串,我正在尝试更新"LastUrl"列.

I am trying to update "LastUrl" column if the base url is a subString of a query.

一些我喜欢的例子

Base Url (in database)         Query
-----------------------------------------------------------------
http://example.com/path        http://example.com/path/path2
http://example.com/path?id=0   http://example.com/path?id=0&x=xx 

推荐答案

LIKE运算符检查左侧的值是否与右侧的模式匹配.因此,只需将值放在左侧,将模式放在右侧. 如果模式需要数据库中没有的%,则必须附加它:

The LIKE operator checks whether the value on the left matches the pattern on the right. So just put the value on the left, and the pattern on the right. If the pattern needs a % that is not in the database, you have to append it:

SELECT ... WHERE ? LIKE LastUrl || '%' ...

这篇关于检查数据库中的列字符串是否是sqlite中查询的子字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆