2013-12-19 51 views
0

我正在使用查询搜索,但我不明白为什么一个代码是工作,另一个不工作?!为什么rawQuery工作并且查询不起作用?

这是工作:

  String selectQuery = "SELECT * FROM " + TABLE_Soras 
           +" WHERE "+KEY_SoraName+" LIKE '%"+soraName+"%'"; 
      Cursor cursor = db.rawQuery(selectQuery,null); 

但这得不到任何结果:因为在第二种情况下%不追加到值

Cursor cursor = db.query(TABLE_Soras, new String[]{ 
      KEY_SORA_NO, KEY_SoraName}, KEY_SoraName + "=?", 
      new String[]{soraName}, null, null, null, null);*/ 
+0

你是什么意思不工作? –

+1

第二个没有得到任何结果。 – amorenew

+0

基本上它们不是一样的查询! – Pavlos

回答

2

并且您没有将LIKE子句添加到查询中。您需要使用此:

KEY_SoraName + " like ?", new String[] {"%" +soraName + "%"} ... 
+1

谢谢这是一个正确的答案 – amorenew

0

试试这个

Cursor cursor = db.query(TABLE_QuranSoras, new String[]{ 
      KEY_SORA_NO, KEY_SoraName}, KEY_SoraName+" LIKE '%"+soraName+"%'", null, null, null, null); 
+1

_thanks先生Andrian_ – amorenew

0
Cursor cursor = your_database.query(MY_TABLE, new String[] {"first_column","second_column"},your_column + " LIKE '%"+your_criteria+"%'", null, null, null, null); 

尝试是这样的,它可能会工作!

+0

这个查询将不会工作如问 – Andrain

+0

我错过了%,这就是为什么!我将编辑添加它! – Pavlos

+1

**非常感谢** – amorenew