Gambas sqlite
Posted: Wednesday 27th September 2017 8:22am
How to use sqlite in gambas? I'm using gambas in raspberry pi and DB browser for Sqlite.
Code: Select all
' Gambas class file
Private dbConnection As New Connection
Public Sub Form_Open()
Dim myTable As Table
dbConnection.Name = "/home/steve/Gambas/my-tv/me-tv.db" 'the path to a valid database
dbConnection.Type = "sqlite3"
TextArea1.Clear()
Try dbConnection.Open()
' display each database table name
For Each myTable In dbConnection.Tables
TextArea1.Text &= myTable.Name & gb.CrLf
Next
End
' Gambas class file
Private dbConnection As New Connection
Public Sub Form_Open()
Dim myTable As Table
dbConnection.Host = "/home/steve/Gambas/my-tv/"
dbConnection.Name = "me-tv.db" 'the path to a valid database
dbConnection.Type = "sqlite3"
TextArea1.Clear()
Try dbConnection.Open()
' display each database table name
For Each myTable In dbConnection.Tables
TextArea1.Text &= myTable.Name & gb.CrLf
Next
End
stevedee wrote: ↑Wednesday 27th September 2017 10:45am Thats a big question, and I don't know how much you know about Gambas & Sqlite, but you will need to include the gb.db component and if you want to use Data Bound controls gb.db.form.
This is the simplest code I can think of to get you started;
[...]
stevedee wrote: ↑Friday 13th December 2019 11:09pm This is a bit odd. When I wrote my original sqlite3 database code in 2009, it certainly worked with .Host="" and .Name={full path + file name}
The Gambas Documentation now show .Host =path and .Name is just the file name.
However, the documentation still suggests that .Host is a computer name or IP address (which is normally what "host" implies):-
"Returns or sets the host where the database server resides.
This host can be a machine name, or an IP address. The default host is localhost."
With hDB 'open the database
.Type = "sqlite3"
.Host = User.Home &/ "Gambas/my-tv"
.Name = "me-tv.db"
End With
When putting the db file in a directory outside of the user's home directory (such as an NFS shared directory), omit "User.Home" and add an absolute path with a beginning slash ("/").
With hDB 'open the database
.Type = "sqlite3"
.Host = /path/to/shared/directory/Gambas/my-tv"
.Name = "me-tv.db"
End With
' Gambas module file
' directory and file paths
Public sBaseDir As String = ".WhiteIslandSoftware"
Public sConfigDir As String = "Config"
Public sDataDir As String = "Data"
Public sAppDir As String = Application.Name
Public sConfigFilename As String = sAppDir & ".conf"
Public sAppDirPath As String = User.Home &/ sBaseDir &/ sAppDir
Public sConfigPath As String = User.Home &/ sBaseDir &/ sAppDir &/ sConfigDir
Public sDataPath As String = User.Home &/ sBaseDir &/ sAppDir &/ sDataDir
' database
Public hResData As Result
Public hDB As New Connection
Public sDBName As String = "x31"
MDatabase.module file...
' Gambas module file
Public Sub DBConnect()
' db.Debug = True
If Not Exist(MGlobal.sDataPath) Then FMain.CheckDirIntegrity()
If Not Exist(MGlobal.sDataPath &/ MGlobal.sDBName) Then DBInitialize() ' Return
Try MGlobal.hDB.Close
With MGlobal.hDB 'open the database
.Type = "sqlite3"
.Host = MGlobal.sDataPath
.Name = MGlobal.sDBName
End With
Try MGlobal.hDB.Open()
End
Got2BeFree wrote: ↑Saturday 14th December 2019 5:50pm
This is how I set up my sqlite databases. (This is from my event reminder app.) If I need to have the db in a NFS location, I can easily change the sBaseDir string variable and drop the "User.Home &/" from the xxxPath string variables. When creating a new db app, I just copy these 2 files into the new app and make a few changes.