GrumpyCrouton
GrumpyCrouton

Reputation: 8621

SQLite3 in CodeIgniter 3.0.6

I can't find any documentation on how to use sqlite3 in CodeIgniter, but it does say that it is supported.

Here is my current database configuration:

$db['default']['hostname'] = '';
$db['default']['username'] = '';
$db['default']['password'] = '';
$db['default']['database'] = 'db/base.db';
$db['default']['dbdriver'] = 'sqlite3';
$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;

But I get a very undescriptive error on page load

Unable to connect to your database server using the provided settings.

Filename: core/CodeIgniter.php

Line Number: 500

So my question is, why is my config not working, and how can I make it work?

Upvotes: 6

Views: 15306

Answers (2)

m.eita
m.eita

Reputation: 11

'database' => './application/database/data.db',

if not working replace with

'database' => APPPATH.'database/data.db',

and

'dsn'      => 'sqlite:application/database/data.db',// path/to/database

if not working replace with

'dsn'      => 'sqlite:'.APPPATH.'/database/data.db',// path/to/database

Upvotes: 1

moped
moped

Reputation: 2247

You are using CI2 syntax, I don't know where you got it as in default package you can find the same code as below, where only thing you need to define is database (path to db) and dbdriver

$db['default'] = array(
    'dsn'      => '',
    'hostname' => '',
    'username' => '',
    'password' => '',
    'database' => './application/database/data.db',
    'dbdriver' => 'sqlite3',
    'dbprefix' => '',
    'pconnect' => FALSE,
    'db_debug' => (ENVIRONMENT !== 'production'),
    'cache_on' => FALSE,
    'cachedir' => '',
    'char_set' => 'utf8',
    'dbcollat' => 'utf8_general_ci',
    'swap_pre' => '',
    'encrypt'  => FALSE,
    'compress' => FALSE,
    'stricton' => FALSE,
    'failover' => array(),
    'save_queries' => TRUE
);

Or you can use this (with PDO database driver which requires dsn string, otherwise CI will try to build it)

$db['default'] = array(
    'dsn'      => 'sqlite:application/database/data.db',// path/to/database
    'hostname' => '',
    'username' => '',
    'password' => '',
    'database' => '',
    'dbdriver' => 'pdo',
    'dbprefix' => '',
    'pconnect' => FALSE,
    'db_debug' => (ENVIRONMENT !== 'production'),
    'cache_on' => FALSE,
    'cachedir' => '',
    'char_set' => 'utf8',
    'dbcollat' => 'utf8_general_ci',
    'swap_pre' => '',
    'encrypt'  => FALSE,
    'compress' => FALSE,
    'stricton' => FALSE,
    'failover' => array(),
    'save_queries' => TRUE
);

Upvotes: 15

Related Questions