4

I'm struggling to find any information on how to configure SQLite in Kohana 3.2. I mainly need to know:

  • What should I set hostname, database, username and password to (with default user and no password)?

  • Also, how can I set the path to the SQLite database file?

  • What should the "type" be? I tried "sqlite" but I get an error Class 'Database_Sqlite' not found.

This is my current configuration options:

'exportedDatabase' => array
(
    'type'       => 'sqlite',
    'connection' => array(
        /**
         * The following options are available for MySQL:
         *
         * string   hostname     server hostname, or socket
         * string   database     database name
         * string   username     database username
         * string   password     database password
         * boolean  persistent   use persistent connections?
         *
         * Ports and sockets may be appended to the hostname.
         */
        'hostname'   => $hostname,
        'database'   => $database,
        'username'   => $username,
        'password'   => $password,
        'persistent' => FALSE,
    ),
    'table_prefix' => '',
    'charset'      => 'utf8',
    'caching'      => FALSE,
    'profiling'    => TRUE,
),
laurent
  • 88,262
  • 77
  • 290
  • 428

3 Answers3

5

You can use PDO through Database module. The proper way of configuring it looks like this:

'exportedDatabase' => array(
    'type'       => 'pdo',
    'connection' => array(
        'dsn'        => 'sqlite:/path/to/file.sqlite',
        'persistent' => FALSE,
    ),
    'table_prefix' => '',
    'charset'      => NULL, /* IMPORTANT- charset 'utf8' breaks sqlite(?) */ 
    'caching'      => FALSE,
    'profiling'    => TRUE,
),

One disadvantage of using PDO in Kohana is that in ORM you have to specify all fields by hand in your model (you should do it anyway for performance reasons) because of how different database systems handle listing of table fields.

There is also real database module created by banditron. You have to remember that's it is NOT a drop-in replacement for Database module and therefore Kohana's ORM will not work with it. Other than that it's pretty neat and has wide support for database systems other than SQLite.

Yarin
  • 173,523
  • 149
  • 402
  • 512
d4rky
  • 469
  • 6
  • 13
  • @d4rky- I've tested this on 3.2 and it doesn't work. As far as I can tell Kohana's PDO framework doesn't include a SQLite driver. – Yarin Nov 03 '11 at 00:31
  • PDO is PHP's part, not Kohana's, so it's more likely your PHP doesn't have sqlite driver. http://php.net/manual/en/book.pdo.php – d4rky Nov 04 '11 at 13:16
  • @k4rky- You're right it does work, but only after I changed charset from 'utf8' to NULL. I edited your answer to reflect that. – Yarin Nov 05 '11 at 03:28
3

As I found out, Kohana 3.x doesn't actually support SQLite. There's an unsupported module for it and, as far as I can tell, it's not working.

It's easy enough to use PDO though and the syntax is pretty much the same as Kohana's ORM:

$db = new PDO('sqlite:' . $dbFilePath);

$query = $db->prepare('CREATE TABLE example (id INTEGER PRIMARY KEY, something TEXT)');
$query->execute();

$query = $db->prepare("INSERT INTO example (id, something) VALUES (:id, :something)");
$query->bindParam(':id', $id);
$query->bindParam(':something', $something);
$query->execute();
laurent
  • 88,262
  • 77
  • 290
  • 428
-1

I don't use Kohana, but this should work:

'hostname' => /path/to/your/sql/lite/file.sqlite
'database' => ''
'username' => ''
'password' => ''
DavidEG
  • 5,857
  • 3
  • 29
  • 44