Skip to content

Instantly share code, notes, and snippets.

@little-brother
little-brother / main.c
Created April 26, 2021 12:38
Attach an user defined function to SQLite database example
// gcc main.c sqlite3.c -o main.exe
#include <stdio.h>
#include "sqlite3.h"
static void square (sqlite3_context *ctx, int argc, sqlite3_value **argv) {
int type = sqlite3_value_type(argv[0]);
double d = sqlite3_value_double(argv[0]);
switch (type) {
case SQLITE_NULL:
@little-brother
little-brother / odd.c
Created April 26, 2021 12:29
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 {
@little-brother
little-brother / square.c
Created April 26, 2021 12:18
SQLite scalar function example
static void square (sqlite3_context *ctx, int argc, sqlite3_value **argv) {
int type = sqlite3_value_type(argv[0]);
double d = sqlite3_value_double(argv[0]);
switch (type) {
case SQLITE_NULL:
sqlite3_result_null(ctx);
break;
case SQLITE_INTEGER: