我正在使用mysql-native driver 。我的代码:
ResultRange MySQLTablesRange = mysqlconnection.query(`SELECT historysensor FROM TableName`);
auto historysensor = MySQLTablesRange.array.filter!(a=>a[0].coerce!string.canFind("historysensor"));
但是在发送字符串时,我得到的 historysensor
不是一个数组,而是如下结构:Row([historysensor_10774], [false])
。
因此,每次为了获得值(value),我都需要进行大量的转换,例如:
foreach(sensor;historysensor)
{
writeln(sensor[0].coerce!string.split("_")[1]);
}
如何将 historysensor
制作为简单数组,以便能够在没有 [0].coerce!string
的情况下工作?
最佳答案
你可以在做其他事情之前映射它
auto historysensor = MySQLTablesRange.array.
map!(a => a[0].coerce!string). // this is new
filter!(a.canFind("historysensor"). // no need for coerce here now
array; // convert to plain array of strings
顺便说一句,这里使用 filter
也可能是一个错误,将这部分作为查询的一部分,以便数据库执行此操作,这可能会更有效且更易于阅读。
关于mysql - 如何从 MySQL ROW 类型创建数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43452554/