Problem Connecting To SQLite Example

I took the SQLiteEample program from the example projects folder and tried to add the following code to the open event on the SQLiteExample form to automate the process and load data if the data if the database exists and it seems to connect but I don’t think it is. What am I doing wrong?

[code] //
// Variables
//
Dim dbFile As FolderItem
Dim db As New SQLiteDatabase

	//
	// Connect To The Database
	//
	dbFile = GetFolderItem(SpecialFolder.Desktop.Child("example.sqlite").AbsolutePath)
	db.DatabaseFile = dbFile
	If db.Connect Then
			 mIsConnected = True
			CreateStatusLabel.Text = "OK."          //proceed with database operations here..
	Else
			 mIsConnected = False
			CreateStatusLabel.Text = "Error: " + db.ErrorMessage
	End If
	
	//
	// Load Then Data
	//
	If IsConnected then showdata

[/code]

is example.sqlite exist?

If not, you can add code to check the file and create the new one if not exist

If not db.DatabaseFile.Exists And Not db.CreateDatabaseFile Then
        MsgBox "Can't create database file"
        Return
End If

The database exists and I can open it using a SQLite browser app.

Ok, seems not problem with your code, but it’s better to add code to check the file.

In your code

dbFile = GetFolderItem(SpecialFolder.Desktop.Child("example.sqlite").AbsolutePath)

AbsolutePath is deprecated function, so replace it with NativePath.

FYI, this code

dbFile = GetFolderItem(SpecialFolder.Desktop.Child("example.sqlite").NativePath)

same with

dbFile = SpecialFolder.Desktop.Child("example.sqlite")

When I make the suggested changes I’m still getting nothing…it says it is connected but it isn’t.

My bet: you’ve created a local variable which will be out of scope once the open event has finished.

Without having tried myself: Maybe “db” should be the public App property App.DB?

code: Dim db As New SQLiteDatabase
(change to): App.DB = New SQLiteDatabase[/code]

Correct. You need to move db variable to window property or app property.

or you can replace this code

If IsConnected then showdata

with

If mIsConnected then showdata

or just change the last line to:

if IsConnected then App.DB = db showdata end if
(assing your successfully connected “db instance” to the one “showdata” is using, too)

Actually, if OP does’t change the isConnected function, it will look like this

If App.DB Is Nil Then
        mIsConnected = False
End If

Return mIsConnected

So, if isConnected then always False

ah yes - as i said: i just wrote my input out of my head (without having tried it).
then the second idea should probably be:

// Load Then Data // App.DB = db If IsConnected then showdata

[quote=316695:@Jürg Otter]ah yes - as i said: i just wrote my input out of my head (without having tried it).
then the second idea should probably be:

// Load Then Data // App.DB = db If IsConnected then showdata[/quote]
That should work

I’m not sure which suggestion was most important, but with a few changes and moving the showdata code into the method fixed it.