halojoy wrote:open small database file in current directory
You might as well open :memory:, i.e., an in-memory database. But given that this can be accomplished as in my example, I see no point unless it can be shown that this is a performance bottle neck and that this alternative solution is measurably faster. If you do want it to be faster, you probably should wrap the inserts in a transaction and reuse a prepared statement, e.g.,
<?php
try {
$db = new PDO('sqlite::memory:'); // Create an in-memory database.
// Create the table and insert the filenames and corresponding sizes.
$db->beginTransaction();
$db->exec('CREATE TABLE Files ("name" TEXT, "size" INTEGER)');
$stmt = $db->prepare('INSERT INTO Files VALUES (:name, :size)');
foreach (glob('*.wav') as $filename) {
$stmt->bindValue(':name', $filename, PDO::PARAM_STR);
$stmt->bindValue(':size', filesize($filename), PDO::PARAM_INT);
$stmt->execute();
}
$db->commit();
// Retrieve the filenames and sizes sorted in ascending order by size.
$stmt = $db->query('SELECT "name", "size" FROM Files ORDER BY "size"');
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
// Print $row['name'] and $row['size']
// ...
}
$db = NULL; // Close the database connection.
} catch (PDOException $e) {
die('Error: Could not create in-memory database.');
}
It is quite clear that this is more complicated than just using two loops, an associative array, and asort().