26

In a Symfony2 project, you can configure the databases connections at the app/config/parameters.ini file. Documentation states that you can use, among others, sqlite3 PDO driver.

But configuring sqlite doesn't works well:

[parameters]
    database_driver   = pdo_sqlite
    database_host     = localhost
    database_port     =
    database_name     = test_project.db
    database_user     = root
    database_password = 

Using app/console doctrine:database:create, successfully creates a test_project.db file at the project root directory.

But after creating some entities, then running app/console doctrine:schema:update --force should create the tables on the database file, but it doesn't, file appears empty, with O bytes size.

Note that using any other PDO driver works well, but not with SQLite...

I've also tried to use the full path for the db file in the database_name parameter, but to no avail, database still doesn't gets updated.

For reference, here's the doctrine dbal section of the config.yml file:

doctrine:
    dbal:
        driver:   %database_driver%
        host:     %database_host%
        port:     %database_port%
        dbname:   %database_name%
        user:     %database_user%
        password: %database_password%
        charset:  UTF8

Is there a way around this? configurations missing? something not stated on the official doc of symfony2 project?

11
  • 2
    Did you try giving the full path to the db? Mar 4, 2012 at 3:06
  • 1
    yes I have, let me state that... Mar 4, 2012 at 3:07
  • 1
    Ok. and the doctrine/dbal section of your main config references the ini values with %parameter_name%? Mar 4, 2012 at 3:11
  • 1
    Ok just checking the basics... I've actually never tried sqlite with Symfony2, or Symfony 1.x for that matter:-) Mar 4, 2012 at 3:14
  • 2
    It worked! thanks!!! hey you deserve the credit here! You should post the answer and I'll mark it as the right one ;) Mar 4, 2012 at 3:36

5 Answers 5

25

According to Doctrine the elements used for sqlite DBAL configuration are:

  • user (string): Username to use when connecting to the database.
  • password (string): Password to use when connecting to the database.
  • path (string): The filesystem path to the database file. Mutually exclusive with memory. path takes precedence.
  • memory (boolean): True if the SQLite database should be in-memory (non-persistent). Mutually exclusive with path. path takes precedence.

This is also listed in the full reference for Doctrine configuration in Symfony2, although not elaborated on.

So you need to switch up your config params to match whats appropriate for sqlite.

6
  • 2
    Thanks! I added a path element to config.yml pointing to %database_path%, then in parameters.ini used database_path to actually refer to the db file I use... Mar 4, 2012 at 3:44
  • Nice, i was actually wondering if it would pick that up from the ini. Good to know :-) Mar 4, 2012 at 3:45
  • 3
    :) also! I've found that the db file needs permissions set correctly. At least on Linux, the db file must be read-write accessible by the webserver user. And the directory where it resides needs read-write-execute permissions for that user too... Thanks again! Mar 4, 2012 at 3:53
  • There is a problem if you only want to use sqlite for testing or whatever - if you add a %database_path% to your config files, you have to add a path parameter to all the parameters.ini files you ever use and leave it blank when you are not using SQLite. This complicates situations like using SQLite for local development and MySQL for a live site, for example.
    – rjmunro
    Feb 2, 2013 at 0:51
  • 1
    @r0sk Well actually you should (imo) use deployments scripts (e.g. with jenkins) to take care of that Mar 18, 2015 at 20:33
6

Here is what I needed to get SQLite to work, just after doing symfony new myapp :

in app/config.yml :

# Doctrine Configuration
doctrine:
    dbal:
        driver:   pdo_sqlite
        path:     "%database_path%"

In app/config/parameters.yml:

parameters:
    database_path: "%kernel.root_dir%/db/myapp_%kernel.environment%.db3"
    ...

Next I could do a composer install, create a new entity and it just worked.

2
  • mailer_host, mailer_user?
    – vladkras
    Aug 20, 2017 at 6:54
  • In my case I need to make sure I had php7.2-sqlite installed on my Ubuntu VM. If you get an error about a missing driver, that could be the problem.
    – David
    Nov 14, 2018 at 21:21
2

I've found that if I add a path line pointing at the database_name to my config.yml, sqlite seems to pick that up, and MySQL doesn't seem to complain.

doctrine:
    dbal:
        driver:   %database_driver%
        host:     %database_host%
        port:     %database_port%
        dbname:   %database_name%
        path:     %database_name%
        user:     %database_user%
        password: %database_password%
        charset:  UTF8

This means you can still keep all database information in the parameters file, you don't need separate configs depending on which database you are using.

2

Mainly the file path or the file path permisssion will have issue.

In config.yml, set path to full path like

/home/{name}/NB/PHP/Symfony/test/src/Database/data.db3

Dont give %database_path% or what ever. Try this it will work.

If it works you can give as

%kernel.root_dir%/../src/Database/%database_path%

Also check sqlite is ok by

phpinfo(INFO_MODULES);

In view/output you can see pdo_sqlite and its version.

1

In my case setting a username and password in config/packages/doctrine.yaml did not create a username/password protected sqlite database.

doctrine:
    dbal:
        charset:  UTF8
        url: '%DATABASE_URL%'
        user:     'foo'
        password: 'bar'

It seems like the parameters username and password are ignored?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.