Created
April 26, 2021 12:29
-
-
Save little-brother/4cc7ac1d4c4ad8a15db07712a7564ded to your computer and use it in GitHub Desktop.
SQLite scalar function with context example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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