首页 新闻 会员 周边

求救!C++的OTL库otl_stream不能正常结束的问题

0
悬赏园豆:10 [已解决问题] 解决于 2015-05-15 18:22

根据说明文档otl_stream类构造函数的第一个参数是缓存区中能存放查询结果的行数,按正常理解,如果表中有100条记录,我将该参数设置50条应该是可以的,但实际上不行,在读第50条的f2的时候就报错,所以我将该参数设置到200,但是一样读到第100行的f2的时候报错,错误与设置50是一样的,不同只是一个在读第50行最后一列的值时报错,一个在读第100行最后一列的值时报错,且otl_exception中所有属性成员都为空字符串!我将otl源码中的抛出异常的那一句去掉(这是下策,可能造成严重的潜在问题,但是我想看一下结果,而且只是去掉一个throw语句),我想总该没有错误了吧?合理的话,到第100行之后,i.eof()语句应该为false,但是实际却仍是true,所以程序继续执行循环然后报错,看来简单修改一下otl源码也不行,这如何解决呢?很急,望有高手能救一把!

void select() {

    otl_stream i(50, "select f1,f2 from test_tab", db);

     float f1, f2;

     while (!i.eof()){ //不知为何i.eof()总是为true?

       //i.set_flush(true); //此句加与不加是一样的

       i>>f1>>f2;

       cout<<"f1="<<f1<<", f2="<<f2<<endl;

 } }

我用的是SqlServer2005,otl版本4.0.334(最新的)

Patrickz10的主页 Patrickz10 | 初学一级 | 园豆:7
提问于:2015-05-15 11:23
< >
分享
最佳答案
0

看一下这里的示例代码,在whil(!i.eof())之前进行了额外的操作(i<<8<<8;):

void select()
{ 
 otl_stream i(50, // buffer size
              "select * from test_tab where f1>=:f11<int> and f1<=:f12<int>*2",
                 // SELECT statement
              db // connect object
             ); 
   // create select stream
 
 float f1;
 char f2[31];

 i<<8<<8; // assigning :f11 = 8, :f12=8
   // SELECT automatically executes when all input variables are
   // assigned. First portion of output rows is fetched to the buffer

 while(!i.eof()){ // while not end-of-data
  i>>f1;
  if(i.is_null())
   cout<<"f1=NULL,";
  else
   cout<<"f1="<<f1<<",";
  i>>f2;
  if(i.is_null())
   cout<<"f2=NULL";
  else
   cout<<"f2="<<f2;
  cout<<endl;
 }

}
收获园豆:10
dudu | 高人七级 |园豆:30994 | 2015-05-15 11:35

这个额外的操作是设置示例中sql语句(“where f1>=:f11<int> and f1<=:f12<int>*2”)中的参数,而我的sql语句中没有where和后面的条件,不需要参数,如果我加上去,会报错:“No input variables have been defined in SQL statement”

Patrickz10 | 园豆:7 (初学一级) | 2015-05-15 12:15

@Patrickz10: 或者换成这里的读取方法:

void select()
{ 
 otl_stream i(50, // buffer size
              "select * from test_tab "
             "where f1>=:f11<int> and f1<=:f12<int>*2",
                 // SELECT statement
              db // connect object
             ); 
   // create select stream
 
 int f1;
 char f2[31];
 otl_stream_read_iterator<otl_stream,otl_exception,otl_lob_stream> rs;

 rs.attach(i); // attach the iterator "rs" to the stream "i".
 i<<8<<8; // assigning :f11 = 8, :f12 = 8
   // SELECT automatically executes when all input variables are
   // assigned. First portion of output rows is fetched to the buffer

 while(rs.next_row()){ // while not end-of-data
  rs.get(1,f1);
  rs.get(2,f2);
  cout<<"f1="<<f1<<", f2="<<f2<<endl;
 }

 rs.detach(); // detach the itertor from the stream

 i<<4<<4; // assigning :f11 = 4, :f12 = 4
   // SELECT automatically executes when all input variables are
   // assigned. First portion of output rows is fetched to the buffer

 while(!i.eof()){ // while not end-of-data
  i>>f1>>f2;
  cout<<"f1="<<f1<<", f2="<<f2<<endl;
 }

}
dudu | 园豆:30994 (高人七级) | 2015-05-15 12:36

@dudu: 改好了,暂时未查明原因,估计是某个参数变量(如db是局部变量),与select不在同一函数作用域,然后程序正常运行,又不知道什么时候程序才发现这个变量无效了,就将某个状态变量设置为无效,但是直到运行到超出缓存才去检查这个状态,结果就运行不下去了,初步理解是这样!

Patrickz10 | 园豆:7 (初学一级) | 2015-05-15 18:22
其他回答(1)
0

改成 i>>f1>>f2>>endr; 或 i.move_next();试试

线木偶 | 园豆:8 (初学一级) | 2016-01-18 21:19
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册