S
S
Sergey Kamyshev2017-10-23 13:26:29
Java
Sergey Kamyshev, 2017-10-23 13:26:29

How to correctly formulate a sql query with two or more arguments (SQLite)?

There is a users table, which has a field name, tag and others (not related to this problem) . You need to display records only with the necessary tag (tag) and if you need to filter (find) the desired record from the proposed ones. How to write a sql query with two arguments?

final String tag =  intent.getStringExtra("tag");
final String[] selectionArgs = {tag};
            
userAdapter.setFilterQueryProvider(new FilterQueryProvider() {
@Override
 public Cursor runQuery(CharSequence constraint) {

         if (constraint == null || constraint.length() == 0) {

         return sqlHelper.database.rawQuery("select * from users where tag like ?selTag", null);
 //если не вводить текст показываются нужные записи                        
          }
            else {

              return sqlHelper.database.rawQuery("select * from users where tag like ?selTag and name like ?",
                        new String[] {"%" + constraint.toString() + "%"}); //не работает фильтрация 
                                                                                                           
// если с запроса убрать "tag like ?selTag" фильтрация успешно                                                                                                        
//осуществляется по всей таблице, не учитывая тег
                    }
                }
            });

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Gornostaev, 2017-10-23
@ser_cool_geek

Naturally, it does not work, but you use two parameters in the request, but you pass one argument. Add the second element to the arguments array.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question