In addition to using LIMIT SQL statements you can also use the SQLite3 function querySingle to retrieve a single row, or the first column.
SQLite3 Quickstart Tutorial
This is a complete example of all the commonly used SQLite related APIs. The aim is to get you up and running really fast. You can also get a runnable PHP file of of this tutorial.
Creating/opening a database
Let's create a new database first. Create it only if the file doesn't exist and open it for reading/writing.
The extension of the file is up to you, but .sqlite is pretty common and self-explanatory.
Creating a table
Inserting sample data.
It's advisable to wrap related queries in a transaction (with keywords BEGIN and COMMIT),
even if you don't care about atomicity. If you don't do this, SQLite automatically wraps every single query in a transaction, which slows down everything immensely. If you're new to SQLite, you may be surprised why the INSERTs are so slow .
Insert potentially unsafe data with a prepared statement.
You can do this with named parameters:
Let's fetch today's visits of user #42.
We'll use a prepared statement again, but with numbered parameters this time, which are more concise:
Note: If there are no more rows, fetchArray() returns false. You can take advantage of this in a while loop.
Free the memory - this in not done automatically, while your script is running
Here's a useful shorthand for fetching a single row as an associative array.
The second parameter means we want all the selected columns.
Watch out, this shorthand doesn't support parameter binding, but you can
escape the strings instead.
Always put the values in SINGLE quotes! Double quotes are used for table
and column names (similar to backticks in MySQL).
Another useful shorthand for retrieving just one value.
Finally, close the database.
This is done automatically when the script finishes, though.
This modified text is an extract of the original Stack Overflow Documentation created by following contributors and released under CC BY-SA 3.0