Still struggling with SQLIte Database in Web Edition

I am developing a web app using WE 2014r3.1. For some reason, it’s not catching a failed connection to the SQLite Database. Here is what I’m doing:

  1. I am going into the session and creating a property called MyDB of type SQLDatabase and making it Public
  2. In the sessions open() method, I have the following code:
Dim dbFile As FolderItem
Dim MyDB as new SQLiteDatabase
 
dbFile = SpecialFolder.ApplicationData.Child("MyApplication").Child("MyDB.db")
  
MyDB.DatabaseFile = dbFile
If MyDB.Connect() = False Then
   MsgBox("Could not connect to database for use. Error: " + MyDB.ErrorMessage)
   Quit
End If

If I understand correctly, a new session is created when a user connects to the web application. Immediately upon the creation of the session, the session.open() method is executed and the database code above should try to connect to the MyDB database file and then display a MsgBox when the file isn’t there. But, for some reason, that doesn’t happen. Even if the database file isn’t there (and I’ve checked, it isn’t) it just goes ahead and loads the main screen.

Am I misunderstanding something here? Can anyone offer me some advice on how to get this working?

Thanks,
Dave

I don’t do “web” apps with XOJO, but I think the problem is SCOPE

You are creating the instance of MYDB in the session.open event…
It will only “live” as long as that event lives…
it doesn’t catch the failure to connect, because it DOES connect, then immediately disconnects when it goes out of scope

Move the statement of

Dim MyDB as new SQLiteDatabase

to a public module and that should fix it…

[quote=272891:@Dave S]I don’t do “web” apps with XOJO, but I think the problem is SCOPE

You are creating the instance of MYDB in the session.open event…
It will only “live” as long as that event lives…
it doesn’t catch the failure to connect, because it DOES connect, then immediately disconnects when it goes out of scope

Move the statement of

Dim MyDB as new SQLiteDatabase

to a public module and that should fix it…[/quote]

Thank you for the information. But I have to disagree. In my case, I know that the database is not being connected to because there is no database to connect to. The file isn’t there at all because I’ve not created it yet. I can see how scope would come into play once it actually is really connecting but I don’t think this is the case here.

Because the file doesn’t exist the line

dbFile = SpecialFolder.ApplicationData.Child("MyApplication").Child("MyDB.db")

sets dbFile to NIL

When you try to connect to a database that has NIL for the database file what you get is an IN MEMORY database
So connect succeeds
And then when the session ends the db is removed from memory & … no data in the database because its no longer in memory

You really need to check the folder item for existence BEFORE you try to connect
Something like

  Dim dbFile As FolderItem
  Dim MyDB as new SQLiteDatabase
  
  dbFile = SpecialFolder.ApplicationData.Child("MyApplication")
  if dbfile = nil then 
    // we have some really big problem as this means something in 
    // SpecialFolder.ApplicationData doesn't exist
    
    // so panic & get the heck out
    Quit
  elseif dbfile.exists = false then
    dbFile.CreateAsFolder()
    
    if dbFile.Exists = false then
      // panic & get the heck out as we just created it !!!!!!!
      Quit
    end if
  
  if dbFile.Directory = false then
    // panic & get the heck out as its not a DIR ??????????
    Quit
  end if
  
  dbFile = dbFile..Child("MyDB.db")
  if dbFile = nil then
  elseif dbFile.Exists = false
    If MyDB.CreateDatabaseFile() = False Then
      MsgBox("Could not connect to database for use. Error: " + MyDB.ErrorMessage)
      Quit
    End If
  else
    If MyDB.Connect() = False Then
      MsgBox("Could not connect to database for use. Error: " + MyDB.ErrorMessage)
      Quit
    End If
  end if

Aha! I didn’t realize that’s how you create an in-memory database. I don’t like that! But thank you very much, I’ll change it and see how it goes. Many thanks!

Norman? confused…

Is what you said (in regards to the snippet in the OP) true only for WEB apps?
because in a DESKTOP APP if the file does not exist at the designated path, then the msgbox DOES fire.
I just tried it…

a FolderItem would not be NIL, if it were a “legal” path… but it would “not exist”
you indicated that it would be NIL… so a WEB thing then?

[quote=272898:@Dave S]Norman? confused…

Is what you said (in regards to the snippet in the OP) true only for WEB apps?
because in a DESKTOP APP if the file does not exist at the designated path, then the msgbox DOES fire.
I just tried it…

a FolderItem would not be NIL, if it were a “legal” path… but it would “not exist”
you indicated that it would be NIL… so a WEB thing then?[/quote]
So did I dave
Copied pasted ran
no msg box
I definitely did not have a file at the indicated path
dbFile = SpecialFolder.ApplicationData.Child(“MyApplication”).Child(“MyDB.db”)
for me dbfile is nil after this line

If you already had a path named as his was indicated you would not get a nil

The point really was if the PATH cant be resolved (for whatever reason) and you get a NIL connect WILL succeed since you get an in memory DB

FWIW this is how you create an in memory db

  Dim dbFile As FolderItem
  Dim MyDB as new SQLiteDatabase
  
  If MyDB.Connect() = False Then
            break
  End If

the databaseFile property of the instance is nil - exactly what you’d get if the path to the indicated db could not be resolved, likely because in davids case SpecialFolder.ApplicationData.Child(“MyApplication”) does not exist.