Hi, all.

How about this approach?
I just implemented a simple code(User Defined Function) that returns a
number as like as row number.

It was worked pretty good with my simple SQL test cases.

sqlite> insert into test values ('first record');
sqlite> insert into test values ('second record');
sqlite> insert into test values ('third record');
sqlite> select rownum(0), * from test;
1 | first record
2 | second record
3 | third record

Note. parameter value of 0 is not necessary, but it should be exist to work
properly to use aux data in UDF.

Below shows my code.
It is registered by calling sqlite3_create_function() after database is
opened.

typedef struct ROWNUM_t ROWNUM_t;
struct ROWNUM_t{
  int nNumber;
};

static void rownum_free(void *p){
  sqlite3_free(p);
}

static void rownum(
  sqlite3_context *context,
  int argc,
  sqlite3_value **argv
){
  ROWNUM_t* pAux;

  pAux = sqlite3_get_auxdata(context, 0);

  if(!pAux) {
    pAux = (ROWNUM_t*)sqlite3_malloc(sizeof(ROWNUM_t));
    if(pAux) {
      pAux->nNumber = 0;
      sqlite3_set_auxdata(context, 0, (void*)pAux, rownum_free);
    }
    else {
      sqlite3_result_error(context, "sqlite3_malloc failed", -1);
      return;
    }
  }
  pAux->nNumber++;

  sqlite3_result_int(context, pAux->nNumber);
}

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

Reply via email to