SQLite Forum

In C API, do prepared statements of simple inserts execute repeatedly even if not reset?
Login
Thank you for the explanation.  I think I now understand why sqlite3_step() didn't fail for the insert statements without a manual reset.  However, now my question is why does the select statement fail without a manual reset?

In the code below, the sqlite3_step(db_base->insertLexi) works repeatedly without a manual reset.

The first time the sqlite3_step(db_base->getLexi) it is executed, both sqlite3_column_text(db_base->getLexi, 0) and sqlite3_column_text(db_base->getLexi,8) point to data. The second time it is executed, without a manual reset, sqlite3_step(db_base->getLexi) returns SQLITE_DONE and both the sqlite3_column_text(...) return NULL. But with a manual reset, both the sqlite3_column_text(...) again point to data.

It appears that the statement is not being reset automatically for the sqlite3_step(db_base->getLexi).  I'm not arguing that it is not being reset but only saying that it appears to behave that way in that without the manual reset, I can't get the statement to return data. All the query requests is a select of the row with the greatest rowid. 

Thank you.

void write_json_lexi( const char *c, const char *tab )
  { 
    int rc = 0;     
    if ( ( rc = sqlite3_step( db_base->insertLexi ) ) != SQLITE_DONE )
      {
        sprintf( response, "{\"tab\":\"%s\",\"c\":\"%c\",\"s\":1,\"msg\":\"Failed to db_base->insertLexi.\" }", tab, *c );
        send_response( response );
        sqlite3_reset( db_base->insertLexi );
        sqlite3_reset( db_base->getLexi );
        return;
      }

    sprintf( response, "{\"tab\":\"%s\",\"c\":\"%c\",\"s\":0, \"msg\":\"Successfully wrote to json_raw.\"}", tab, *c );
    send_response( response );

    char *data,
         *strongs_no;

    if ( ( ( rc = sqlite3_step( db_base->getLexi ) ) != SQLITE_ROW && rc != SQLITE_DONE ) || 
         ( strongs_no = sqlite3_column_text( db_base->getLexi, 0 ) ) == NULL ||
         ( data = sqlite3_column_text( db_base->getLexi, 8 ) ) == NULL )
      {
        sprintf( response, "{\"tab\":\"%s\",\"c\":\"%c\",\"s\":1,\"rc\":%d,\"msg\":\"Failed to db_base->getLexi.\" }", tab, *c, rc );
        send_response( response );
        sqlite3_reset( db_base->insertLexi );
        sqlite3_reset( db_base->getLexi );
        return;
      }

    int szBytes = sqlite3_column_bytes( db_base->getLexi, 8 );
    sprintf( response, "{\"tab\":\"%s\",\"c\":\"%c\",\"s\":0,\"strongs_no\":\"%s\",\"bytes\":%ld,\"data\":\"%s\" }", tab, *c, strongs_no, szBytes, data );
    send_response( response );
    
    sqlite3_reset( db_base->insertLexi );
    sqlite3_reset( db_base->getLexi );

    return;

  } // close write_json_lexi