Thread overview | |||||||||
---|---|---|---|---|---|---|---|---|---|
|
December 07, 2015 Question about mysql-d Object | ||||
---|---|---|---|---|
| ||||
When I do the following: auto mysql = new Mysql("localhost", 3306, "mt", "", "verwaltung"); auto rows = mysql.query("select field from my_table limit 50"); foreach(row;rows){ writeln(row["field"]);} // second time same loop foreach(row;rows){ writeln(row["field"]);} I only get the output of the first loop (50 lines), probably this is a feature not a bug, but what kind of Object is rows? A nested loop, did not worked either: foreach(row;rows){ foreach(field;row){ writeln(field);} } Which other ways to access the elements of rows do I have? Sorry, but I am very new on D, (it may be the best language available, I don't know yet, but it is really the most interesting!) thank you! |
December 07, 2015 Re: Question about mysql-d Object | ||||
---|---|---|---|---|
| ||||
Posted in reply to Martin Tschierschke | On Monday, 7 December 2015 at 14:40:12 UTC, Martin Tschierschke wrote:
> When I do the following:
>
> auto mysql = new Mysql("localhost", 3306, "mt", "", "verwaltung");
> auto rows = mysql.query("select field from my_table limit 50");
>
> foreach(row;rows){
> writeln(row["field"]);}
>
> // second time same loop
>
> foreach(row;rows){
> writeln(row["field"]);}
>
> I only get the output of the first loop (50 lines), probably this is a feature
> not a bug, but what kind of Object is rows?
>
> A nested loop, did not worked either:
>
> foreach(row;rows){
> foreach(field;row){
> writeln(field);}
> }
>
> Which other ways to access the elements of rows do I have?
>
> Sorry, but I am very new on D, (it may be the best language available, I don't know yet, but it is really the most interesting!) thank you!
I suppose that's because the rows object is a mysql cursor, which means you can only iterate it once, and only forward (Not sure how it is implemented in mysql-d). It seems like your inner loop is exhausting it.
If you need random access or multiple iterations, you first need to copy your rows into another array/structure, and then you can do whatever you want with it.
|
December 07, 2015 Re: Question about mysql-d Object | ||||
---|---|---|---|---|
| ||||
Posted in reply to Martin Tschierschke | On Monday, 7 December 2015 at 14:40:12 UTC, Martin Tschierschke wrote:
> When I do the following:
>
> auto mysql = new Mysql("localhost", 3306, "mt", "", "verwaltung");
> auto rows = mysql.query("select field from my_table limit 50");
>
> foreach(row;rows){
> writeln(row["field"]);}
>
> // second time same loop
>
> foreach(row;rows){
> writeln(row["field"]);}
>
> I only get the output of the first loop (50 lines), probably this is a feature
> not a bug, but what kind of Object is rows?
>
> A nested loop, did not worked either:
>
> foreach(row;rows){
> foreach(field;row){
> writeln(field);}
> }
>
> Which other ways to access the elements of rows do I have?
>
> Sorry, but I am very new on D, (it may be the best language available, I don't know yet, but it is really the most interesting!) thank you!
what if you make array from it:
import std.array: array;
auto rows = mysql.query("select field from my_table limit 50").array;
|
December 08, 2015 Re: Question about mysql-d Object | ||||
---|---|---|---|---|
| ||||
Posted in reply to Daniel Kozak | On Monday, 7 December 2015 at 16:11:19 UTC, Daniel Kozak wrote: > On Monday, 7 December 2015 at 14:40:12 UTC, Martin Tschierschke wrote: >> When I do the following: >> >> auto mysql = new Mysql("localhost", 3306, "mt", "", "verwaltung"); >> auto rows = mysql.query("select field from my_table limit 50"); >> >> foreach(row;rows){ >> writeln(row["field"]);} >> >> // second time same loop >> >> foreach(row;rows){ >> writeln(row["field"]);} >> [...] >> A nested loop, did not worked either: >> >> foreach(row;rows){ >> foreach(field;row){ >> writeln(field);} >> } >> >> Which other ways to access the elements of rows do I have? >> [...] > what if you make array from it: > import std.array: array; > auto rows = mysql.query("select field from my_table limit 50").array; Thank you both for your answers! This worked, for the first part of my question. Now I took a work around, getting the field names in a separate mysql-request, but they should be available in the row object, too? |
December 08, 2015 Re: Question about mysql-d Object | ||||
---|---|---|---|---|
| ||||
Posted in reply to Martin Tschierschke | V Tue, 08 Dec 2015 14:34:53 +0000 Martin Tschierschke via Digitalmars-d-learn <digitalmars-d-learn@puremagic.com> napsáno: > On Monday, 7 December 2015 at 16:11:19 UTC, Daniel Kozak wrote: > > On Monday, 7 December 2015 at 14:40:12 UTC, Martin Tschierschke wrote: > >> When I do the following: > >> > >> auto mysql = new Mysql("localhost", 3306, "mt", "", > >> "verwaltung"); > >> auto rows = mysql.query("select field from my_table limit 50"); > >> > >> foreach(row;rows){ > >> writeln(row["field"]);} > >> > >> // second time same loop > >> > >> foreach(row;rows){ > >> writeln(row["field"]);} > >> > [...] > >> A nested loop, did not worked either: > >> > >> foreach(row;rows){ > >> foreach(field;row){ > >> writeln(field);} > >> } > >> > >> Which other ways to access the elements of rows do I have? > >> > [...] > > what if you make array from it: > > import std.array: array; > > auto rows = mysql.query("select field from my_table limit > > 50").array; > Thank you both for your answers! > This worked, for the first part of my question. > Now I took a work around, getting the field names in a separate > mysql-request, > but they should be available in the row object, too? > rows.fields(); |
December 08, 2015 Re: Question about mysql-d Object | ||||
---|---|---|---|---|
| ||||
Posted in reply to Martin Tschierschke | V Tue, 08 Dec 2015 14:34:53 +0000 Martin Tschierschke via Digitalmars-d-learn <digitalmars-d-learn@puremagic.com> napsáno: > On Monday, 7 December 2015 at 16:11:19 UTC, Daniel Kozak wrote: > > On Monday, 7 December 2015 at 14:40:12 UTC, Martin Tschierschke wrote: > >> When I do the following: > >> > >> auto mysql = new Mysql("localhost", 3306, "mt", "", > >> "verwaltung"); > >> auto rows = mysql.query("select field from my_table limit 50"); > >> > >> foreach(row;rows){ > >> writeln(row["field"]);} > >> > >> // second time same loop > >> > >> foreach(row;rows){ > >> writeln(row["field"]);} > >> > [...] > >> A nested loop, did not worked either: > >> > >> foreach(row;rows){ > >> foreach(field;row){ > >> writeln(field);} > >> } > >> > >> Which other ways to access the elements of rows do I have? > >> > [...] > > what if you make array from it: > > import std.array: array; > > auto rows = mysql.query("select field from my_table limit > > 50").array; > Thank you both for your answers! > This worked, for the first part of my question. > Now I took a work around, getting the field names in a separate > mysql-request, > but they should be available in the row object, too? > rows.fieldNames() this one is better result type is string[] |
December 08, 2015 Re: Question about mysql-d Object | ||||
---|---|---|---|---|
| ||||
Posted in reply to Daniel Kozak | On Tuesday, 8 December 2015 at 15:14:06 UTC, Daniel Kozak wrote: [...] >> >> A nested loop, did not worked either: >> >> >> >> foreach(row;rows){ >> >> foreach(field;row){ >> >> writeln(field);} >> >> } [...] >> Now I took a work around, getting the field names in a separate >> mysql-request, >> but they should be available in the row object, too? >> > > rows.fieldNames() this one is better result type is string[] Thank you!!! |
Copyright © 1999-2021 by the D Language Foundation