简体   繁体   English

为什么我的光标无限循环到最后一行

[英]Why is my cursor infinitely looping on the last row

Hello I've been trying to use a cursor with embedded sql in c but I can't seem to get it to stop reading the last row in my table. 您好,我一直试图在C中使用带有嵌入式sql的游标,但似乎无法停止读取表中的最后一行。 The table is called publication with two attributes pubid and title. 该表称为发布,具有两个属性pubid和title。 I just want my cursor to iterate through and display the pubid. 我只希望光标遍历并显示pubid。 This is what i have: 这就是我所拥有的:

    EXEC SQL DECLARE C1 CURSOR FOR SELECT pubid FROM publication;
    EXEC SQL OPEN C1;
    while(SQLCODE !=100){
        EXEC SQL FETCH C1 INTO :pubid; //a host variable that was declared earlier
        cout<<pubid<<endl;
    }

When I run, it displays all the rows and infinitely repeats displaying the last row. 当我运行时,它将显示所有行,并无限重复显​​示最后一行。 I tried displaying the SQLCODE as well and it remains 0, so I'm not sure why the cursor doesn't move past the last row 我也尝试显示SQLCODE,它仍然为0,所以我不确定为什么光标没有移到最后一行

EXEC SQL DECLARE C1 CURSOR FOR SELECT pubid FROM publication;
EXEC SQL OPEN C1;
EXEC SQL WHENEVER NOT FOUND GOTO close_c1;
while(SQLCODE !=100) {
    EXEC SQL FETCH C1 INTO :pubid;
    cout<<pubid<<endl;
}
close_c1:
EXEC SQL CLOSE C1;

Something like this should work. 这样的事情应该起作用。 Also consider using EXEC SQL WHENEVER SQLERROR clean_up_function; 还可以考虑使用EXEC SQL WHENEVER SQLERROR clean_up_function; to be able to print out diagnostics. 以便打印出诊断信息。 I found references here . 我在这里找到参考。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM