Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 672
GrumpyCrouton Avatar asked May 06 '16 19:05

GrumpyCrouton


2 Answers

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
);
like image 171
moped Avatar answered Sep 28 '22 07:09

moped


'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
like image 32
m.eita Avatar answered Sep 28 '22 09:09

m.eita