Using CodeIgniter 2 with SQLite 3 databases
Note: If you haven’t updated to CodeIgniter 2.1.1 – do so. There is a bug in 2.1 preventing it from working with SQLite databases properly, see the changelog.
The process of connecting to a SQLite 3 database with CodeIgniter isn’t straightforward. You have to use the (built-in) PDO driver and put the path to the database, with protocol prefix, in the hostname parameter, here’s an example:
/application/config/database.php
... $db['default']['hostname'] = 'sqlite:'.APPPATH.'db/your_database.sqlite'; $db['default']['username'] = ''; $db['default']['password'] = ''; $db['default']['database'] = ''; $db['default']['dbdriver'] = 'pdo'; $db['default']['dbprefix'] = ''; $db['default']['pconnect'] = TRUE; $db['default']['db_debug'] = TRUE; $db['default']['cache_on'] = FALSE; $db['default']['cachedir'] = ''; $db['default']['char_set'] = 'utf8'; $db['default']['dbcollat'] = 'utf8_general_ci'; $db['default']['swap_pre'] = ''; $db['default']['autoinit'] = TRUE; $db['default']['stricton'] = FALSE; ...
This assumes you have your database under:
/application/db/your_database.sqlite
If the file does not exist, an empty database will be created at that location.
After you have connected, you can use the Active Record class normally to query the db, for example:
$query = $this->db->get('users'); foreach ($query->result() as $row) { echo $row->id . '<br/>'; }
Remember that you need to build your table structures first with a tool such as phpLiteAdmin.
Why does the syntax differ when conecting to a SQLite 3 database?
The reason you have to put the database path in the hostname field is the way CI instantiates the PDO object:
system/database/drivers/pdo/pdo_driver.php
... function db_connect() { $this->options['PDO::ATTR_ERRMODE'] = PDO::ERRMODE_SILENT; return new PDO($this->hostname, $this->username, $this->password, $this->options); } ...Explore posts in the same categories: Computers, Development, Technical solutions
Tags: codeigniter, php, phpliteadmin, sqlite
You can comment below, or link to this permanent URL from your own site.
05/08/2012 at 12:24
[…] a Stanislav Khromov por dar con la solución. Categories: CodeIgniter, Linux, PHP, SQLite Tags: codeigniter, php, sqlite Comments (0) […]
06/09/2012 at 21:19
Stanislav, thank you for this! It solved my problem. The CI documentation does not correctly describe how to use SQLite.
– Brian
12/09/2012 at 00:50
Thanks man, the documents about this problem at ci.com sucks…
09/10/2012 at 19:51
Hey! Thanks for this useful post.
24/10/2012 at 20:29
Seriously, thank you very much. Saved me from pulling my hair out.
01/04/2013 at 16:47
this saved my day , god bliss you 🙂
08/06/2013 at 11:00
Thanks for the wonderful tip Stanislav.
30/03/2014 at 19:28
Thank you for this. I would also like to point out to my fellow Ubuntu users that may find themselves here that they need to:
sudo apt-get install php5-sqlite
Otherwise, the PDO driver will throw an exception. Cheers!
05/02/2015 at 11:21
i can connect to the database but i can not retrieve data. what to do ?
19/03/2015 at 16:08
PHP Codeigniter – Learn PHP Codeigniter Framework From Scratch
19/10/2017 at 13:00
how can i use pdo predefined functions i codeigniter