driven, enthusiastic and a little bit geeky.

SQLite Wrapper C++ This project is an excerpt of the Shoutz0r code base that provides a small but useful abstraction in C++ for the SQLite library allowing easy retrieval to structures of queries.

SQLite Wrapper C++

The Shoutz0r mediaserver has sprouted a number of simple libraries that were necessary to properly construct the server. One of these libraries is a wrapper for the C SQLite library.

It gives the user a simple thread-safe interface to using queries and resultsets in a more object-oriented environment. It's usage is very simple, merely looking at the header files will give you an impression of how it should work.

This is an example of a simple query from the Shoutz0r codebase. It creates an ObjectResult query with the File structure as it's template class, using the FileFactory method (which is defined elsewhere), the contents of File objects is initialized and inserted into the ObjectResult.

From thereon out, it's a matter of addressing the objects in the objectresult like an array of references to File structures.

struct File
{
	size_t fileId;
	size_t directoryId;
	std::string name;
	std::string fullName;
	std::string type;
};

/**
 *  Return a random file identifier
 */
int MediaLibrary::random()
{
	ObjectResult fResult(FileFactory);
	d_db.query(fResult, "SELECT * FROM File ORDER BY RANDOM() LIMIT 1");

	if (fResult.nRows() > 0)
	    return fResult[0].fileId;
	else
	    return -1;
}


/**
 *  File factory function
 */
void FileFactory(File &file, string const &key, string const &value)
{
	stringstream str(value);

	if (key == "fileId")
	    str >> file.fileId;

	if (key == "directoryId")
	    str >> file.directoryId;

	if (key == "name")
	    file.name = string(value);  

	if (key == "fullName")
	    file.fullName = string(value);

	if (key == "type")
	    file.type = string(value);
}

The release is somewhat sloppy, a rudimentary Makefile is supplied which will make everything compile and generates a library. For examples of how to use it, check the Shoutzor project, in particular the files in the db/ folder.

Snapshot build from 26-04-2010

Happy coding.