SQL query incorrectly returning no results

You must Login before you can answer or comment on any questions.

Hi all,

I have an app that is trying to pull data from a local database. I've connected to the database and can pull data when the SQL query is based on the primary key but I've been trying to run the same query on a different integer column and it incorrectly returns 0 rows. I've tested the SQL statement elsewhere and it returns the data fine, just not inside the app. Also I've tried querying the database on another column that is based on a string and that works too.

Example of code that doesn't work:

var rows = db.execute('SELECT * FROM Event WHERE track_id="1"');
But this code does work:
var rows = db.execute('SELECT * FROM Event WHERE event_id="1"');
I've checked the spellings of the column names and everything but it still doesn't work. Anyone else encountered the same problem?

Thanks

— asked 2 years ago by Andy F
2 Comments
  • Would you please state your mobile platform, as you should with every post? Thanks

    — commented 2 years ago by Paul Dowsett

  • sorry, currently building it for iPhone

    — commented 2 years ago by Andy F

3 Answers

Accepted Answer

Andy, Here's a few crazy things to remember when using SQLite in Titanium. You might already know them but can't hurt to mention them anyway.

Firstly, when working on the device or in the simulator, you have to reset it to see any changes in the database. In the simulator goto File > Reset Contents and Settings and reload your app. It may help.

Secondly, simplify your tests, use this code just to see if you get a result

var rows = db.execute('SELECT * FROM Event');
I like to test for everything just to se if there is a result. Then you know you are at least returning something from the database.

These two handy hints always help me when I'm getting frustrated. Hope they help you. Wayne

— answered 2 years ago by Wayne Buchner
answer permalink
1 Comment
  • THANK YOU so much! Can't believe it but the reset contents and settings solved it all. Kicking myself for not seeing that sooner, seeing as it's such a simple thing to check. Definitely a tip to remember.

    I've spent all day and a lot of time this week trying to solve it and I was starting to give up so thanks so much for the tip!

    — commented 2 years ago by Andy F

Andy

It doesn't work because you are not using execute() correctly. You should be using string substitution. See Storing data for examples.

Cheers

— answered 2 years ago by Paul Dowsett
answer permalink
1 Comment
  • sorry, should have said I had already tried that. Just did it again to double check and still getting the same problem.

    Thanks for the suggestion.

    — commented 2 years ago by Andy F

I have never had trouble with any selects. You mentioned the the column which is failing is an integer column, so try this instead:

var rows = db.execute('SELECT * FROM Event WHERE event_id=1');

— answered 2 years ago by Doug Handy
answer permalink
3 Comments
  • yeah tried that as well and it works either way on the column that works, but neither works on the one I'm having trouble with

    — commented 2 years ago by Andy F

  • Then are you 100% positive the field event_id actually contains the value you expect? It sounds to me like it does not. Try logging the event_id field value after you have read a record using track_id in the where clause.

    — commented 2 years ago by Doug Handy

  • yeah, I have 8 rows in the database and they each have a value of 1-8 for the field I'm trying to find. But I took your advice and tried to return that field from a successful query and it strangely returned null. I've checked all nine fields and the only ones that are not returned null are the first two, despite each column having data. The only difference I could see between these 2 and the other 7 was that the 2 that were returned were set to "NOT NULL". I changed the rest of the table so all columns don't accept null values, but that hasn't fixed it.

    — commented 2 years ago by Andy F

Your Answer

Think you can help? Login to answer this question!