Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use sqlite database on symfony2 project?

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?

like image 873
Javier Novoa C. Avatar asked Mar 04 '12 02:03

Javier Novoa C.


People also ask

Is SQLite good for big projects?

Sqlite is ok for mobile application and small applications but I would avoid it for larger projects. Go for something like MySQL, SQL Server (Windows) or Postgre. SQLite can be really slow in big projects. It depends on what are your wanting to do and what a "large project" in your view is.

How do I add a SQLite database to Visual Studio?

Open Visual Studio, select new project, and, in Visual C#, select “Console Application” and provide the name as SQLiteDemo. Click OK. To connect SQLite with C#, we need drivers. Install all required SQLite resources from the NuGet package, as pictured in Figure 1.

Does Visual Studio support SQLite?

The SQL Server Compact & SQLite Toolbox adds several features to help your SQL Server Compact and SQLite development efforts: Explore! Connect to SQL Server Compact 4.0, 3.5, SQL Server and SQLite database files in Visual Studio 2017 and later.


2 Answers

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.

like image 26
Fred Avatar answered Oct 20 '22 18:10

Fred


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.

like image 124
prodigitalson Avatar answered Oct 20 '22 18:10

prodigitalson