

void db_insert_into_db(char const *timestr char const *typestr, int ctrstr) {


	std::string filename = "local/errorlog.txt";
	std::ofstream outfile;
	outfile.open(filename, std::ios_base::app);

	int rc;
	char *exec_errmsg;

	const char dbname[] = "local/log.db";
	
	sqlite3 *db = NULL;
		
	rc = sqlite3_open(dbname, &db);
	if(SQLITE_OK != rc) {
		outfile << "Can't open database "<< dbname << " (" << rc << "): " << sqlite3_errmsg(db) << std::endl;
		sqlite3_close(db);
		exit(1);
	}

	const char insert_sql[] = "INSERT INTO log (thedate, thetype, thecounter) VALUES (?,?,?)";
	sqlite3_stmt *insert_stmt = NULL;


	rc = sqlite3_exec(db, "BEGIN IMMEDIATE", 0, 0, 0);
	while ( rc != SQLITE_OK) {
		sqlite3_busy_timeout(db, 230);
		outfile << "Zzzzz in while" << std::endl;
		break;
	} //while

	// prepare_v2
	rc = sqlite3_prepare_v2(db, insert_sql, -1, &insert_stmt, NULL);
	if(SQLITE_OK != rc) {
		outfile << "A Can't prepare insert statment " << insert_sql << " (" << rc << "): " << sqlite3_errmsg(db) << std::endl;
		sqlite3_close(db);
	}

	rc = sqlite3_bind_text(insert_stmt, 1, timestr, strlen(timestr), NULL);
	if(SQLITE_OK != rc) {
		outfile << "B Error binding value in insert (" << rc << "): " << sqlite3_errmsg(db) << std::endl;
		sqlite3_close(db);
	} 
	
	rc = sqlite3_bind_text(insert_stmt, 2, typestr, strlen(typestr), NULL);
	if(SQLITE_OK != rc) {
		outfile << "C Error binding value in insert (" << rc << "): " << sqlite3_errmsg(db) << std::endl;
		sqlite3_close(db);
	} 
	
	rc = sqlite3_bind_int(insert_stmt, 3, ctrstr);	
	if(SQLITE_OK != rc) {
		outfile << "D Error binding value in insert (" << rc << "): " << sqlite3_errmsg(db) << std::endl;
		sqlite3_close(db);
	} 

	// step
	rc = sqlite3_step(insert_stmt);
	if(SQLITE_DONE != rc) {
		outfile << "H insert statement didn't return DONE (" << rc << "): " << sqlite3_errmsg(db) << std::endl;
	} 

	rc = sqlite3_finalize(insert_stmt);	
	if(SQLITE_OK != rc) {
		outfile << "I Error finalize (" << rc << "): " << sqlite3_errmsg(db) << std::endl;
		sqlite3_close(db);
	} 
	
	rc = sqlite3_close(db);
	if(SQLITE_OK != rc) {
		outfile << "J Error close db (" << rc << "): " << sqlite3_errmsg(db) << std::endl;
		sqlite3_close(db);
	} 

	// commit all to sqlite
	sqlite3_exec(db, "COMMIT", 0, 0, 0);

	outfile.close();

};
