Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

Henrik Christensen
seal-mask
.a{fill-rule:evenodd;}techdegree
Henrik Christensen
Python Web Development Techdegree Student 38,322 Points

PDO SQLite

Hi,

I'm working on a project and using SQLite3 (for now) but if I enter a DB that doesn't exist then it just create a new DB with that name instead of throwing an exception.

I can see on stackoverflow and php.net that if you enter a invalid .db file then it just create a new one, and I can't figure out why because it's throwing an exception just fine when Alena does it in the Handling Exceptions | Integrating PHP with Databases video?

My connection.php

<?php

try {
    $db = new PDO("sqlite:".__DIR__."/test.db"); // teeest.db just creates a new file named teeest.db
    //var_dump($db);
} catch(PDOException $e) {
    die($e->getMessage());
}

echo "success";

1 Answer

Benjamin Larson
Benjamin Larson
34,055 Points

This took a little digging for a helpful answer haha. I tried the same as you, using the your code to connect a localhost on a windows machine and it would simply create the test.db if it was missing. I tried setting it in Laravel, however, and it did indeed throw an exception--so I dug into the laravel code and found this:

<?php
    public function connect(array $config)
    {
        $options = $this->getOptions($config);

        // SQLite supports "in-memory" databases that only last as long as the owning
        // connection does. These are useful for tests or for short lifetime store
        // querying. In-memory databases may only have a single open connection.
        if ($config['database'] == ':memory:') {
            return $this->createConnection('sqlite::memory:', $config, $options);
        }

        $path = realpath($config['database']);

        // Here we'll verify that the SQLite database exists before going any further
        // as the developer probably wants to know if the database exists and this
        // SQLite driver will not throw any exception if it does not by default.
        if ($path === false) {
            throw new InvalidArgumentException("Database (${config['database']}) does not exist.");
        }

        return $this->createConnection("sqlite:{$path}", $config, $options);
    }

The comments are helpful to see that the SQLite driver for PDO will not itself throw any exception for this, but Laravel implements it's own check on the file. Learn something new every day...

Benjamin Larson
Benjamin Larson
34,055 Points

I realized that I pretty much just confirmed the behavior you were questioning, and not answering the question of why it responds differently in Alena's course video. I can't replicate what's in the video either, and it seems there were several questions about this exact problem in the past so you're not alone. I'll keep digging a little, are you using PHP5 or 7?

Benjamin Larson
Benjamin Larson
34,055 Points

I downloaded the source code from the course and it's still generating the file if it doesn't exist, so I don't know how it worked the way it did in the course video. Perhaps it was a little video trickery? It would respond that way if using a mysql database call.