_lastErrorMessage = ""; $this->_database = null; if(!getConfig("dataDirectory")){ $this->_lastErrorMessage = "No data directory set!"; error_log("Could not connect to the database: "+ $this->lastErrorMessage()); $this->databaseFile = null; } else{ $this->_databaseFile = getConfig("dataDirectory") ."/siweather.sqlite"; } } function __destruct() { if(!is_null($this->_database)){ $this->close(); } } public function databaseFile() { return $this->_databaseFile; } public function lastErrorMessage() { return $this->_lastErrorMessage; } public function connect() { if(!is_null($this->_database)){ echo "database is not null"; return true; } if(is_null($this->_databaseFile)){ $this->_lastErrorMessage = "No database file given"; error_log("Could not connect to the database: "+ $this->lastErrorMessage()); return false; } try { $db = new SQLite3($this->_databaseFile); } catch(PDOException $exception){ $this->_lastErrorMessage = $exception->getMessage(); error_log("Could not connect to the database: "+ $this->lastErrorMessage()); return false; } # loading SpatiaLite as an extension if(!$db->loadExtension("libspatialite.so.7")){ $this->_lastErrorMessage = "Could not load SpatialLite extension: ". $db->lastErrorMsg(); error_log($this->lastErrorMessage()); return false; } $this->_database = $db; return true; } public function close() { if(is_null($this->_database)){ return true; } if($this->_database->close()){ $this->_database = null; return true; } $this->_lastErrorMessage = $this->_database->lastErrorMsg(); error_log("Failed to close the database: "+ $this->lastErrorMessage()); return false; } } ?>