Fun with SQLite - Embedded never so easy
Over the weekend, I had to help a friend out with a little assignment (again :-)), to write an address book in C. Anyways, I opted to use SQLite, my reasons being (in no particular order):
- I’d heard how simple it was
- I’d never tried it and was itching
- I’d written a production app with mysql embedded C library about 2 yrs ago, and was curious to see how SQLite felt like
- I just needed an excuse to try SQLite out, and the C API at that
My prognosis in a nutshell: AWESOME!!!
I basically was writing C SQLite code 5 minutes after reading their “Getting started in 5 minutes guide”, and though I was on the irc channel (#sqlite on freenode), I didn’t ask any questions… it was all clear and straight forward – except that part when my AUTOINCREMENT didn’t work on a field declared as *int*, till I realized it was supposed to be *integer* - meehhhh!!!
Anyways, the tipping point for me was how it deals with rows returned in a query.
Typedef for a function pointer sqlite3_callback, that looks like:
int func(void * data, int numcols, char** colvals, char** colnames);
Basically, if you pass this function pointer to sqlite3_exec when executing a query that returns rows, this call back will be called for each row returned! This is very cool really. It’s a massive relief from the pointer gymnastics I had to do with the mysql4 C api, to get at my returned rows back then.
Anyways, I don’t know if this exists yet, but with all the data structure work I’ve been doing recently, I have been thinking of a patch to SQLite that would instead take a call back that looks like this:
int func(void * data, int numrows, sqlite_list);
the sqlite_list, would actually be a linked list of hash tables. Each node in the list will be a returned row, and each node, would actually be a hashtable of the columns. This would be even EASIER to program against.
Currently, I’m not even going to deceive myself and say I’m going to write that patch… I’m not even thinking about it in any of my main processing threads… instead its been swapped out into some fscked up area of my mind… where it will stay until such a time as I have a *voila* experience with the SQLite source code (which I intend to read anyways… but that intention is stored in that same fscked up area of my mind… so you get the real picture of things)
Anyways… I did finish the address book rather nicely, and it was fun to call over one of my C hating colleague to see how it worked. Ope just laffed me to smitherins :-)
The fun part of the project was modeling the entities in the application. We have this Object Persistence Framework we use in our C# apps in the office, and I basically built something close to that in C, to do the project… and it again reaffirmed my belief that learning various programming paradigms leaves you a MUCH better programmer. Taking a look at the Address Book source code, looks SOOOOO much like OOP code written in any modern language… the only difference will be that no member access operator is being used. Infact, it looked like doing Python without the namespaces :)
Conclusion… SQLite is cool, so am I :-)