/* Collection of code snippets by Arne Vajhøj */
/* (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) */
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#include "sqlite3.h"

static char *DB = "test.db";
static char *INS = "INSERT INTO multi(msg) VALUES(?)";
static char *SEL = "SELECT id,msg FROM multi";
static char *TXT = "Hi from C on VMS";

int main()
{
    sqlite3 *con;
    sqlite3_stmt *ins, *sel;
    int stat;
    stat = sqlite3_open(DB, &con);
    if(stat)
    {
        printf("Error in open: %s\n",  sqlite3_errmsg(con));
        exit(1);
    }
    stat = sqlite3_prepare(con, INS, strlen(INS), &ins, NULL);
    if(stat)
    {
        printf("Error in prepare: %s\n",  sqlite3_errmsg(con));
        exit(1);
    }
    stat = sqlite3_bind_text(ins, 1, TXT, strlen(TXT), NULL);
    if(stat)
    {
        printf("Error in bind: %s\n",  sqlite3_errmsg(con));
        exit(1);
    }
    stat = sqlite3_step(ins);
    if(stat != SQLITE_DONE)
    {
        printf("Error in step: %s\n",  sqlite3_errmsg(con));
        exit(1);
    }
    sqlite3_finalize(ins);
    stat = sqlite3_prepare(con, SEL, strlen(SEL), &sel, NULL);
    if(stat)
    {
        printf("Error in prepare: %s\n",  sqlite3_errmsg(con));
        exit(1);
    }
    while(sqlite3_step(sel) == SQLITE_ROW)
    {
        printf("%d %s\n", sqlite3_column_int(sel, 0), sqlite3_column_text(sel, 1));
    }
    sqlite3_finalize(sel);
    sqlite3_close(con);
    return 0;
}
