不能使用类似的Andr​​oid应用程序条款 [英] cant use like clause in android app

查看:100
本文介绍了不能使用类似的Andr​​oid应用程序条款的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的一个数据库sqllite在一个Android应用程序 我想用一个像子句检索SM数据 例如:

I am working on a database with sqllite in an android app I want to retrieve sm data using a like clause ex:

Cursor c = myDB.query(MY_DATABASE_TABLE, null, " SongName LIKE '%"+"=?"+"%'" , 
           new String[]{match_str}, null, null,"SongHit  DESC");

这应该给所有SongName开始match_str,但它不是working.Why?

It should give all SongName starting with match_str but its not working.Why?

推荐答案

本:

" SongName LIKE '%"+"=?"+"%'"

最终会这样看,当SQL跨preTER看到它:

Will end up looking like this when the SQL interpreter sees it:

" SongName LIKE '%=?%'"

这将匹配包含文字的任何SongName=?我不认为这就像你想要的任何东西。

And that will match any SongName that contains a literal "=?" and I don't think that's anything like what you want.

A 匹配的字符在SQL像任何序列,它在本质上是一样的。* 的一个普通的前pression;所以,如果你想匹配的开始,那么你不想要一个领先的。此外,你的占位符必须是单引号之外否则将PTED作为一个问号,而不是一个占位符间$ P $。

A % matches any sequence of characters in an SQL LIKE, it is essentially the same as .* in a regular expression; so, if you want to match at the beginning then you don't want a leading %. Also, your ? placeholder needs to be outside the single quotes or it will be interpreted as a literal question mark rather than a placeholder.

您想要更多的东西是这样的:

You want something more like this:

String[] a = new String[1];
a[0]       = match_str + '%';
Cursor   c = myDB.rawQuery("SELECT * FROM songs WHERE SongName LIKE ?", a);

如果你想成为真正严格也不得不逃离 _ 符号内 match_str 以及但是这将作为练习留给读者。

If you wanted to be really strict you'd also have to escape % and _ symbols inside match_str as well but that's left as an exercise for the reader.

这篇关于不能使用类似的Andr​​oid应用程序条款的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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