laravel laravel开发环境sqlite数据库不存在

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19069601/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 02:38:54  来源:igfitidea点击:

laravel development environment sqlite database does not exist

sqlitelaravellaravel-4

提问by isimmons

Trying to use sqlite in development environment. It seems to detect the environment correctly but when I try to migrate to development.sqlite I get exception thrown "database does not exist"

尝试在开发环境中使用 sqlite。它似乎正确地检测到环境,但是当我尝试迁移到 development.sqlite 时,我抛出异常“数据库不存在”

artisan command

工匠命令

php artisan migrate --env=development

bootstrap/start.php

引导程序/start.php

$env = $app->detectEnvironment(array(

    'development' => array('localhost'),

));

app/config/development/database.php

应用程序/配置/开发/database.php

<?php

return array(
    'default' => 'sqlite',

    'connections' => array(
            'sqlite' => array(
            'driver'   => 'sqlite',
            'database' => __DIR__.'/../database/development.sqlite',
            'prefix'   => '',
        )
    )
);

As far as I know laravel is supposed to create the file if it does not exist but since it didn't I tried manually creating the file and still get the exception thrown.

据我所知,如果文件不存在,laravel 应该创建该文件,但由于它不存在,我尝试手动创建该文件并仍然抛出异常。

UPDATE: Maybe something not right with the env because the same thing happens if I try ':memory' for the database.

更新:也许 env 有问题,因为如果我为数据库尝试 ':memory' 也会发生同样的事情。

UPDATE 2: I tried running the sample unit test but add to TestCase.php

更新 2:我尝试运行示例单元测试但添加到 TestCase.php

     /**
     * Default preparation for each test
     *
     */
    public function setUp()
    {
        parent::setUp(); // Don't forget this!

        $this->prepareForTests();
    }

    /**
     * Creates the application.
     *
     * @return Symfony\Component\HttpKernel\HttpKernelInterface
     */
    public function createApplication()
    {
        $unitTesting = true;

        $testEnvironment = 'testing';

        return require __DIR__.'/../../bootstrap/start.php';
    }

    /**
     * Migrates the database and set the mailer to 'pretend'.
     * This will cause the tests to run quickly.
     *
     */
    private function prepareForTests()
    {
        Artisan::call('migrate');
        Mail::pretend(true);
    }

And this too gives the same exception though the testing env is already shipped with laravel. So I'll see if I can find any new issues on that.

尽管测试环境已经随 laravel 一起提供,但这也给出了相同的例外。所以我会看看我是否能找到任何新的问题。

回答by isimmons

Wow, typos and wrong paths.

哇,错别字和错误的路径。

Copying the sqlite array from config/database.php into config/development/database.php I forgot to change the path to the development.sqlite file from

将 config/database.php 中的 sqlite 数组复制到 config/development/database.php 我忘记更改 development.sqlite 文件的路径

__DIR__.'/../database/development.sqlite'

to

__DIR__.'/../../database/development.sqlite'

And for the in memory test it should have been

对于内存测试,它应该是

':memory:'

instead of

代替

':memory'

回答by Erica Badu

I noticed that my database.php file had the following

我注意到我的 database.php 文件有以下内容

'sqlite' => [
    'driver' => 'sqlite',
    'database' => env('DB_DATABASE', database_path('database.sqlite')),
    'prefix' => '',
],

I changed it to read the following, and it worked just fine.

我将其更改为阅读以下内容,并且效果很好。

'sqlite' => [
    'driver' => 'sqlite',
    'database' => database_path('database.sqlite'),
    'prefix' => '',
],

回答by ankit patel

One of the problem which I faced was I use "touch storage/database.sqlite" in terminal, so database is created in Storage folder instead of database folder.

我面临的问题之一是我在终端中使用“touch storage/database.sqlite”,因此数据库是在 Storage 文件夹而不是数据库文件夹中创建的。

in my config/database.php path is database_path('database.sqlite')

在我的 config/database.php 路径是 database_path('database.sqlite')

'sqlite' => [
        'driver' => 'sqlite',
        'database' => database_path('database.sqlite'),
        'prefix' => '',
    ],

than I use command "php artisan migrate" which gave me error "Database (/Applications/MAMP/htdocs/FOLDER_NAME/database/database.sqlite) does
not exist."

比我使用命令“php artisan migrate”给了我错误“数据库(/Applications/MAMP/htdocs/FOLDER_NAME/database/database.sqlite)不
存在”。

so it's obvious database file is not in database folder as It was generated in Storage folder, so copy "database.sqlite" from storage folder or run command "touch database/database.sqlite"

所以很明显数据库文件不在数据库文件夹中,因为它是在存储文件夹中生成的,所以从存储文件夹复制“database.sqlite”或运行命令“touch database/database.sqlite”

Hope that helps.!!

希望有帮助。!!

回答by Vadim Novozhilov

Well, my answer is kinda outdated, but anyway. I faced the same problem, but with Laravel 5, I am using Windows 7 x64. First I manually created SQLite database called 'db' and placed it into storage directory, then fixed my .env file like this:

好吧,我的回答有点过时了,但无论如何。我遇到了同样的问题,但是对于 Laravel 5,我使用的是 Windows 7 x64。首先,我手动创建了名为“db”的 SQLite 数据库并将其放入存储目录,然后像这样修复我的 .env 文件:

APP_ENV=local
APP_DEBUG=true
APP_KEY=oBxQMkpqbENPb07bLccw6Xv7opAiG3Jp

DB_HOST=localhost
DB_DATABASE='db'
DB_USERNAME=''
DB_PASSWORD=''

CACHE_DRIVER=file
SESSION_DRIVER=file
QUEUE_DRIVER=sync

MAIL_DRIVER=smtp
MAIL_HOST=mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=null
MAIL_PASSWORD=null`

I thought it would fix my problems, but the command line keeps telling me that database doesn't exist. And then I just checked the path to db in my database.php file and this is why I put database file into storage directory. But nothing changed. And finally I checked db's extension and it was .db, not .sqlite as default extension you see in your sqlite block in database.php. So this is how I reconfigured sqlite piece:

我以为它会解决我的问题,但命令行一直告诉我数据库不存在。然后我只是在我的 database.php 文件中检查了 db 的路径,这就是我将数据库文件放入存储目录的原因。但什么都没有改变。最后我检查了 db 的扩展名,它是 .db,而不是 .sqlite 作为您在 database.php 的 sqlite 块中看到的默认扩展名。所以这就是我重新配置 sqlite 的方式:

'sqlite' => [
    'driver'   => 'sqlite',
    'database' => storage_path().'/db.db',
    'prefix'   => '',
],

And of course don't forget to set sqlite as default database in your database.php file. Good luck!

当然不要忘记在 database.php 文件中将 sqlite 设置为默认数据库。祝你好运!