Thursday, May 14, 2009

Zoltar Available on iTunes



Actually it's been available for about a week now but Apple didn't list it. I've run into a big problem with the iTunes store that a lot of other developers have been having. The result is that I've only had a few downloads, and Zoltar is much higher quality than most apps I see out there.

I hate to ask, but for anyone who follows this blog and has an iPhone, please go to the iTunes store and give Zoltar a great review. I'll buy you a beer the next time I see you!

SQLite C++ wrapper, so you don't have to

SQLite is great. It is small, fast and easy to set up. However, if you are writing software that needs it, you're in for a bit of learning. You will need to learn what API calls to make and when to make them, usually by going over example code and using trial and error. I am introducing an open source SQLite C++ wrapper that will allow you to use the database without having to learn the SQLite API. It may save you several days of work.

The wrapper is just two C++ files, has a MIT style license and is platform independent. You will still need to link in SQLite 3.0 and learn the quirky SQLite SQL syntax.

You can download it here.

Creating a database and table
#include "sdsqlite.h"

void create_db(void)
{
sd::sqlite database("mydb.db");
database << "create table if not exists work (first_name text, last_name text, hours real)";
}


Database insertion
#include "sdsqlite.h"

struct work_data { char* first; char* last; float hours; };

work_data wdata[] = {
{"Joe", "Smith", 2.5},
...
};

void insert_rows(void)
{
try
{
sd::sqlite database("mydb.db"); // open the db with the table already created

sd::sql insert_query(database); // build an sql query
insert_query << "insert into work (first_name, last_name, hours) VALUES(?, ?, ?)";

database << "begin transaction";// create a transaction for speed

// insert data (sdsqlite will auto-detect data type and execure query)
for(int i=0;i<sizeof(wdata)/sizeof(work_data);++i)
insert_query << wdata[i].first << wdata[i].last << wdata[i].hours;

database << "commit transaction";// complete transaction
}
catch(sd::db_error& err)
{
// do something with error
}
}


Database extraction
#include "sdsqlite.h"

void extract_name(const std::string& name)
{
try
{
sd::sqlite database("mydb.db"); // open the db with the table already created

// select all names that begin with the contents of the "name" variable
sd::sql selquery(database);
selquery << "select first_name, last_name, hours from work where first_name like ?" << name+"%";

// extract the matching rows
float hours;
std::string first, last;
while(selquery.step())
{
selquery >> first >> last >> hours;

// do something with the data
}
}
catch(sd::db_error& err)
{
// do something with error
}
}