On Wed, 5 May 2010 03:08:42 -0700 (PDT), yogibabu
<madra...@interia.pl> wrote:

>
>in php I declared database object:
>$pdo = new PDO('sqlite:mybase.DB3');
>
>i know how to get information about engine used in this connection, which
>is:
>$pdo->getAttribute(PDO::ATTR_DRIVER_NAME);   ---> string 'sqlite'
>
>But I do not know how to get back the actual database file name back from
>this object. Is it possible to be done from query to the database itself
>(maybe some kind of PRAGMA) or from query to the object.


You already know the filename, you passed it to the
constructor. If you want to maintain more state information,
just subclass the PDO classes which do have the extra
attributes you desire.

The constructor can validate the database, and create a new
one when needed. In example below I a text file with the
schema to PDO in order to create the database.

<?php

/* constants */
define('SQLITEDBPATH','/opt/var/sqlite/db/');

/* sqlitedb class */
class sqlitedb extends PDO {

        var $sqlitedbpath;       // path to the database files
        var $sqlitedbname;       // database name without path
and extension

        /* Class constructor */
        function __construct($dbname){
                $this->sqlitedbpath = SQLITEDBPATH;
                $this->sqlitedbname = $dbname;

                /* Make connection to database */
                try {
                        $catchmsg = 'Failed to connect to database
'.$this->sqlitedbname.', ';
                        parent::__construct('sqlite:'.$this->dbfile());
                        $this->setAttribute(PDO::ATTR_ERRMODE,
PDO::ERRMODE_EXCEPTION);
                        $sql = 'SELECT COUNT(name) AS tblcount FROM
sqlite_master WHERE type == \'table\'';
                        $res = $this->query($sql);
                        if (!$res){
                                throw new Exception('Error on query
sqlite_master table count');
                        }
                        $row = $res->fetch();
                        if (!$row){
                                throw new Exception('Error on fetch
sqlite_master table count');
                        }
                        $tblcount = $row['tblcount'];
                        if ($tblcount < 3){
                                $fschema = 'inc/mk'.$this->sqlitedbname.'.sql';
                                if (file_exists($fschema)){
                                        $catchmsg = 'Failed to initialize 
database
'.$this->sqlitedbname.', ';

/**
  * remove -- ..... \n comments -> \n
  * replace 3 newlines by a single one
  * replace 2 newlines by a single one
  */
                                        $schema = preg_replace(array(
'/\s*--[^\n]*\n/',
'/\n\n\n/',
'/\n\n/'),array(
"\n",
"\n",
"\n"),file_get_contents($fschema));
                                        $this->exec($schema);
                                        unset($schema);
                                } else {
                                        throw new Exception('No schemafile
'.$fschema.' present, can\'t create database.');
                                }
                        }
                        return TRUE;
                } catch (PDOException $e) {
                        error_log($catchmsg . $e->getMessage());
                        error_log(print_r($this->errorInfo(),TRUE));
                        return(FALSE);
                }
        } // end __construct()

        /**
         *  For debugging: tell us when we're done.
         *  Comment out the whole function to suppress.
         */
        function __destruct(){
                error_log(__FUNCTION__.' '.$this->sqlitedbname);
        }

        /**
         *  Return the full disk:/path/filename.ext of the sqlite
database file.
         *
         */
        function dbfile(){
                return $this->sqlitedbpath.$this->sqlitedbname.'.db3';
        }

etcetera.

};  // end class sqlitedb

?>
-- 
  (  Kees Nuyt
  )
c[_]
_______________________________________________
sqlite-users mailing list
sqlite-users@sqlite.org
http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users

Reply via email to