[Gambas-user] automatic get key from resultsets in collection

Tobias Boege taboege at ...626...
Tue Mar 14 20:25:01 CET 2017


On Tue, 14 Mar 2017, PICCORO McKAY Lenz wrote:
> how can i made?
> 
> i wish to handle the resultsets like php does:
> 
> foreach ($resultobjusuario as $column => $row)
>                         $arraydata[$column] = $row;
> 
> this made me a map (like collections in gambas) where the index key its the
> column table name, and the value its the column currentl field value.
> 
> any ideas?, please without made a class for the results! KISS rules
> 

The Result object has a Fields attribute. You use it like this:

  '' Return the record ~hResult~ points to as a Collection. Keys are the column names.
  Private Sub ResultToCollection(hRes As Result) As Collection
    Dim cMap As New Collection
    Dim hField As ResultField

    For Each hField In hRes.Fields
      cMap[hField.Name] = hRes[hField.Name]
    Next
    Return cMap
  End

But you have to be careful with what the database driver gives you as the
column name. Testing this with an sqlite3 database, I got column names like
"id, name" when I did a "SELECT *" but I got "table.id, table.name" when I
did an explicit "SELECT id,name". So depending on the SELECT, it had the
table name and a dot prepended. There seems to be a PRAGMA [1] to control
this behaviour in sqlite3 but it's deprecated. Don't know about other DBMS.

Regards,
Tobi

[1] https://sqlite.org/pragma.html#pragma_short_column_names

-- 
"There's an old saying: Don't change anything... ever!" -- Mr. Monk




More information about the User mailing list