Close database

Hi everybody,
I try to close my db (database)
I have 2 methods (linked with 2 buttons) :

  • DB_Ouvrir
  • DB_Fermer

–––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
// DB_Ouvrir
// 26/04/14 - Méthode pour ouvrir la base de donnée.
// Modifiée :
//

Dim dialog As OpenDialog
Dim file As FolderItem

dialog = New OpenDialog // create the dialog (does not actually show it)

// The filter is what type of file you are looking for
// The filters are defined in the File Types dialog
// inside the edit menu
dialog.Filter = “sqlite” // “text/plain”

dialog.PromptText = “Sélectionner la base de donnée.”// The OpenDialog class supports custom prompts…
dialog.Title = “Ouvrir fichier”// …and custom title’s

// Also, you can display the dialogs as sheets
// by calling dlg.showModalWithin(self)
//
// For regular modal dialogs, just call dlg.showModal
file = dialog.ShowModal

//then test for nil to see if the user clicked cancel
If file <> Nil Then
SelectedFolderItemLabel.Text = file.AbsolutePath

Dim dbFile As FolderItem
Dim db As New SQLiteDatabase
'dbFile = GetFolderItem("database_005.sqlite")
dbFile = GetFolderItem(SelectedFolderItemLabel.Text)

db.DatabaseFile = dbFile
If db.Connect Then
  
  'mDB=db
  'mDb.DatabaseFile = dbFile
  
  OpenStatusLabel.Text = "SQLite database est ouverte"
  Toolbar_11.Tool_Ouvrir.Enabled = False
  Toolbar_11.Tool_Fermer.Enabled = True
  
  //proceed with database operations here..
Else
  MsgBox("Erreur d'ouverture de la Database : " + db.ErrorMessage)
  Toolbar_11.Tool_Ouvrir.Enabled = True
  Toolbar_11.Tool_Fermer.Enabled = False
  
End If

Else
SelectedFolderItemLabel.Text = “User clicked Cancel.”
End If
–––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
// DB_Fermer
// 26/04/14 - Méthode pour fermer la base de donnée.
// Modifiée :
// 26/04/14 - Problème avec db.Close

if db.Connect then
If db.Close Then // Base fermée <- Line 7
Toolbar_11.Tool_Ouvrir.Enabled = True
Toolbar_11.Tool_Fermer.Enabled = False
Else // base ouverte
Toolbar_11.Tool_Ouvrir.Enabled = False
Toolbar_11.Tool_Fermer.Enabled = True
End If
msgbox(“base fermée”)
else
msgbox(“Basse non connectée”)
end if
–––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––

I have a error :
Window1_DB_Fermer, line 7 - This method doesn’t return a value
if db.Close Then // Base fermée

I don’t understand why ?
When I trace on this method, db is nil. Why, normally is declare like SQLiteDatabase

I have on Properties :
Name : db
Type : SQLiteDatabase
Scope : Public

What is false ?

if db.Connect then
  db.Close() // Base ferme <- Line 7
  Toolbar_11.Tool_Ouvrir.Enabled = True
  Toolbar_11.Tool_Fermer.Enabled = False

  msgbox("base ferme")
else
  msgbox("Basse non connecte")
end if

Also, be aware that, unless there is a problem, db.Connect will establish a connection to your database even if was already closed.

Kem, I don’t understand.
My base is correctly open.
But why db is not identify on this method ?

I Work on Mac, can somebody help me with ‘Messages’ on Sharescreen, or with ‘TeamViewer’ ?

The db.Close method does not return a value. You simply call it and it closes. See

http://documentation.xojo.com/index.php/Database.Close
If the database had not been connected, it does nothing, but you can’t test that.

db.Connect does return a value, but will connect the database even if it wasn’t connected before. In the code you posted, the Else block should never execute.

You are creating a local variable in DB_Ouvrir. The variable is not available in DB_Fermer. Use a property instead.