Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to pass argument in custom SQL Query? #973

Closed
jaydip-pawar opened this issue Dec 16, 2020 · 7 comments
Closed

How to pass argument in custom SQL Query? #973

jaydip-pawar opened this issue Dec 16, 2020 · 7 comments

Comments

@jaydip-pawar
Copy link

Stream<List<Password>> watchSearchedPasswords(String query) {
    return customSelectStream('SELECT * FROM Password ORDER BY wName ASC WHERE wName CONTAINS "$query" OR wAddress CONTAINS "$query";',
    readsFrom: {passwords},).map((rows) {
      return rows.map((row) => Password.fromData(row.data, db)).toList();
    });
  }

I have created following method in which i want to return list of rows that contains string which i provide when method is called but how to pass it?

@simolus3
Copy link
Owner

You can pass variables like this:

Stream<List<Password>> watchSearchedPasswords(String query) {
  return customSelectStream('SELECT * FROM Password ORDER BY wName ASC WHERE wName CONTAINS ?1 OR wAddress CONTAINS ?1;',
    variables: [Variable.withString(query)],
    readsFrom: {passwords},
  ).map((rows) {
    return rows.map((row) => Password.fromData(row.data, db)).toList();
  });
}

But in that case I recommend to use build-time checked queries or moor files which can generate those methods for you.

Also, feel free to ask questions like that in the discussions.

@jaydip-pawar
Copy link
Author

I am getting following exception:
Unhandled Exception: DatabaseException(near "WHERE": syntax error (code 1): , while compiling: SELECT * FROM Password ORDER BY wName ASC WHERE wName CONTAINS ?1 OR wAddress CONTAINS ?1;) sql 'SELECT * FROM Password ORDER BY wName ASC WHERE wName CONTAINS ?1 OR wAddress CONTAINS ?1;' args [v]}

@simolus3
Copy link
Owner

Ah yeah, the WHERE must come before the ORDER BY, I didn't see that at first. Build-time queries or moor files would have spotted that problem ;)

@jaydip-pawar
Copy link
Author

But Still I am getting Exception :-(

E/SQLiteLog(20484): (1) near "CONTAINS": syntax error
E/flutter (20484): [ERROR:flutter/lib/ui/ui_dart_state.cc(177)] Unhandled Exception: DatabaseException(near "CONTAINS": syntax error (code 1): , while compiling: SELECT * FROM Password WHERE wName CONTAINS ?1 OR wAddress CONTAINS ?1 ORDER BY wName ASC ;) sql 'SELECT * FROM Password WHERE wName CONTAINS ?1 OR wAddress CONTAINS ?1 ORDER BY wName ASC ;' args [f]}

@simolus3
Copy link
Owner

Ah yeah, CONTAINS isn't a thing in sqlite3. instr(wName, ?1) > 0 OR instr(wAddress, ?1) > 0 might work?

@jaydip-pawar
Copy link
Author

jaydip-pawar commented Dec 16, 2020

I have solved my issue, in Query I am typing instr that's why I'm getting exception, I solved it by writing it in upper case like INSTR, but it is case sensitive so there is any way to find the substring of data with case insensitive .

@simolus3
Copy link
Owner

but it is case sensitive so there is any way to find the substring of data with case insensitive

You could use wName LIKE ('%' || ?1 || '%'), which is case-insensitive for ascii chars.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants