[Gambas-user] Looking up data from a second table
T Lee Davidson
t.lee.davidson at gmail.com
Tue Feb 20 07:22:15 CET 2018
On 02/19/2018 07:47 PM, Christof Thalhofer wrote:
> Am 20.02.2018 um 00:03 schrieb T Lee Davidson:
>
>> I think the DataSource should be set programatically.
>
> For sure. This can be done easily in a module:
>
> --8<-------------------------------------------------------------------
>
> Property Read MyDB as Connection
> Private $MyDB as Connection
>
> Private Function MyDB_Read() As Connection
>
> If Not $MyDB.Opened Then
> With $MyDB
> .Type = "postgresql" ' sqlite3, mysql
> .Host = "hostname"
> .Name = "dbname"
> .Login = "username"
> .Password = "password123456"
> End With
> Open($MyDB)
> Endif
>
> Return $MyDB
> Catch
> Error.Propagate
>
> End
>
> --8<-------------------------------------------------------------------
>
> If you save this module with the name "DBs.module" then one can use the
> connection anywhere in the program like so:
>
> --8<-------------------------------------------------------------------
>
> Dim Qry as String
> Dim Res as Result
>
> Qry = "select str from anything;"
> Res = DBs.MyDB.Exec(Qry)
>
> --8<-------------------------------------------------------------------
>
> Or for a DataSource:
>
> --8<-------------------------------------------------------------------
>
> DataSource1.Connection = DBs.MyDB
> DataSource1.Table = "select this from that;"
>
> --8<-------------------------------------------------------------------
Nice.
And if one does not need to easily use the connection from multiple forms, the connection can simply be defined and opened in
the main form's Open event:
Public Sub Form_Open()
Dim hConn As Connection
hConn = New Connection("sqlite3://" & User.Home &/ "db.sqlite")
Try hConn.Open()
If Error Then
Debug "Cannot open database."
Return
Endif
DataSource1.Connection = hConn
DataSource1.Table = "select this from that;"
End
> For the problem of the OP: I was not able to display the data in a
> DataView. I do not understand how the DataView corresponds with the
> DataSource, beecause I do not work with gb.db.form.
The DataView should be a child of DataSource so that it 'inherits' the connection.
--
Lee
More information about the User
mailing list