Skip to content

Instantly share code, notes, and snippets.

@little-brother
little-brother / undark
Last active September 6, 2021 18:17
Undark (fork) - SQLite deleted and corrupted data recovery tool
// Forked from http://pldaniels.com/undark/
// Build: gcc undark.c -o undark.exe -lws2_32 -s
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <ctype.h>
@little-brother
little-brother / Generate Excel file
Last active August 17, 2021 08:56
How to create one-sheet xlsx file without any code dependencies by template
// https://habr.com/ru/post/572684/
// template.xslx - https://github.com/little-brother/sqlite-gui/blob/master/resources/template.xlsx
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
const unsigned int crc32_tab[] = {
0x00000000, 0x77073096, 0xee0e612c, 0x990951ba, 0x076dc419, 0x706af48f,
0xe963a535, 0x9e6495a3, 0x0edb8832, 0x79dcb8a4, 0xe0d5e91e, 0x97d2d988,
@little-brother
little-brother / main.c
Last active August 8, 2021 17:45
SQLite query watcher
/*
Build
gcc.exe -Wall -O2 -c main.c -o obj\main.o
gcc.exe -Wall -O2 -c sqlite3.c -o obj\sqlite3.o
gcc.exe -o watcher.exe obj\main.o obj\sqlite3.o -s
Usage example
watcher -d "D:/my.sqlite" -q "select * from t order by 1 desc limit 10" - t 2
*/
#include <stdbool.h>
@little-brother
little-brother / vtab.c
Last active May 7, 2021 16:09
SQLite non-table-valued virtual table example
// gcc -shared vtab.c -o vtab.dll -s -static
// create virtual table t using vtab(4); -- 4 is a column count
#include "sqlite3ext.h"
SQLITE_EXTENSION_INIT1
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
typedef struct vtab_vtab vtab_vtab;
@little-brother
little-brother / vtab.c
Last active July 13, 2021 09:39
SQLite "read file" virtual table example
// gcc -shared vtab.c -o vtab.dll -s -static
// select * from vtab('D:/1.txt')
// select line from vtab where path = 'D:/1.txt'
#include "sqlite3ext.h"
SQLITE_EXTENSION_INIT1
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#define MAX_LINE_SIZE 1024
@little-brother
little-brother / vtab.c
Last active May 7, 2021 15:34
SQLite minimal virtual table example
// gcc -shared vtab.c -o vtab.dll -s -static
#include "sqlite3ext.h"
SQLITE_EXTENSION_INIT1
#include <string.h>
typedef struct vtab_vtab vtab_vtab;
struct vtab_vtab {
sqlite3_vtab base;
};
@little-brother
little-brother / aggavg.c
Last active April 26, 2021 13:13
SQLite aggregate function with complex state example
// gcc -shared aggavg.c -o aggavg.dll -s -static
#include "sqlite3ext.h"
SQLITE_EXTENSION_INIT1
typedef struct agg_state {
int cnt;
double sum;
} agg_state;
static void avg_step (sqlite3_context *ctx, int argc, sqlite3_value **argv) {
@little-brother
little-brother / aggsum.c
Last active April 26, 2021 13:13
SQLite aggregate function example
// gcc -shared aggsum.c -o aggsum.dll -s -static
#include "sqlite3ext.h"
SQLITE_EXTENSION_INIT1
static void sum_step (sqlite3_context *ctx, int argc, sqlite3_value **argv) {
double* total = (double*)sqlite3_aggregate_context(ctx, sizeof(double));
if (total == NULL)
return sqlite3_result_error_nomem(ctx);
*total += sqlite3_value_double(argv[0]);
@little-brother
little-brother / main.c
Created April 26, 2021 12:42
SQLite scalar functions extension example
// gcc main.c sqlite3.c -o main.exe
#include <stdio.h>
#include "sqlite3.h"
int main() {
sqlite3* db;
if (SQLITE_OK != sqlite3_open_v2(":memory:", &db, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE | SQLITE_OPEN_URI, NULL)) {
printf("Error: can't open a database");
return -1;
}
@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: