PeraMika
PeraMika

Reputation: 3688

SQLite/PDO - How to use/connect to database file (.db) on localhost (XAMPP)?

I use XAMPP with

So, if I understood well some tutorials, that's all I need to be able to start working.

So, in C:\xampp\htdocs\my_project I copied database.db file (sakila downloaded from here: https://github.com/joshuakleveter/sakila_db/tree/master/public_html )

Then I created index.php that looks like this:

<?php

try {
    $db = new PDO('sqlite:./database.db');
    $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch(Exception $e) {
    echo 'Error: ';
    echo $e->getMessage();
    die();
}

and when I run this there are no errors. As you can see, my path is /database.db. If I change it to just

$db = new PDO('sqlite:.database.db');

no errors. Or without dot:

$db = new PDO('sqlite:database.db');

or absolute path - there are no errors.

Even worse, if I put wrong not existing .db name:

$db = new PDO('sqlite:daaaaaaaaaaaatabase.db');

also - there are no errors.

Can you tell me what am I doing wrong?

With all of the above paths I've tried the following:

$results = $db->query('select * from film');
var_dump($results);
die();

and I get "General error: 1 no such table", but the database.db has that table (I installed sqlite, opened that database in command line and checked - the database.db is OK).

Upvotes: 2

Views: 3775

Answers (1)

CL.
CL.

Reputation: 180060

Connecting to a not yet existing file is how new databases are created.

Relative paths are relative to the current directory, which you usually cannot control, except by setting it to some absoulte path. Just use an absolute path directly when opening the DB.

Upvotes: 2

Related Questions