public function table_exists($tablename = null)
    {
        if (!empty($tablename)) {
            if (isset($this->tables[$tablename])) return true;
            $query = "SELECT name FROM sqlite_master WHERE name='{$tablename}'";
            $results = $this->query($query);
            if ($results && $results->numRows()) {
                $this->tables[$tablename] = true;
                return true;
            }
        }
        return false;
    }