Tuesday, May 3, 2011

Working with SQLite in AIR: Part 10

Today we are going to learn about the first parameter of the execute() method.

We've talked about it before, and I promised to write about this in detail in future. Before, we've left the first parameter of execute() -1, which is the default value. Now let's see what other values can we enter and what does this even do.

The number basically means how many items from our SQLite database are we reading using the SELECT command. This is especially useful when you are working with very large databases that have over a thousand of items. If we have that many elements, then it will surely be more effective and faster to only read the ones that we are actually going to use.

For example, say we have a very big database of something, for example, a list of customers at a large company. We want to display the customers in a list and let the user browse through them, but in our AIR application we only allow 50 people to be shown. This is where we can set a limit for how many items to read from the database using the first parameter in the execute() function.

By default the value equals -1, which means read all the items. If the number is positive, then that number equals the amount of items to be read. For example:

myStat.execute(50);

The code above will only read the first 50 items of the results of our SELECT command.

Now, what about the other items? How do we display the rest of the results?

You don't need to call execute() again. All you have to do is call a method called next(), which will read the same amount of items after the ones we've already read. In fact, it also throws the same events as the execute() method.

myStat.next();

There is only a method to return the next items in the database, but no method to do the same for the previous. But if we are, like in the example I've stated, displaying the results in pages, then we would simply store all our results in an array, and return to the previous values through our array if we need to go back to previous pages, instead of requesting from SQL again.

Thanks for reading!

Related:

Working with SQLite in AIR: Part 1
Working with SQLite in AIR: Part 2
Working with SQLite in AIR: Part 3
Working with SQLite in AIR: Part 4
Working with SQLite in AIR: Part 5
Working with SQLite in AIR: Part 6
Working with SQLite in AIR: Part 7
Working with SQLite in AIR: Part 8
Working with SQLite in AIR: Part 9
Working with SQLite in AIR: Part 11
Working with SQLite in AIR: Part 12

No comments:

Post a Comment