0

I got three SELECT statements connected with UNION ALL. I set up the compound statement such that sqlite3_prepare_v2() would prepare the resulting rows for me. If I were to make use of the resulting data, could I use a for() loop?

int i;
for(i = 0; sqlite3_step(res) == SQLITE_ROW; i++) {
    if (i == 0) data1 = sqlite3_column_int(res,0);
    else if (i == 1) data2 = sqlite3_column_int(res,0);
    else data3 = sqlite3_column_int(res,0);
}

Is this supposed to work? I've tried it but it gives me garbage data. Is there an alternative way to implement this?

4

1 回答 1

0

这行得通。垃圾数据完全来自其他地方。您可以使用它来缩短您键入的代码量,但请注意,您可以使用它的数量是有限制的。

附加信息:您可以使用每个UNION表的相同参数从不同的表中进行选择WHERE,假设您让用户输入两个数据参数,并且有四个表使用这些参数,仅通过一些附加值不同。假设我想获取该附加列的值。我会让用户输入与我想要的输出所属的表不同的东西。使用SELECT与 复合的语句UNION,可以比进行实际比较和冗长的迭代更容易地选择所需的输出。

于 2012-09-27T17:35:08.163 回答