SQLite
SQLite or more accurately SQLite3 is a file based database. That means data gets persisted directly into a singular database file within the servers filesystem (or wherever you define it) instead of an actual database on the server.
This makes saving small amounts of data very simple. It is however not suitable for large amounts but SQL DBs are generally not made for big big amounts of data.
Accessing data works very similar to PDO in PHP, SQLite is also a default PHP extension so no extra packages are necessary.
Its setup can be a little icky sometimes as it relies alot on read and write permissions so it can get permission problems right after creating the database file.
You create a database file in the CLI like so:
sqlite3 <database_name>.db
Opening the database file automatically selects the Database so this doesn't need to be selected.
You can then create a table with pretty much standard SQL syntax:
CREATE TABLE IF NOT EXISTS `advent_winners` (`won_at` DATE NOT NULL, `name` VARCHAR(255) NOT NULL);
You can also do this with code by running
$connection = new SQLite3('data/dashboard.db'); $connection->exec("CREATE TABLE IF NOT EXISTS `advent_winners` (`won_at` DATE NOT NULL, `name` VARCHAR(255) NOT NULL);")
This can now be used to normally query things just like PDO except for one annoying thing.
Result sets can't be read out normally, you can only read out one row at a time but you don't get the row count, which leads to having to make your own logic for getting the row count.
$nrows = 0; $resultSet->reset(); while ($resultSet->fetchArray()) { $nrows++; } $resultSet->reset(); $results = []; for ($i = 0; $i < $nrows; $i++) { $results[] = $resultSet->fetchArray(); } return $results;
This works since fetchArray() returns false if it is called upon a row that doesn't exist, which means we can get the row count and correctly read out every row with another for loop.
