PHP PDO SQLite connection

13,189

This is the expected behaviour. When SQLite is called, it looks for the filename specified.

    $db = new PDO("sqlite:".__DIR__."/database.sql");

If the database.sql file is not found, it will attempt to create the file.

As an aside, you might want to change the file extension from .sql to .sqlite for clarity.

Share:
13,189
Denis Milosavljevic
Author by

Denis Milosavljevic

Interested in web & mobile development, Experienced in Ecommerce management but here to improve code skills particuralery (PHP, MySql, JS).

Updated on July 22, 2022

Comments

  • Denis Milosavljevic
    Denis Milosavljevic almost 2 years

    I created connection to database, but I dont know why it always create a new empty database.sql file. When I rename the database file he creates always a new file instead giving me an error.

    Here is my code

        try {
       $db = new PDO("sqlite:".__DIR__."/database.sql");
        $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    }   catch (Exception $e) {
        echo "Unable to connect";
        echo $e->getMessage();
        exit;
    }
    
    echo "Connected to the database";