Start of a new thin C++ header only SQLite wrapper

This commit is contained in:
Sébastien Rombauts 2012-03-30 22:31:22 +02:00
commit 733c92e753
4 changed files with 113 additions and 0 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
Debug
Release

62
Makefile Normal file
View File

@ -0,0 +1,62 @@
### Options: ###
# C++ compiler
CXX = g++
# flags for C++
CXXFLAGS ?= -Wall
# [Debug,Release]
BUILD ?= Debug
### Conditionally set variables: ###
ifeq ($(BUILD),Debug)
BUILD_FLAGS = -g -rdynamic -fno-inline -O0 -DDEBUG -D_DEBUG
endif
ifeq ($(BUILD),Release)
BUILD_FLAGS = -O2
endif
ifeq ($(BUILD),Debug)
LINK_FLAGS = -g -rdynamic
endif
ifeq ($(BUILD),Release)
LINK_FLAGS =
endif
### Variables: ###
CPPDEPS = -MT $@ -MF`echo $@ | sed -e 's,\.o$$,.d,'` -MD -MP
SQLITE_CXXFLAGS = $(BUILD_FLAGS) $(CXXFLAGS)
SQLITE_EXAMPLE1_OBJECTS = \
$(BUILD)/main.o
### Targets: ###
all: $(BUILD) $(BUILD)/example1
clean:
rm -f $(BUILD)/*.o
rm -f $(BUILD)/*.d
rm -f $(BUILD)/example1
$(BUILD): $(BUILD)/
mkdir -p $(BUILD)
$(BUILD)/example1: $(SQLITE_EXAMPLE1_OBJECTS)
$(CXX) -o $@ $(SQLITE_EXAMPLE1_OBJECTS) $(LINK_FLAGS) -lsqlite3
$(BUILD)/main.o: src/main.cpp
$(CXX) -c -o $@ $(SQLITE_CXXFLAGS) $(CPPDEPS) $<
.PHONY: all clean
# Dependencies tracking:
-include $(BUILD)/*.d

19
src/main.cpp Normal file
View File

@ -0,0 +1,19 @@
#include <iostream>
#include "sqlite.hpp"
int main (void)
{
std::cout << "Hello SQLite.hpp\n";
try
{
SQLite::Database db("sqlite.db3");
}
catch (std::exception& e)
{
std::cout << e.what() << std::endl;
}
std::cout << "Bye SQLite.hpp\n";
return 0;
}

30
src/sqlite.hpp Normal file
View File

@ -0,0 +1,30 @@
#include <sqlite3.h>
#include <stdexcept>
namespace SQLite
{
class Database
{
public:
explicit Database(const char* apFilenameUt8, const bool abReadOnly = false) :
mpSQLite(NULL)
{
int err = sqlite3_open_v2(apFilenameUt8, &mpSQLite, abReadOnly?SQLITE_OPEN_READONLY:SQLITE_OPEN_READWRITE, NULL);
if (SQLITE_OK != err)
{
std::string strerr = sqlite3_errmsg(mpSQLite);
sqlite3_close(mpSQLite);
throw std::runtime_error(strerr);
}
}
virtual ~Database(void)
{
sqlite3_close(mpSQLite);
}
private:
sqlite3* mpSQLite;
};
};