To 'sqlite3_step' or not to 'sqlite3_step' as is the case...

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

To 'sqlite3_step' or not to 'sqlite3_step' as is the case...

Terry MacDonald-2
The following code is part of a class member function that loads data
from an existing database. The database has been opened and there IS
data in the target table.  I have had it working with callbacks, but
using the prepare/step/finalise approach below I consistently get the
log message:

Step : ERROR - not an error

i.e. the 'sqlite3_step' loop returns only once indicating an error but
the error message says 'not an error'. Can anyone spot my silly error?

--------------------------------------------------------------------
string sql = "SELECT name,value FROM setting;" ;
sqlite3_stmt *pStmt ;

LOGLN("SQL: "+sql) ;
if( sqlite3_prepare( db, sql.c_str(), -1, &pStmt, 0 ) != SQLITE_OK )
{
    LOGLN(string("Prepare: ERROR -")+sqlite3_errmsg(db)) ;
}
else
{
    int rc ;
    while( rc=sqlite3_step(pStmt) != SQLITE_DONE )
    {
        if( rc == SQLITE_ROW )
        {
            string name = (const char*)(sqlite3_column_text(pStmt,0)) ;
            string value = (const char*)(sqlite3_column_text(pStmt,1)) ;
            settings[name] = value.c_str() ? value : "" ;
            LOGLN(name+"="+value) ;
        }
        else if( rc == SQLITE_BUSY )
        {
            LOGLN("Step: BUSY") ;
            continue ;
        }
        else if( rc == SQLITE_ERROR )
        {
            LOGLN(string("Step : ERROR - ")+sqlite3_errmsg(db)) ;
            break ;
        }
        else if( rc == SQLITE_MISUSE )
        {
            LOGLN("Step: MISUSE") ;
            break ;
        }
    }
}
sqlite3_finalize(pStmt) ;


Reply | Threaded
Open this post in threaded view
|

Re: To 'sqlite3_step' or not to 'sqlite3_step' as is the case...

Dan Kennedy
My hunch is that you need to change the while() line to:
 
while ( (rc = sqlite3_step(pStmt)) != SQLITE_DONE )
 
But I could be wrong, and I don't have a computer with a C compiler to test
this right now.
 


Terence MacDonald <[hidden email]> wrote:
The following code is part of a class member function that loads data
from an existing database. The database has been opened and there IS
data in the target table. I have had it working with callbacks, but
using the prepare/step/finalise approach below I consistently get the
log message:

Step : ERROR - not an error

i.e. the 'sqlite3_step' loop returns only once indicating an error but
the error message says 'not an error'. Can anyone spot my silly error?

--------------------------------------------------------------------
string sql = "SELECT name,value FROM setting;" ;
sqlite3_stmt *pStmt ;

LOGLN("SQL: "+sql) ;
if( sqlite3_prepare( db, sql.c_str(), -1, &pStmt, 0 ) != SQLITE_OK )
{
LOGLN(string("Prepare: ERROR -")+sqlite3_errmsg(db)) ;
}
else
{
int rc ;
while( rc=sqlite3_step(pStmt) != SQLITE_DONE )
{
if( rc == SQLITE_ROW )
{
string name = (const char*)(sqlite3_column_text(pStmt,0)) ;
string value = (const char*)(sqlite3_column_text(pStmt,1)) ;
settings[name] = value.c_str() ? value : "" ;
LOGLN(name+"="+value) ;
}
else if( rc == SQLITE_BUSY )
{
LOGLN("Step: BUSY") ;
continue ;
}
else if( rc == SQLITE_ERROR )
{
LOGLN(string("Step : ERROR - ")+sqlite3_errmsg(db)) ;
break ;
}
else if( rc == SQLITE_MISUSE )
{
LOGLN("Step: MISUSE") ;
break ;
}
}
}
sqlite3_finalize(pStmt) ;



               
---------------------------------
 Yahoo! Music Unlimited - Access over 1 million songs. Try it free.
Reply | Threaded
Open this post in threaded view
|

Re: To 'sqlite3_step' or not to 'sqlite3_step' as is the case...

John Stanton-3
In reply to this post by Terry MacDonald-2
This works -

... just after database is opened, compile the SQL statement
  if (sqlite3_prepare(db, dbst_cart_wt_in_sel,
                       -1,
                       &dbcmp_cart_wt_in_sel,
                       &dbend_cart_wt_in_sel) != SQLITE_OK) {
     errormet("908",  (char *)sqlite3_errmsg(db)); /*Fatal DB Error.*/
     return(TRUE);
   }  /*if*/

... called to access SELECT, after a bind.
   /*Now we execute the SQL statement.  Handle the possibility that
   sqlite is busy, but drop out after a number of attempts.*/
   busy_count = 0;
   found = 0;
   while (TRUE) {
     rc = sqlite3_step(dbcmp_cart_wt_in_sel);

     switch (rc) {
       case SQLITE_BUSY:    /*We must try again, but not forever.*/
         if (busy_count++ > MAX_BUSY_TRIES) {
           sqlite3_reset(dbcmp_cart_wt_in_sel);
           errormet("908", "DB locked busy");
           return(-1);
         }  /*if*/
#if WIN32
         sleep(0); /*Relinquish time slice for gentler polling.*/
#else
         yield();  /*Drop time slice*/
#endif
         break;
       case SQLITE_DONE:    /*Success on multi row read.*/
         sqlite3_reset(dbcmp_cart_wt_in_sel); /*Ready for next step.*/
         return(found);
       case SQLITE_ROW:     /*A row has been found.*/
.... extract your row data
         found++;
         break;
       case SQLITE_ERROR:   /*Run time error, discard the VM.*/
         sqlite3_reset(dbcmp_cart_wt_in_sel);
         sprintf(strg, "SQL SELECT error: %s\n", sqlite3_errmsg(db));
         errormet("908", strg);
         return(-1);
       case SQLITE_MISUSE:  /*VM should not have been used.*/
         sqlite3_reset(dbcmp_cart_wt_in_sel);
         errormet("908", "SQLITE_MISUSE");
         return(-1);
         break;
       default:
         sqlite3_reset(dbcmp_cart_wt_in_sel);
         errormet("908", "Unexpected return from sqlite3_step");
         return(-1);
     }  /*switch*/
   }    /*while*/


Terence MacDonald wrote:

> The following code is part of a class member function that loads data
> from an existing database. The database has been opened and there IS
> data in the target table.  I have had it working with callbacks, but
> using the prepare/step/finalise approach below I consistently get the
> log message:
>
> Step : ERROR - not an error
>
> i.e. the 'sqlite3_step' loop returns only once indicating an error but
> the error message says 'not an error'. Can anyone spot my silly error?
>
> --------------------------------------------------------------------
> string sql = "SELECT name,value FROM setting;" ;
> sqlite3_stmt *pStmt ;
>
> LOGLN("SQL: "+sql) ;
> if( sqlite3_prepare( db, sql.c_str(), -1, &pStmt, 0 ) != SQLITE_OK )
> {
>     LOGLN(string("Prepare: ERROR -")+sqlite3_errmsg(db)) ;
> }
> else
> {
>     int rc ;
>     while( rc=sqlite3_step(pStmt) != SQLITE_DONE )
>     {
>         if( rc == SQLITE_ROW )
>         {
>             string name = (const char*)(sqlite3_column_text(pStmt,0)) ;
>             string value = (const char*)(sqlite3_column_text(pStmt,1)) ;
>             settings[name] = value.c_str() ? value : "" ;
>             LOGLN(name+"="+value) ;
>         }
>         else if( rc == SQLITE_BUSY )
>         {
>             LOGLN("Step: BUSY") ;
>             continue ;
>         }
>         else if( rc == SQLITE_ERROR )
>         {
>             LOGLN(string("Step : ERROR - ")+sqlite3_errmsg(db)) ;
>             break ;
>         }
>         else if( rc == SQLITE_MISUSE )
>         {
>             LOGLN("Step: MISUSE") ;
>             break ;
>         }
>     }
> }
> sqlite3_finalize(pStmt) ;
>
>

Reply | Threaded
Open this post in threaded view
|

Re: To 'sqlite3_step' or not to 'sqlite3_step' as is the case...

Terry MacDonald-2
In reply to this post by Dan Kennedy
Dan Kennedy wrote:

>My hunch is that you need to change the while() line to:
>
>while ( (rc = sqlite3_step(pStmt)) != SQLITE_DONE )
>
>But I could be wrong, and I don't have a computer with a C compiler to test
>this right now.
>
>
>
>Terence MacDonald <[hidden email]> wrote:
>The following code is part of a class member function that loads data
>from an existing database. The database has been opened and there IS
>data in the target table. I have had it working with callbacks, but
>using the prepare/step/finalise approach below I consistently get the
>log message:
>
>Step : ERROR - not an error
>
>i.e. the 'sqlite3_step' loop returns only once indicating an error but
>the error message says 'not an error'. Can anyone spot my silly error?
>
>--------------------------------------------------------------------
>string sql = "SELECT name,value FROM setting;" ;
>sqlite3_stmt *pStmt ;
>
>LOGLN("SQL: "+sql) ;
>if( sqlite3_prepare( db, sql.c_str(), -1, &pStmt, 0 ) != SQLITE_OK )
>{
>LOGLN(string("Prepare: ERROR -")+sqlite3_errmsg(db)) ;
>}
>else
>{
>int rc ;
>while( rc=sqlite3_step(pStmt) != SQLITE_DONE )
>{
>if( rc == SQLITE_ROW )
>{
>string name = (const char*)(sqlite3_column_text(pStmt,0)) ;
>string value = (const char*)(sqlite3_column_text(pStmt,1)) ;
>settings[name] = value.c_str() ? value : "" ;
>LOGLN(name+"="+value) ;
>}
>else if( rc == SQLITE_BUSY )
>{
>LOGLN("Step: BUSY") ;
>continue ;
>}
>else if( rc == SQLITE_ERROR )
>{
>LOGLN(string("Step : ERROR - ")+sqlite3_errmsg(db)) ;
>break ;
>}
>else if( rc == SQLITE_MISUSE )
>{
>LOGLN("Step: MISUSE") ;
>break ;
>}
>}
>}
>sqlite3_finalize(pStmt) ;
>
>
>
>
>---------------------------------
> Yahoo! Music Unlimited - Access over 1 million songs. Try it free.
>  
>
Spot on Dan! You spotted by unintentional stoopid mistake.  Thanks