Rafael Almeida
Rafael Almeida

Reputation: 10712

How do I connect CakePHP to a SQLite database?

I have a rather basic problem: I can't seem to connect CakePHP to my SQLite database. Surprisingly, I didn't find lots of information about it on the internet, though I may be looking up the wrong keywords. Nevertheless, this is my connection code:

var $default = array(
        'driver' => 'sqlite',
        'connect' =>'sqlite_popen',
        'persistent' => false,
        'host' => 'localhost',
        'port' => '',
        'login' => '',
        'password' => '',
        'database' => '/home/MY_USER_NAME/public_html/my_database.sqlite',
        'schema' => '',
        'prefix' => '',
        'encoding' => ''
);

Still, Cake only says "Cake is NOT able to connect to the database". Also, I don't know where to see the "real" logs (i.e., the error returned from the SQLite "driver"). So, I hit a dead-end. What should I do?

Thanks in advance.

Upvotes: 10

Views: 15793

Answers (5)

Shridhar U Patil
Shridhar U Patil

Reputation: 41

For cakephp 3.6 it should be like this:

'default' => [
    'className' => 'Cake\Database\Connection',
    'driver' => 'Cake\Database\Driver\Sqlite',
    'persistent' => false,
    'host' => 'localhost',
    'port' => '',
    'login' => '',
    'password' => '',
    'database' => 'C:\Bitnami\wampstack-5.6.39-0\apache2\htdocs\cake36\cake36.sqlite',
    'schema' => 'test',
    'prefix' => '',
    'encoding' => ''
],

Upvotes: 0

trante
trante

Reputation: 33986

As of CakePHP 2.0, Sqlite 3 is supported out of the box.

Be sure that sqlite is enabled in your PHP configuration:

phpinfo();

Inside app/Config/databases.php you can define Sqlite database like this:

public $default = array(
        'datasource' => 'Database/Sqlite',
        'persistent' => false,
        'database' => 'my_database_name',
        'prefix' => '',
        'encoding' => 'utf8',
);

Now check your app/webroot/my_database_name.sqlite file.

Upvotes: 9

anonymous
anonymous

Reputation: 61

How to CakePHP with SQLite3:

Requirments:

Steps:

Unpack the Datasources plugin in place.

Edit dbo_sqlite3.php and add:

App::import('Datasource','DboSource');

...just before the 'class' definition.

Use the following configuration in your database.php file:

var $default = array(
    'datasource' => 'Datasources.DboSqlite3',
    'login' => '',
    'password' => '',
    'database' => '/full/path/to/db.sqlite');

Done.

Upvotes: 6

gidmanma
gidmanma

Reputation: 1514

SQLite3 is not officially supported yet by CakePHP... probably because the file attached to this bug/enhancement works.

https://trac.cakephp.org/ticket/3003

Grab the latest version of the file, update it with any newer patches, upload it to your cake/libs/model/datasources/dbo directory and configure it in your database.php file.

I am using a file called dbo_sqlite3.php

My configuration file uses this for the driver setting:

  'driver' => 'sqlite3',

Upvotes: 6

Paolo Bergantino
Paolo Bergantino

Reputation: 488344

Are you trying to connect to a SQLite 3 database? CakePHP doesn't support them yet.

Other than that, you might want to try adding the leading / in your path. Seems like you're trying to do an absolute path but without the leading slash it's not going to do what you think it's going to do.

Upvotes: 1

Related Questions