当前位置:  开发笔记 > 编程语言 > 正文

Android SQLite select*from table where name like%key%using prepared statements

如何解决《AndroidSQLiteselect*fromtablewherenamelike%key%usingpreparedstatements》经验,为你挑选了2个好方法。

我想使用预准备语句来防止在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 + "%"});

它给出了一个超出范围的绑定或列索引谢谢.



1> Digvesh Pate..:
    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);`



2> Anirudha Aga..:

尝试

Cursor cursor = database.rawQuery(sqlQuery, new String[]{"'%" + criterion + "%'", 
   "'%" + criterion + "%'",
   "'%" + criterion + "%'",
   "'%" + criterion + "%'",
   "'%" + criterion + "%'"});

你之前和之后都缺少"'".

推荐阅读
罗文彬2502852027
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有