gpt4 book ai didi

mysql - 如何从 MySQL ROW 类型创建数组?

转载 作者:行者123 更新时间:2023-11-29 10:52:42 26 4
gpt4 key购买 nike

我正在使用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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com