Setup SQLite in Zend Framework

Like many things in Zend Framework this is very simple. I will here show how I usually do.

First we set the required parameters in application.ini:

[production]
...
;Setup database (SQLITE)
resources.db.adapter = "PDO_SQLITE"
resources.db.params.dbname = APPLICATION_PATH "/../db/basic.db"
...

This tells Zend that we will be using a SQLite database (PDO_SQLITE) and then also the path to the SQLite db file (basic.db)

For easy access we will now add the database handle to the Zend registry:
Bootstrap.php

class Bootstrap extends Zend_Application_Bootstrap_Bootstrap {
 ...
    protected function _initDbAdapter() {
        $this->bootstrap('db');
        $db = $this->getResource('db');
        Zend_Registry::set('db' , $db);
    }
 ...
}

Then whenever we need to use the database we collect if from the Registry

// Get SQLite db handle from the registry
$db = Zend_Registry::get('db');

// Use the database
$resultset = $db->fetchAll("SELECT id, name FROM projects");

Done!

Tested on Mac OSX 10.6.8 and Zend Framework 1.10.8

Leave a Comment


NOTE - You can use these HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre lang="" line="" escaped="" cssfile="">

This site uses Akismet to reduce spam. Learn how your comment data is processed.