本手册下载

X DevAPI用户指南/使用结果集/一次获取所有数据项

9.4一次获取所有数据项

除了使用模式fetchOne ()解释第9.3节,“处理数据集”X DevAPI还提供了一个使用模式fetchAll (),它将数据集的所有数据项作为列表传递给应用程序。不同的X DevAPI实现为其列表的编程语言使用适当的数据类型。由于使用了不同的数据类型,因此支持该语言的本机构造来访问列表元素。下面的示例假定测试模式存在,其中存在员工表myTable

MySQL Shell JavaScript代码

var myResult = myTable。选择([“名称”,“年龄”])。在哪里(名称:名称).bind('名称',' L %)。execute ();var myRows = myResult.fetchAll();for (index in myRows){print (myRows[index].name + " is " + myRows[index]。Age + " years old.");}

MySQL Shell Python代码

myResult = myTable。select(['name', 'age']) \ .where('name like:name').bind('name','L%') \ .execute() myRows = myResult.fetch_all() for row in myRows: print("%s is %s years old."% (row.name row.age))

node . js JavaScript代码

myTable。select(['name', 'age']) .where('name like:name') .bind('name', 'L%') .execute() .then(myResult => {var myRows = myResult. fetchall ();myRows。forEach(行= > {console.log(“${行[0]}${行[1]}年old. ');});});

c#代码

var myRows = myTable。Select("name", "age") . where ("name like:name")。绑定(“名字”、“L %”). execute ();var rows = myRows.FetchAll();

Python代码

结果= myTable。选择([“名称”,“年龄”])\。(“名称:名称”)。bind('name', 'L%') \ .execute() rows = result.fetch_all() for row in rows: print("{0} is {1} years old."。格式(行(“名字”),行["年龄"]))

Java代码

RowResult myRows = myTable。Select ("name, age") .where("name like:name")。绑定(“名字”、“L %”). execute ();List rows = myRows.fetchAll();for (Row Row: rows){//访问字段println(" Age: " + row.getInt(" Age ") + "\n");}

c++代码

RowResult myRows = myTable。选择(“姓名、年龄”)。(“名称:名称”).bind(“名字”、“L %”). execute ();std::list rows = myRows.fetchAll();for (Row Row: rows) {cout << Row [1] << endl;} //直接遍历行,而不将它们存储在容器中(Row Row: myRows.fetchAll()) {cout << Row [1] << endl;}

当混合fetchOne ()fetchAll ()要从一个数据集中读取,请记住每次调用fetchOne ()fetchAll ()使用返回的数据项。不能再次请求已使用的项。例如,如果应用程序调用fetchOne ()获取数据集的第一个数据项,然后对fetchAll ()返回第二个到最后一个数据项。所返回的数据项列表的第一个项不是fetchAll ()。同样,当调用fetchAll ()对于之前调用过的数据集,第二次调用返回一个空集合。

的使用fetchAll ()强制连接器在将列表作为整体传递给应用程序之前构建内存中所有项的列表。列表的生命周期独立于生成它的数据集的生命周期。

异步查询执行将在发出查询之后、在从服务器接收任何回复之前将控制权返回给调用方。调用fetchAll ()读取异步查询执行产生的数据项可能会阻塞调用者。fetchAll ()在从服务器读取结果之前不能将控制权返回给调用者。