Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Iterate a select statement in SQLite

Tags:

c++

sqlite

I have prepared a select statement.

sqlite3_prepare(db, "SELECT symbol,image FROM test WHERE template=? AND field=?", -1, &stmt, 0);
sqlite3_bind_text16(stmt, 1, pParams->pszFile, -1, SQLITE_STATIC);
sqlite3_bind_text16(stmt, 2, pParams->pszFieldId, -1, SQLITE_STATIC);

How to go about iterating through the results?

like image 392
sashoalm Avatar asked Mar 25 '23 07:03

sashoalm


1 Answers

int rc = sqlite3_prepare(db, "SELECT symbol,image FROM test WHERE template=? AND field=?", -1, &stmt, 0);
sqlite3_bind_text16(stmt, 1, pParams->pszFile, -1, SQLITE_STATIC);
sqlite3_bind_text16(stmt, 2, pParams->pszFieldId, -1, SQLITE_STATIC);  

 if(rc != SQLITE_OK)
 {
    fprintf(stderr, "SQL error: %s\n", sqlite3_errmsg(db));
    return;
  }
  rc = sqlite3_step(stmt);
  int ncols = sqlite3_column_count(stmt);

  while(rc == SQLITE_ROW)
  {

      for(i=0; i < ncols; i++)
      {
         fprintf(stderr, "'%s' ", sqlite3_column_text(stmt, i));
      }

      fprintf(stderr, "\n");
      rc = sqlite3_step(stmt);
  }

  sqlite3_finalize(stmt);
like image 193
Vijendra Singh Avatar answered Apr 20 '23 19:04

Vijendra Singh