diff --git a/api_db.php b/api_db.php index a4de0002..515554fb 100644 --- a/api_db.php +++ b/api_db.php @@ -17,19 +17,27 @@ $data = array(); // Needs package php5-sqlite, e.g. // sudo apt-get install php5-sqlite -$db = new SQLite3('/etc/pihole/pihole-FTL.db'); -if(!$db) - die("Cannot access database"); +function SQLite3_connect($trytoreconnect) +{ + try { + // connect to database + $db = new SQLite3('/etc/pihole/pihole-FTL.db'); + } + catch (Exception $exception) { + // sqlite3 throws an exception when it is unable to connect, try to reconnect after 3 seconds + if($trytoreconnect) + { + sleep(3); + SQLite3_connect(false); + } + } +} +SQLite3_connect(true); -// Long-Term API functions -// if (isset($_GET['test'])) -// { -// // $results = $db->query('SELECT * FROM QUERIES order by TIMESTAMP ASC LIMIT 2'); -// $results = $db->query('SELECT TIMESTAMP,TYPE,DOMAIN,CLIENT,STATUS FROM QUERIES order by TIMESTAMP ASC'); -// echo "N=".$result->numColumns; -// while ($row = $results->fetchArray()) -// var_dump($row); -// } +if(empty($db)) +{ + die("Error connecting to database"); +} if (isset($_GET['getAllQueries']) && $auth) {