Titanium Community Questions & Answer Archive

We felt that 6+ years of knowledge should not die so this is the Titanium Community Questions & Answer Archive

Cannot detect SQLite empty field

This is really bugging me now, at least I have found a way of making my app not crash, but its still not working as expected, I am calling several fields from a database, some of these fields are empty (NULL). But Ti doesnt seem to be recognising NULL or an empty string.

Here is my code which doesnt crash, but is still returning an image true when this actually no image.

if(rows.field('image') != 'NULL') {
        var imageRow = Ti.UI.createView({
            backgroundImage:'images/linen.png',
            height:'auto',
            width:320,
            top: 'auto'
        });

        var imgView = Ti.UI.createImageView({
            background: '#000',
            image: rows.fieldByName('image'),
            top: 10,
            left: 10,
            right: 10,
            bottom: 10,
            height: 'auto',
            width: 'auto'
        });
        imageRow.add(imgView);
        Titanium.API.info(rows.fieldByName('image'));
    }

Am I wrong to be checking the field in this way? (in an if() statement).

— asked November 9th 2010 by Ian Tearle
0 Comments

5 Answers

  • Had exactly this problem, none of the other suggestions fixed it for me. I think it is because you get an 'undefined' string returned for NULL values. So the following fixed it for me

    if (rows.fieldByName('details') != 'undefined') {
            //do some stuff
    }
    
    — answered April 2nd 2011 by Nelson Wootton
    permalink
    0 Comments
  • To check for null or empty string, do this:

    if ((myVar != null) || (myVar != '')) {
    
    // do something
    
    }
    

    What you're doing is checking for a string that contains the word NULL

    — answered November 9th 2010 by William Ovanna
    permalink
    1 Comment
    • Yep tried that, but if for example I add Titanium.API.info(rows.fieldByName('image')); inside that if statement it still returns nothing. and the other something I want to hide is displayed.

      This is so frustrating!

      — commented November 9th 2010 by Ian Tearle
  • If anything else fails you could try checking the field length, I assume it's a path to an image and if your images are in the "images/" folder the length should be more than 7 chars long.

    if (rows.fieldByName('image').length > 7)
    {
        //you got an image
    }
    
    — answered November 9th 2010 by S. Levi
    permalink
    0 Comments
  • so

    if(rows.fieldByName('image')==null)
    {
    do stuff
    }
    

    doesnt work?

    — answered November 9th 2010 by Josh Lewis
    permalink
    1 Comment
    • no. that doesnt work either, I have been doing

      if(rows.fieldByName('image')!=null)
      {
      do stuff
      }
      

      but the "do stuff" still happens. This happens on all of my fields that are possible to have empty fields. Even .length() is not working, the code inside the if statement is still running.

      — commented November 9th 2010 by Ian Tearle
  • I would use typeof() to inspect what is really coming back there…

    — answered November 9th 2010 by Stefan Neumann
    permalink
    1 Comment
    • of course, should have thought of that earlier, its actually identified as a string, so, going back to my original code, before I added null, and checking for an empty string:

      if(rows.field('image') != '') {
      // do something
      }
      

      my app promptly crashes complaining about an unrecognised selector.
      http://pastie.org/private/zbzkfmfkwfgpefkfgcagg - for the whole error.

      — commented November 10th 2010 by Ian Tearle
The ownership of individual contributions to this community generated content is retained by the authors of their contributions.
All trademarks remain the property of the respective owner.