6. Raw SQLite query
The function sqlar:sql
performs raw SQL commands on an
input file.
6.1. Raw SQLite in C
<<static_sqlar_function_declarations>>=
static int sqlar_sql(char *db, char *sql);
<<sqlar_functions>>=
static int callback(void *notused,
int argc,
char **argv,
char **azcolname)
{
int i;
for(i = 0; i < argc; i++) {
printf("%s = %s\n",
azcolname[i],
argv[i] ? argv[i] : "NULL");
}
printf("\n");
return 0;
}
static int sqlar_sql(char *filename, char *sql)
{
int rc;
sqlite3 *db;
char *errmsg;
errmsg = NULL;
rc = sqlite3_open(filename, &db);
if(rc) {
fprintf(stderr,
"Can't open database: %s\n",
sqlite3_errmsg(db));
sqlite3_close(db);
return 1;
}
rc = sqlite3_exec(db, sql, callback, 0, &errmsg);
if(rc != SQLITE_OK) {
fprintf(stderr, "SQLite error: %s\n", errmsg);
sqlite3_free(errmsg);
}
sqlite3_close(db);
return 0;
}
6.2. Raw SQLite in Scheme
Raw sqlite commands can be executed in Scheme using the function sqlar:sql
.
<<sqlar_scheme_entries>>=
{"sqlar:sql", pp_sql, 2, 2, {STR,STR,___}},
<<sqlar_scheme_functions>>=
static cell pp_sql(cell x)
{
char *db;
char *sql;
int rc;
cell cstr;
cstr = car(x);
db = string(car(x));
x = cdr(x);
sql = string(car(x));
rc = sqlar_sql(db, sql);
if(rc != 0) {
error("SQLite error", cstr);
}
return UNSPECIFIC;
}
prev | home | next