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

Empty query result, no rows #788

Open
GuilhermeLessa opened this issue May 22, 2018 · 3 comments
Open

Empty query result, no rows #788

GuilhermeLessa opened this issue May 22, 2018 · 3 comments

Comments

@GuilhermeLessa
Copy link

When the query has no result, some error occurs inside the item(0) function and break code.
Else dont run.

Returning the promise without displaying error?
How to execute a code if the select returns empty?

db.executeSql('SELECT userValid ... "', [], function (r) {
        alert(1); //sucess!
        if (r.rows.item(0).userValid == '1') { //test empty result, stop code
          //home page 
          alert(2);
        } else {
           //login page
           alert(3);
        }
  }, function (error) {
      
 });
@brodycj
Copy link
Contributor

brodycj commented May 22, 2018

In case of SELECT query which returns zero (0) rows I can think of the possible results:

  • success with zero (0) rows: check if r.rows.length === 0
  • error result

I will need to add a test case to be 100% sure on all platforms.

Testing, possible fix, and documentation needed for June 2018 release ref: #773

@GuilhermeLessa
Copy link
Author

With verification length works fine.

 if (r.rows.length === 0) {
          //false
 } else {
          if (r.rows.item(0).valor == '1') {
            //true
          } else {
            //false
          }
}

Your repository support is the best. I wish good improvements to the next release. Thanks!

@brodycj
Copy link
Contributor

brodycj commented May 22, 2018

Reopening for now, would like to keep this open until I get this in the test suite for all platforms and explicitly documented.

I would like to make another comment about the code above: it seems to me like the code assumes that there will never be more than 1 record that matches the SELECT statement. I would have added something else to ensure that this is always the case. Recommended: use constraints in CREATE TABLE. This is very common practice and there is plenty of documentation available including:

It would also be possible to check this after the fact, using assertion or error logging in case of r.rows.length > 1 (Sentry.io is highly recommended for logging internal errors).

@brodycj brodycj reopened this May 22, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants