Skip to content

Instantly share code, notes, and snippets.

@little-brother
Created April 26, 2021 12:29
Show Gist options
  • Select an option

  • Save little-brother/4cc7ac1d4c4ad8a15db07712a7564ded to your computer and use it in GitHub Desktop.

Select an option

Save little-brother/4cc7ac1d4c4ad8a15db07712a7564ded to your computer and use it in GitHub Desktop.
SQLite scalar function with context example
void odd (sqlite3_context *ctx, int argc, sqlite3_value **argv){
int *pCounter = (int*)sqlite3_get_auxdata(ctx, 0);
if (pCounter == 0) {
pCounter = sqlite3_malloc(sizeof(*pCounter));
if (pCounter == 0)
return sqlite3_result_error_nomem(ctx);
*pCounter = sqlite3_value_type(argv[0]) == SQLITE_NULL ? 0 : sqlite3_value_int(argv[0]);
sqlite3_set_auxdata(ctx, 0, pCounter, sqlite3_free);
} else {
++*pCounter;
}
sqlite3_result_int(ctx, *pCounter % 2);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment