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).
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 }
-
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
-
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 }
-
so
if(rows.fieldByName('image')==null) { do stuff }
doesnt work?
-
I would use typeof() to inspect what is really coming back there…