Android SQL query()不会替换问号

时间:2016-07-12 16:08:54

标签: android sqlite

查询中的问号不会被替换,我无法弄清楚原因 我总是得到Query(在调试时查看):

  

SELECT消息,时间戳

     

FROM chatMessage

     

在哪里chatId =?

     

ORDER BY timestamp ASC

     

LIMIT 5

在下面的代码中:

public ArrayList<String> queryLastMessages(String chatId, int count){
  SQLiteDatabase db = new DatabaseHelper(context).getReadableDatabase();

  String[] columns = {
            COLUMN_NAME_CHAT_MESSAGE,
            COLUMN_NAME_TIMESTAMP
    };

  String selection =  COLUMN_NAME_CHAT_ID + " = ?";

  String[] selectionArgs = {
            chatId
    };

  // ToDo: Ascending or Descending?
  Cursor cursor = db.query(
          TABLE_CHAT_MESSAGE,
          columns,
          selection,
          selectionArgs,
          null,
          null,
          COLUMN_NAME_TIMESTAMP + " ASC",
          String.valueOf(count));

  cursor.moveToFirst();

  ArrayList<String> arrayList = new ArrayList<>();
  if(cursor.getCount() != 0){
    while(!cursor.isAfterLast()){
      arrayList.add(cursor.getString(0));
      cursor.moveToNext();
    }
  }

  return arrayList;
}

任何想法我做错了什么?谢谢你的帮助。

1 个答案:

答案 0 :(得分:0)

试试这个:

SQLiteStatement stmt = db.compileStatement(query);
stmt.bindString(1, stringYouWantToReplaceQuestionMark);
stmt.execute();