automatic get key from resultsets in collection

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

automatic get key from resultsets in collection

PICCORO McKAY Lenz
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

Lenz McKAY Gerardo (PICCORO)
http://qgqlochekone.blogspot.com
------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
Gambas-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/gambas-user
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: automatic get key from resultsets in collection

Tobias Boege-2
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

------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
Gambas-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/gambas-user
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: automatic get key from resultsets in collection

PICCORO McKAY Lenz
2017-03-14 15:25 GMT-04:00 Tobias Boege <[hidden email]>:

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

 i confirmed.. so i made a little modification:

cMap[hField.Name] = hRes[ Replace( hField.Name, Tablename & ".", "") ]

with this wil be usefully a way to got the current table without already
know the query table...
but in any case that's work!

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.
>
this also happened in any DBMS

this behavior are correct and good, its usefully for table references and
joins in internals


>
> Regards,
> Tobi
>

Many many thanks Tobias
------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
Gambas-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/gambas-user
Loading...