2011-03-11 56 views
5

我想使用內置的SQLite函數,我不知道如何格式化查詢。如何格式化WHERE子句和?,可以去那裏:如何格式化WHERE子句和'?'在SQLite查詢?

column [] = {"name"}; 
selectionArg [] = {"john}; 
Select name from mytable where id = "john" 

db.query(mytable, column, id = ?, selectionArg, null,null,null); 

我不是在那裏我可以測試它,但我想要得到它在第一時間。我不確定?是否需要"?"或者如果是id = + "?"等等。我似乎無法找到任何格式的例子。

回答

18

對於查詢:

select name from mytable where id = "john" 

使用

(SQliteDatabase) db.query(
    "mytable" /* table */, 
    new String[] { "name" } /* columns */, 
    "id = ?" /* where or selection */, 
    new String[] { "john" } /* selectionArgs i.e. value to replace ? */, 
    null /* groupBy */, 
    null /* having */, 
    null /* orderBy */ 
); 
+0

我希望你不要我記得我編輯了你的答案,因爲它基本上是更好的答案;) – user634618 2011-03-11 17:37:44

+0

沒問題...謝謝! – Karan 2011-03-11 18:31:40

2

不得使用周圍的問號報價

例如:

db.query(
      true, 
      "tablename", 
      new String[] { 
       "col1", 
       "col2" 
      }, 
      "col1 = ?", 
      new String[]{ "value" }, 
      null, 
      null, 
      null, 
      null 
     );