December 28, 2010

Use sqlite3 with codeigniter

Codeigniter is a small, fast, and well-documented web framework. However, currently (as of version 1.7.3), only sqlite2 is supported. I made codeigniter 1.7.3 work with sqlite3, including the scaffolding feature, which I really like.

Here is how to make codeigniter 1.7.3 work with sqlite3:

1. Download the php-based sqlite3 PDO driver for codeigniter. This driver is based on the driver in the codeigniter wiki, but fixed up so it works with v1.7.3 and with scaffolding.

Download the sqlite3 PDO driver (based on the wiki driver 0.2)

2. Edit system/database/DB_driver.php,  around line 831, add the 4 lines below starting with '+'.


foreach($query->result_array() as $row)
{
if (isset($row['COLUMN_NAME']))
{
$retval[] = $row['COLUMN_NAME'];
}
+ else if ($this->platform()=="pdo")
+ {
+ $retval[] = next($row);
+ }
else
{
$retval[] = current($row);
}
}


3. Create directory /pdo in /database/drivers and copy to this directory
  driver *.php files
4. Create SQLite3 database file, and put it to any directory.
  My database file is [APPPATH]/db/base.db
5. In application database config [APPPATH]/config/database.php  set next settings:
 
$db['default']['hostname'''; 
$db['default']['username''';
$db['default']['password''';
$db['default']['database''sqlite:'.APPPATH.'db/base.db';
$db['default']['dbdriver''pdo'

Enjoy.

2 comments: