#include <iostream>
#include <sqlite3.h>
//sqlite3 needs to be compiled if you don't have ubuntu
//if you have ubuntu use
//sudo apt install libsqlite3-dev 
//INSTALLS sqlite3.h headers without having to compile below
//then run using 
// g++ hello-world-sqlite-3.cpp -l sqlite3 -o hello-world-sqlite3.out
// ./hello-world-sqlite3.out
// if windows see if there is a 32 bit .dll file for mingw
//to compile manually need to get source
//since programming in c, can use amalgamation source file sqlite3.c
//grab shell.c sqlite3.c and sqlite3.h from tarball
//gcc shell.c sqlite3.c -lpthread -ldl
//pthread library  multi thread
//dl dynamic loading
//full -lreadline -lncurses -o sqlite3
//o = output file name
//sudo apt install libreadline-dev libncurses-dev
//in windows g++ -shared sqlite3.c -o sqlite3.dll
using namespace std;
//for c_str()

//calls for each row , colnames = column name , column value
static int callback(void *errorData, int numColumns, char **colValues, char **colNames)
{
	int i;
	fprintf(stderr, "error %s: \n", (const char*) errorData);
	printf("num columns %d\n", numColumns);
	for (i = 0; i < numColumns; i++)
	{	
		printf("%d %s = %s\n",i, colNames[i], colValues[i] ? colValues[i] : "NULL");
	}
	printf("\n");
	return 0;
}

int main(int argc, char **argv)
{
	sqlite3 *db;
	std::string sqlStatement = "SELECT * FROM barcodes;";
	int exit = 0;
	//OEN UTF 8 read write
	//file:///N:/database/file.sqlite?mode=ro
	exit = sqlite3_open("barcodes.sqlite3", &db);
	if(exit != SQLITE_OK) 
	{
		std::cerr << "Error open db" << sqlite3_errmsg(db) << std::endl;
		return (-1);
	}
	else
	{
		std::cout << "SQL Database opened" << std::endl;
	}
	char * messageError;
	//exec call callback function multiple times for each row returned
	//https://www.sqlite.org//c3ref/exec.html
	//db open database, c string sql, callback function, argument, err message
	exit = sqlite3_exec(db, sqlStatement.c_str(), callback, 0 , &messageError);
	if (exit != SQLITE_OK)
	{
		std::cerr << "Select Error" << std::endl;
		sqlite3_free(messageError);
	}
	else
	{
		std::cout << "Success select" << std::endl;
	}

	sqlite3_close(db);
	return (0); 
}

