connection = new PDO('sqlite:' . $dbPath); $this->connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $this->connection->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC); } catch (PDOException $e) { throw new Exception('Database connection failed: ' . $e->getMessage()); } } public static function getInstance() { if (self::$instance === null) { self::$instance = new self(); } return self::$instance; } public function getConnection() { return $this->connection; } public function query($sql, $params = []) { try { $stmt = $this->connection->prepare($sql); $stmt->execute($params); return $stmt; } catch (PDOException $e) { throw new Exception('Query failed: ' . $e->getMessage()); } } public function fetchAll($sql, $params = []) { $stmt = $this->query($sql, $params); return $stmt->fetchAll(); } public function fetchOne($sql, $params = []) { $stmt = $this->query($sql, $params); return $stmt->fetch(); } public function execute($sql, $params = []) { $stmt = $this->query($sql, $params); return $stmt->rowCount(); } public function lastInsertId() { return $this->connection->lastInsertId(); } }