Answer the question
In order to leave comments, you need to log in
How to use one self-written function several times in a request?
Here is the module code for sqlite3:
#include "sqlite3ext.h"
#include <stdlib.h>
#include <stdio.h>
SQLITE_EXTENSION_INIT1
static void moneyFunc(
sqlite3_context *context,
int argc,
sqlite3_value **argv
){
char *result;
int sum = sqlite3_value_int(argv[0]);
int full = sum / 100;
int dec = sum % 100;
if (dec == 0){
snprintf(result, 8, "%d,00", full);
} else if (dec > 0 && dec < 10){
snprintf(result, 8, "%d,0%d", full, dec);
} else if (dec >= 10 && dec <= 99){
snprintf(result, 8, "%d,%d", full, dec);
} else return;
sqlite3_result_text(context, result, 8, 0);
}
int sqlite3_extension_init(
sqlite3 *db,
char **pzErrMsg,
const sqlite3_api_routines *pApi
){
SQLITE_EXTENSION_INIT2(pApi)
sqlite3_create_function(db, "money", 1, SQLITE_ANY, 0, moneyFunc, 0, 0);
return 0;
}
SELECT money(100)
SELECT money(100), money(350)
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question