/* Collection of code snippets by Arne Vajhøj */
/* (from articles on eksperten.dk / vajhoej.dk written sometime between 2004 and now) */
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#include "sqlite3.h"

int main()
{
    sqlite3 *con;
    sqlite3_stmt *ins, *sel;
    char *insstr = "INSERT INTO msgs (msg) VALUES(?)";
    char *selstr = "SELECT msg FROM msgs";
    char *msg = "Hi from C!";
    int stat;
    /* open database */
    stat = sqlite3_open("msgs.db", &con);
    if(stat) printf("Error in open: %s\n",  sqlite3_errmsg(con));
    /* write message to database */
    stat = sqlite3_prepare(con, insstr, strlen(insstr), &ins, NULL);
    if(stat) printf("Error in prepare: %s\n",  sqlite3_errmsg(con));
    sqlite3_bind_text(ins, 1, msg, strlen(msg), NULL);
    stat = sqlite3_step(ins);
    sqlite3_finalize(ins);
    /* dump all messages in database */
    stat = sqlite3_prepare(con, selstr, strlen(selstr), &sel, NULL);
    if(stat) printf("Error in prepare: %s\n",  sqlite3_errmsg(con));
    while(sqlite3_step(sel) == SQLITE_ROW)
    {
        printf("|%s|\n", sqlite3_column_text(sel, 0));
    }
    sqlite3_finalize(sel);
    /* close database */
    sqlite3_close(con);
    return 0;
}
