SQLite Forum

Opening a database without creating a new one and Error logging
Login

Opening a database without creating a new one and Error logging

(1) By PongoZ on 2020-04-02 08:20:09 [link]

Hi. I am new to PHP.
I am using this function to open a database and delete an entry, however I don't want to create new db if it doesn't exist. In that case I'd like to have an error returned to the HTML page.

Problem is that neither 

       echo "Unable to open database\n";
nor 
       echo "entry deleted\n";

 show on the browser's console on the webpage. I am using Safari on Mac.

Also the alert does not pop up although the entry is successfully deleted.

I want to have full control of the success / no success of the operation, so that this info can be fed back to the HTML page and other methods can be triggered accordingly.

Any help would be appreciated! Thanks 


<?php
   class MyDB extends SQLite3
   {
      function __construct()
      {
         $this->open('myDb.db');
      }
   }
   $db = new MyDB();
   if(!$db)
   {
       echo "Unable to open database";

   } else {
      echo "Opened database successfully\n";
       
       $Id = $_GET['Id'];
       $db->exec("DELETE FROM myDb.db WHERE entryId=\"$Id\"");
       $db->close();
      
       echo "entry deleted\n";
       alert("entry deleted");
   }
   
?>

(2) By Stephan Beal (stephan) on 2020-04-02 08:47:09 in reply to 1 [link]

This is a question for your PHP documentation, not sqlite. sqlite, by default, create a database if it doesn't already exist. You can solve that in 2 ways with PHP: 1) learn (vai the PHP docs) how to detect whether a file exists before trying to open it or 2) learn (via the PHP docs) how to tell the PHP sqlite bindings to *not* create a database if it doesn't already exist.

```
$db = new MyDB();
if(!$db)
{
   echo "Unable to open database";
}
```

That cannot behave how you expect it to (the `new` keyword simply doesn't work like that), but, again, this is an sqlite forum, not a PHP forum, so i'll redirect you to the PHP documentation and forums for why that cannot work.

(3) By PongoZ on 2020-04-02 14:43:37 in reply to 2 [link]

You are right. Thank you

(4) By Warren Young (wyoung) on 2020-04-02 15:36:28 in reply to 2 [link]

> sqlite, by default, create a database if it doesn't already exist.

Only through *some* of its interfaces. You can do what the OP wants via [`sqlite3_open_v2(..., SQLITE_OPEN_READWRITE)`][1].

The question, then, is how to get the PHP SQLite DB API to call that instead of `sqlite3_open()` or how to call "v2" without `SQLITE_OPEN_CREATE`.

[1]: https://sqlite.org/c3ref/open.html

(5.1) Originally by luuk with edits by Richard Hipp (drh) on 2020-04-03 16:08:23 from 5.0 in reply to 4 [link]

<https://www.php.net/manual/en/sqlite3.open.php>

~~~
public SQLite3::open ( string $filename [, int $flags = SQLITE3_OPEN_READWRITE | SQLITE3_OPEN_CREATE [, string $encryption_key = "" ]] ) : void
~~~

flags
Optional flags used to determine how to open the SQLite database. By default, open uses `SQLITE3_OPEN_READWRITE | SQLITE3_OPEN_CREATE`.

`SQLITE3_OPEN_READONLY`: Open the database for reading only.

`SQLITE3_OPEN_READWRITE`: Open the database for reading and writing.

`SQLITE3_OPEN_CREATE`: Create the database if it does not exist.


But, indeed, this is not a PHP forum.... ;)

(6) By anonymous on 2020-12-06 00:57:50 in reply to 1

$dbhandle = new SQLite3(":memory:");
$dbhandle->close();
$dbhandle->enableExceptions(TRUE);
try {
	$dbhandle->open("not_exists.sqlite3", SQLITE3_OPEN_READWRITE);
} catch (Exception $exception) {
	echo '<p>There was an error connecting to the database!</p>';
}