IB2017 has asked for the wisdom of the Perl Monks concerning the following question:

Hello monks

I am trying to improve the error handling of my application that uses SQLite. I have noticed that the following script is not producing an error, no matter what $database is (empty, a non existing database name, etc.). However, if $database does not exist I would have expected to see an error. This forces me to first check for the existence of $database. Is this a feature?

use DBI; use strict; my $driver = "SQLite"; my $database = ""; my $dsn = "DBI:$driver:dbname=$database"; my $userid = ""; my $password = ""; my $dbh = DBI->connect($dsn, $userid, $password, { RaiseError => 1 }) or die $DBI::errstr; print "Opened database successfully\n";

Replies are listed 'Best First'.
Re: Error DBI::SQLite file not existent
by haj (Vicar) on Aug 19, 2018 at 18:21 UTC

    Hello IB2017,

    The behaviour of DBD::SQLite when used with a non-existent file is a feature. It allows to create a fresh database out of thin air. You only get an error if you don't have the permission to write to the file's directory.

    As the docs say:

    The file is opened in read/write mode, and will be created if it does not exist yet.

    Although the database is stored in a single file, the directory containing the database file must be writable by SQLite because the library will create several temporary files there.

Re: Error DBI::SQLite file not existent
by thanos1983 (Parson) on Aug 20, 2018 at 07:52 UTC

    Hello IB2017,

    You can also take a look on a previously asked question on the forum Determining if SQLite db exists.

    There are a few proposed solutions on how to implement what you ask.

    Hope this helps, BR.

    Seeking for Perl wisdom...on the process of learning...not there...yet!
Re: Error DBI::SQLite file not existent
by IB2017 (Pilgrim) on Aug 20, 2018 at 09:41 UTC

    I very appreciate your insides