[Gambas-user] Extracting fields from a Result

Tobias Boege taboege at gmail.com
Sun Dec 31 21:16:44 CET 2017


On Sun, 31 Dec 2017, PICCORO McKAY Lenz wrote:
> its a bug in the rest of drivers, i confirmed some time ago... due "too
> much mysql-like" development, its like ms-like only tests and assume all
> are done..
> 

Are you nuts? A nonsensical error from the postgres driver is not a bug
in all the other drivers, just because you suspect us to conspire with
MySQL and Microsoft.

And I, for one, think it would be a great step forward already if Gambas
got a comprehensive test suite for everything that is in the source tree.

On Sun, 31 Dec 2017, T Lee Davidson wrote:
> Yes, MyResult!txtMasterDatabase should work. (Did you copy/paste that name, or type it in directly?) The For Each loop should
> work as well.
> 
> I tried a simple command-line application on a MySQL data table with just one row. All the application does is, first connect to
> the database, and then 'MyResult = hConn.Exec("select * from users")'
> 
> A For Each loop prints the field names just fine. And, 'Print MyResult!id' displays the correct value.
> 
> Perhaps there is a bug in the Gambas PostgreSQL driver. Can you test on a MySQL table to see if that works for you?
> 

I agree that the original code should have worked. See the attached
script which uses an in-memory SQLite3 database to demonstrate that
it works with another driver.

A (minimal!) project and database dump, just enough to reproduce the
behaviour, would be helpful.

Regards,
Tobi

-- 
"There's an old saying: Don't change anything... ever!" -- Mr. Monk
-------------- next part --------------
#!/usr/bin/env gbs3

Use "gb.db.sqlite3"

Public Sub Main()
  Dim hConn As New Connection

  ' Connect to a temporary in-memory database
  hConn.Type = "sqlite3"
  hConn.Name = Null
  hConn.Open()

  ' Create test table
  With hConn.Tables.Add("test")
    .Fields.Add("txtMasterDatabase", db.String)
    .Fields.Add("numRandom", db.Integer)
    .Update()
  End With

  ' Add some data
  With hConn.Create("test")
    !txtMasterDatabase = "abc"
    !numRandom = Rand(0, 100)
    .Update
  End With
  With hConn.Create("test")
    !txtMasterDatabase = "xyz"
    !numRandom = Rand(100, 200)
    .Update
  End With

  DoQuery(hConn)
End

Private Sub DoQuery(hConn As Connection)
  Dim hRes As Result
  Dim hField As ResultField

  ' XXX: Better use Connection.Find(), or the SQLRequest class for
  ' more complex queries. They keep your Gambas code independent of
  ' a choice of DBMS.
  hRes = hConn.Exec("SELECT * FROM test")

  ' Result.Fields contains objects representing the fields present
  ' in the result, from a table schema point of view.
  Print "Fields:"
  For Each hField In hRes.Fields
    Print Space$(2); hField.Name
  Next
  Print

  ' The actual result data is held by the Result object itself.
  Print "Values:"
  For Each hRes
    Print Space$(2); hRes!txtMasterDatabase, hRes!numRandom
  Next
End


More information about the User mailing list