2012-11-26 27 views
1

是否有可能重新使用sqlite3的输出我有这样的查询:sqlite3_step:如何多次解析输出?

  error = sqlite3_prepare_v2(conn,SQL_STMT_GET_FILE,-1, &res, &tail); 
      error = sqlite3_bind_text(res, 1,file,strlen(file), SQLITE_STATIC); assert(error == SQLITE_OK); 
      if (error != SQLITE_OK) { 
      handle_error("No matching record found."); 

和结果分析

while (sqlite3_step(res) == SQLITE_ROW) 
       //do something here with results for device X 

现在我想重新使用输出“水库”例如,代码流将会像

while (sqlite3_step(res) == SQLITE_ROW) 
       //do something here with results for device X 


    //reset the cursor to starting position and scan through the records. 

    while (sqlite3_step(res) == SQLITE_ROW) 
       //do something here with results for device Y 

如何实现此目的?我不喜欢第二次重新运行同一个sql语句并获取结果。

回答

2

您必须自己缓存数据,或者重新运行查询。

阅读的原因on this page about Sqlite and Scrolling Cursors,包括:

的问题是,sqlite3_step()函数不经过预先计算的结果集的所有步骤。更好和更现实的方式来思考问题是假设每个准备好的声明实际上是一个计算机程序。您正在调试器中运行此程序,并且在计算内部的某个语句中设置了一个断点。调用sqlite3_step()函数就像在调试器中按下“运行”按钮,从而要求调试器运行该程序,直到它退出或击中断点。如果它完成,Sqlite3_step()返回SQLITE_ROW,如果它遇到了断点,则返回SQLITE_DONE。如果你打断点,你可以看看局部变量,以便找到“行”的值。然后你可以再次按下“运行”按钮(调用sqlite3_step())继续执行,直到下一个断点或程序退出。