Gambas sqlite
Posted
#1
(In Topic #135)
Trainee
Posted
Regular

This is the simplest code I can think of to get you started;
Create a new project and add a textArea to the Form. Enter this text:-
Code
' 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
When you run this code using a valid database, the textArea should list your database tables.
Posted
Trainee
Posted
Regular

"Unable to locate database `/home/steve/Gambas/my-tv/me-tv.db` in ``"
When I altered mine to move the path portion of the name into the Host property (in a manner similar to what is shown below), then it opened just fine.
Code (gambas)
- ' Gambas class file
- dbConnection.Host = "/home/steve/Gambas/my-tv/"
- dbConnection.Name = "me-tv.db" 'the path to a valid database
- dbConnection.Type = "sqlite3"
- TextArea1.Clear()
- ' display each database table name
- TextArea1.Text &= myTable.Name & gb.CrLf
stevedee said
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;
[…]
Posted
Regular

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."
Posted
Regular

Perhaps, since this is a server-less (or in their words 'engine-less file-based') database, they felt they needed to re-purpose the Host property.
Anyway, here is the help for SQLite, where they are even suggesting 'Application.path' for the value:
http://gambaswiki.org/wiki/howto/databasesqlite?nl
stevedee said
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."
Posted
Administrator

A few things…
When setting the ".host" path, no need to use a trailing slash ("/").
When putting the db file in a user's home directory, set the ".host" path by using "User.Home" (instead of hard coding the path).
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 ("/").
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.
MGlobal.module file…
Code (gambas)
- ' Gambas module file
- ' directory and file paths
- ' database
MDatabase.module file…
Code (gambas)
- ' Gambas module file
- ' db.Debug = True
- .Type = "sqlite3"
- .Host = MGlobal.sDataPath
- .Name = MGlobal.sDBName
Posted
Regular

Got2BeFree said
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.
1 guest and 0 members have just viewed this.


