The following code searches for any table not declared in the array $__keys
and DROP
s the table only if the row count is 0.
It is however, not dropping the table. I have added a number of checks using a string called $return
that collects the table name and its row count if it meets the criteria above before returning that data. The function performs correctly and returns the name of the table to be dropped but still does not delete the table.
I can only conclude that the problem is with this line:
$DB->exec("DROP TABLE $name");
since everything else performs correctly. What have I missed?
function deleteTables($DB, $__keys){
$sql = "SELECT name FROM sqlite_master WHERE type = 'table' AND name != 'sqlite_sequence';";
$tablesquery = $DB->query($sql);
$return = "";
while ($table = $tablesquery->fetchArray(SQLITE3_ASSOC)) {
if(!in_array($table['name'], $__keys)){
$name = $table['name'];
$count = $DB->querySingle("SELECT COUNT(*) as count FROM $name");
if($count == 0){
$return .= $name.":".$count;
$DB->exec("DROP TABLE $name");
}
}
}
return $return;
}
The function returns the name and count of the tables that should be deleted but does not DROP
the table.