It was to be expected that I wouldnt be able to sit and learn iPhone development without interruption, so I’ve had to scale it back for a while whilst fixing bugs and enjoying Christmas
To consolidate my learning I have started making an application for myself. I try to be organised with my finances and keep a fairly keen eye on them but when budgeting I find it hard to put a figure on what I spend on food per month.
Keeping a pen and paper to write down every time I bought something from Tescos was a fruitless endeavour so why not make an iPhone application? I always thought it would be a nice simple project to start with, basic user interaction and some data persistence.
So using what I had learnt in the first few days I created a basic user interface and put in the logic to count and it worked nicely. Also what was quite pleasing was that it was pretty easy, I had a non persistent version working in a few hours.
Data persistence
For this first version I only need to save the amount spent, which you would normally just save in a text file and this is possible on the iPhone. However most of the applications I will be writing will require a structured data storage using SQLite so I decided to skip a few chapters in the book and learn how to do that.
I am aware that the approach I have taken is quite hacky, the database table is created in code (bleurgh!) and the querying code is in the controller, rather than abstracted away elsewhere. But in principle it works and gives a grounding on how to do it.
Adding references
First of all you need to create a reference to the SQLite3 library in the project. Right click on the frameworks folder and select Add to project from the Project menu. Navigate to Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/IphoneSimulatorX.Y.sdk/usr/lib and add the file libsqlite3.dylib.
In the header file that you want to work with SQLite add #import "/usr/include/sqlite3.h" and add a pointer to the SQLite database sqlite3 *database;.
You will also need to define some kind of filepath for the database file. Use the NSSearchPathForDirectoriesInDomains to search for the directories in the iPhone’s directory and get the documents directory by taking the first element in the array returned. Then define a file name for it i.e “mydb.sqlite3″.
Creating the database
The method sqlite3_open takes a filepath to a database and the reference to a database pointer. If the database hasn’t been created, it will create one in the filepath defined, otherwise it just opens.
In Countify I then wrote an SQL statement into an NSString which created the table I wanted IF NOT EXISTS. To execute the code you use the method sqlite3_exec. It is worth noting that SQLite works with C strings, so you have to do [mystring UTF8String] in order for your strings to work.
Running queries on the database
The pattern for querying the database is to prepare a statement, binding different variables as you see fit, performing a step to execute the statement and then using the sqlite methods to read the current data shown.
So first you create a sqlite3_stmt pointer along with an NSString of your query. On to your query you can add ? characters which you can bind different values to, for example:
SELECT TOTALSPENT FROM SPENDING WHERE ID = ?"
Then after you have used sqlite3_prepare_v2 to prepare your sqlite3_stmt you can use the statement multiple times and simply bind different values to them. So imagine were using the above SQL statement to read through multiple spending records:
for(i blah blah whatever i want to iterate through){ if(sqlite3_prepare_v2(database, [nsstringofquery UTF8String], -1, &pointerto_stmt, nil)==SQLITE_OK){ sqlite_3_bind_int(pointerto_stmt, 1, i); //1 refers to the ? index, not zero based! sqlite3_step(pointerto_stmt); totalSpent= totalSpent+sqlite3_column_double(pointerto_stmt, 0); //0 refers to the column index of the row in the database } }
I followed a similar pattern with updating the database.
So this is certainly a lot more long winded than using LINQtoSQL to say the least. But at the same time it is fairly simple and I dont think there will be any need for an elaborate database schema with any projects I do, so the methods I have described should be fine.
Done! For now.
I am quite pleased at how simple (relatively) this was. Admittedly it is a very basic application but it does work well. I am not going to make any additions to it for now, I have to learn other things which are less applicable for what I want to do and more about what I need to write for work. I have to make a video quiz application in under 6 weeks so I really have to work hard to achieve that.
I feel like I’m still not entirely sure how about how an iPhone application should be written from an architectural standpoint. Hopefully as I get more experience I will understand how to structure them so they are a bit more maintainable.
Next blog post will be about writing multiple view applications.






