如何在 symfony2 项目中使用 sqlite 数据库?

2021-12-10 00:00:00 symfony doctrine-orm sqlite

在 Symfony2 项目中,您可以在 app/config/parameters.ini 文件中配置数据库连接.文档说明您可以使用 sqlite3 PDO 驱动程序等.

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.

但是配置 sqlite 效果不佳:

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 = 

使用 app/console 指令:database:create,在项目根目录成功创建了一个 test_project.db 文件.

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

但是在创建一些实体之后,然后运行 ​​app/console 指令:schema:update --force 应该会在数据库文件上创建表,但事实并非如此,文件显示为空,大小为 O 字节.

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.

请注意,使用任何其他 PDO 驱动程序都可以正常工作,但不适用于 SQLite...

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

我也尝试在 database_name 参数中使用 db 文件的完整路径,但无济于事,数据库仍然没有得到更新.

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.

作为参考,这里是 config.yml 文件的学说 dbal 部分:

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

有没有办法解决这个问题?配置不见了?symfony2 项目的官方文档中没有说明什么?

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

推荐答案

根据 Doctrine 用于 sqlite DBAL 配置的元素是:

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

  • user (string):连接到数据库时使用的用户名.
  • password (string):连接数据库时使用的密码.
  • path (string):数据库文件的文件系统路径.与内存互斥.路径优先.
  • memory (boolean):如果 SQLite 数据库应该在内存中(非持久性),则为真.与路径互斥.路径优先.
  • 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.

这也列在完整的Doctrine配置参考中Symfony2,虽然没有详细说明.

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

因此您需要切换配置参数以匹配适合 sqlite 的内容.

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

相关文章