我想使用预准备语句来防止在Android SQLite数据库上进行sql注入.但是,当查询包含Like
并使用时,rawquery崩溃Where name = ?
有没有办法在Android SQLite db中使用like和prepared语句?
这是查询:
sqlQuery = "SELECT * FROM " + TABLE_CALLS + " where " + CALLER_NAME + " like ? COLLATE NOCASE or " + CALLER_NBR + " like ? or " + CALLER_EXT + " like ?" + " or " + IS_OUTGOING + " like ? COLLATE NOCASE or " + TYPE + " like ? COLLATE NOCASE"; Cursor cursor = database.rawQuery(sqlQuery, new String[]{"%" + criterion + "%", "%" + criterion + "%","%" + criterion + "%","%" + criterion + "%","%" + criterion + "%"});
它给出了一个超出范围的绑定或列索引谢谢.
if (name.length() != 0) { name = "%" + name + "%"; } if (email.length() != 0) { email = "%" + email + "%"; } if (Phone.length() != 0) { Phone = "%" + Phone + "%"; } String selectQuery = " select * from tbl_Customer where Customer_Name like '" + name + "' or Customer_Email like '" + email + "' or Customer_Phone like '" + Phone + "' ORDER BY Customer_Id DESC"; Cursor cursor = mDb.rawQuery(selectQuery, null);`
尝试
Cursor cursor = database.rawQuery(sqlQuery, new String[]{"'%" + criterion + "%'", "'%" + criterion + "%'", "'%" + criterion + "%'", "'%" + criterion + "%'", "'%" + criterion + "%'"});
你之前和之后都缺少"'".