Gambas sqlite

Post

Posted
Rating:
#1 (In Topic #135)
Trainee
 How to use sqlite in gambas? I'm using gambas in raspberry pi and DB browser for Sqlite.
Online now: No Back to the top

Post

Posted
Rating:
#2
Avatar
Regular
stevedee is in the usergroup ‘Regular’
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;

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.
Online now: No Back to the top

Post

Posted
Rating:
#3
Trainee
 Thank you very much! I'l try the code.
Online now: No Back to the top

Post

Posted
Rating:
#4
Avatar
Regular
sjsepan is in the usergroup ‘Regular’
I tried this out today too, since I'm finally looking at db access in Gambas. Trying the code as is (with my own project and database paths) I got the equivalent of the following error (I say equivalent because I have edited it to show what it would look like with the path information from the example given):

"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)

  1. ' Gambas class file
  2.  
  3. Private dbConnection As New Connection
  4.  
  5. Public Sub Form_Open()
  6. Dim myTable As Table
  7.  
  8.   dbConnection.Host = "/home/steve/Gambas/my-tv/"
  9.   dbConnection.Name = "me-tv.db" 'the path to a valid database
  10.   dbConnection.Type = "sqlite3"
  11.  
  12.   TextArea1.Clear()
  13.   Try dbConnection.Open()
  14.   ' display each database table name
  15.   For Each myTable In dbConnection.Tables
  16.     TextArea1.Text &= myTable.Name & gb.CrLf
  17.   Next
  18.  

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;
[…]

Online now: No Back to the top

Post

Posted
Rating:
#5
Avatar
Regular
stevedee is in the usergroup ‘Regular’
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."
Online now: No Back to the top

Post

Posted
Rating:
#6
Avatar
Regular
sjsepan is in the usergroup ‘Regular’
Agreed, that is how I've always though of host.
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."

Online now: No Back to the top

Post

Posted
Rating:
#7
Avatar
Administrator
sholzy is in the usergroup ‘unknown’
I haven't created a mySQL db in about 10 years since SQLite is all I've needed for my apps. If you think of ".Host" as a "location" and not just as a path, machine name, or IP address, and use a variable to hold that "location", you should be able to craft your db access code to be swap-able between mySQL and SQLite with minimum effort. There used to be a blurb on this in the wiki, but it looks like db access has been rewritten in the wiki for both SQLite and mySQL.

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).

Code (gambas)

  1. With hDB      'open the database
  2.         .Type = "sqlite3"
  3.         .Host = User.Home &/ "Gambas/my-tv"
  4.         .Name = "me-tv.db"
  5.  

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 ("/").

Code (gambas)

  1. With hDB      'open the database
  2.         .Type = "sqlite3"
  3.         .Host = /path/to/shared/directory/Gambas/my-tv"
  4.        .Name = "me-tv.db"
  5. End With
  6.  



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)

  1. ' Gambas module file
  2.  
  3. ' directory and file paths
  4. Public sBaseDir As String = ".WhiteIslandSoftware"
  5. Public sConfigDir As String = "Config"
  6. Public sDataDir As String = "Data"
  7. Public sAppDir As String = Application.Name
  8. Public sConfigFilename As String = sAppDir & ".conf"
  9. Public sAppDirPath As String = User.Home &/ sBaseDir &/ sAppDir
  10. Public sConfigPath As String = User.Home &/ sBaseDir &/ sAppDir &/ sConfigDir
  11. Public sDataPath As String = User.Home &/ sBaseDir &/ sAppDir &/ sDataDir
  12.  
  13. ' database
  14. Public hResData As Result
  15. Public sDBName As String = "x31"
  16.  

MDatabase.module file…

Code (gambas)

  1. ' Gambas module file
  2.  
  3. Public Sub DBConnect()
  4.  
  5. ' db.Debug = True
  6.  
  7.     If Not Exist(MGlobal.sDataPath) Then FMain.CheckDirIntegrity()
  8.     If Not Exist(MGlobal.sDataPath &/ MGlobal.sDBName) Then DBInitialize()       '  Return
  9.     Try MGlobal.hDB.Close
  10.  
  11.     With MGlobal.hDB      'open the database
  12.         .Type = "sqlite3"
  13.         .Host = MGlobal.sDataPath
  14.         .Name = MGlobal.sDBName
  15.     End With
  16.  
  17.     Try MGlobal.hDB.Open()
  18.  
  19.  

sholzy
Gambas One Site Director

To report bugs in the Gambas IDE:
Official Gambas Bug Tracker
Online now: No Back to the top

Post

Posted
Rating:
#8
Avatar
Regular
sjsepan is in the usergroup ‘Regular’
Good path tips, Thanks!

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.
Online now: No Back to the top
1 guest and 0 members have just viewed this.