I am trying to familiarize myself with the lower-level routines for
queries, so started with the
example here:

http://www.sqlite.org/quickstart.html

...which uses sqlite3_exec() and I replaced that with
prepare/bind/step.  When there are no
bind parameters in the sql statement, the revised program works - but
even putting just one
place-holder ('?') and trying to bind at position 1, I get a
SQLITE_RANGE error.  I have
no clue what I'm doing wrong, if any one can help.   I am taking the
liberty of including
the program in-line since it's less then 100 lines, if that's ok.

Invocation:

working:
./client test.sqlite "select * from emp where ename = 'fred'"

not working - error 25, range erorr:

./client test.sqlite "select * from emp where ename = '?'" fred

Thanks,


   -Chris


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sqlite3.h>

void
print_row(const char *aColData[], const int nNumCols) {
    int i;
    for(i=0; i<nNumCols; i++)
        (void)printf(i<nNumCols-1?"%s,":"%s",
aColData[i]?aColData[i]:"null");
    printf("\n");
}

int
main(int argc, char **argv){
  sqlite3 *db;
  sqlite3_stmt *pStmt;
  char *zErrMsg = 0;
  const char *pzTail = 0;
  int rc;
  int nRows = 0;

  if( argc<3 ){
    fprintf(stderr, "Usage: %s <db> <sql> [bind-param1,
bind-param2...]\n", argv[0]);
    exit(1);
  }

 if((rc = sqlite3_open_v2(argv[1], &db, SQLITE_OPEN_READONLY, 0)) !=
SQLITE_OK) {
    fprintf(stderr, "%d: Can't open database: %s\n", sqlite3_errmsg(db),
__LINE__);
    sqlite3_close(db);
    exit(1);
  }

      printf("%s\n", argv[2]);
  if((rc = sqlite3_prepare_v2(db, argv[2], -1, &pStmt, &pzTail)) !=
SQLITE_OK) {
    fprintf(stderr, "%d: SQL error: %d - %s\n", rc, sqlite3_errmsg(db),
__LINE__);
    exit(1);
  }
 
  if (argc > 3) {
    int j;
    for(j=0; j<argc; j++) {
      printf("%d: %s\n", j+1, argv[3+j]);
      if((rc = sqlite3_bind_text(pStmt, j+1, argv[3+j], -1,
SQLITE_TRANSIENT))
        != SQLITE_OK) {
        fprintf(stderr, "%d: SQL error: %d - %s\n", __LINE__, rc,
sqlite3_errmsg(db));
        exit(1);
      }
    }
  }

  int nCols = sqlite3_column_count(pStmt);
  const char *aColData[nCols];
  int i;
  for (i=0; i<nCols; i++)
    aColData[i] = strdup((char *)sqlite3_column_name(pStmt, i));
  print_row(aColData, nCols);
  for (i=0; i<nCols; i++)
    free((void *)aColData[i]);

  while(rc != SQLITE_DONE) {
    rc = sqlite3_step(pStmt);

    switch(rc) {
      case SQLITE_ROW:
        nRows++;
        for (i=0; i<nCols; i++)
          aColData[i] = (char *)sqlite3_column_text(pStmt, i);
        print_row(aColData, nCols);
        break;

      case SQLITE_DONE:
        printf("\n%d rows processed.\n", nRows);
        break;
     
      default:
        fprintf(stderr, "%d: SQL error: %d - %s\n",__LINE__, rc,
sqlite3_errmsg(db), __LINE__);
        exit(1);
        /*NOTREACHED*/
        break;
    }
  }

  sqlite3_close(db);
  return 0;
}

_______________________________________________
sqlite-users mailing list
sqlite-users@sqlite.org
http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users

Reply via email to