Mysql query result access by field name
Steven Schveighoffer
schveiguy at yahoo.com
Mon May 21 14:17:23 UTC 2018
On 5/21/18 9:39 AM, kdevel wrote:
> On Sunday, 20 May 2018 at 16:08:03 UTC, ipkwena wrote:
>> How does one access the columns fields in a Mysql query results by the
>> column name.
>
> [...]
>
>> Data f;
>> f.name = to!string(allrows[0][0]);
>> f.surname = to!string(allrows[0][1]);
>> f.title = to!string(allrows[0][2]);
>>
>> I am using the mysql-native package or DB connectivity.
>
> According to the source code
> https://github.com/mysql-d/mysql-native/blob/master/source/mysql/result.d it
> should be possible to write the rowdata into the struct:
>
> Data f;
> allrows[0].toStruct (f);
>
> I haven't checked this.
This only works if your struct has exactly the same layout as the fields.
So if, for instance, your rows are selected "title", "name", "surname",
but your data type orders them name, surname, title, you won't be happy
with the result.
-Steve
More information about the Digitalmars-d-learn
mailing list