如何从 MySQL 行类型创建数组?

How to make array from MySQL ROW type?

我正在使用 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])。 所以每次为了获得价值,我都需要做很多转换,比如:

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 也可能是一个错误,请将查询的那部分内容让数据库执行,这样可能会更有效且更易于阅读。