iPhone Programming Tutorial - Creating a ToDo List Using SQLite Part 1
If you're new here, you may want to subscribe to my RSS feed. Thanks for visiting!
If you have been following my tutorials, you know that we have been working primarily with UITableViews. This is mostly because SO many applications can be developed using this simple control. This final UITableView tutorial will be taking all of the skills learned from previous tutorials, putting them all together, and adding SQLite to create a prioritized To-Do list. I will also be showing you how to add multiple columns to your table cells and we will be exploring some of the other controls that the iPhone has to offer. What good would the tutorials be if we didn’t use them to create something useful.
I will move a little faster in this tutorial while still explaining the new stuff in detail. I will assume that you have completed the fruits tutorial and it’s prerequisites.
This tutorial will be a multipart series as it will be a little longer than my previous ones. In this first tutorial, you will learn:
- Create a NavigationBased Application
- Create a Database
- Add the Database to Your Project
- Add the SQLite3 Framework
- Create a Todo Class Object
- Initialize the Database
So let’s get started…
Open up X-Code and Select File->New Project… Select Navigation-Based Application and click Choose…
Name your project todo. Now let’s create the todo database that we will be using. Open up the Terminal application on your Mac. This is located in Applications > Utilities.
If you have installed XCode, you should have mysqlite3 already on your computer. To check this, type:
sqlite3 into the Terminal and sqlite3 should start. Type .quit to exit. If sqlite3 is not installed, install all of the XTools from your Mac Installation Disk.
Now that the terminal is open let’s create the database. This is done with the command:
sqlite3 todo.sqlite
SQLite3 will now start and load the todo.sqlite database. By default the database is empty and contains no tables. If you need a refresher on the basics of SQL databases Google It. Since our application is fairly simple, we only need to create one table. We will create a table called todo by typing the following statement:
CREATE TABLE todo(pk INTEGER PRIMARY KEY, text VARCHAR(25), priority INTEGER, complete BOOLEAN);
One thing to note here is the pk field. It is the primary key of the table. This adds functionality such that every time a row is added to the database, it auto-increments this field. This will be a unique identifier to identify each row. All of the other fields should be fairly self explanitory.
Now that our table has been created, let’s add some data. We will eventually be adding todo items within our app, but for now we will add some defaults. Type the following commands below.
INSERT INTO todo(text,priority,complete) VALUES('Take out the trash',3,0);
INSERT INTO todo(text,priority,complete) VALUES('Do Computer Science homework',1,0);
INSERT INTO todo(text,priority,complete) VALUES('Learn Objective C',1,0);
INSERT INTO todo(text,priority,complete) VALUES('DIGG this tutorial',2,0);
You can add as many todo items as you would like. For this tutorial, make sure you enter a priority between 1 and 3 (You’ll see why later). Now our database has been created and populated let’s exit out of SQLite3. Do this by typing .quit. Your terminal window should look something like this.
Now go back to XCode. Do a Control-Click (right click) on the folder named Resources. Click Add -> Existing Files… and browse to your todo.sqlite file and click Add. It will then prompt you with a screen like this.
Make sure you check the box that says Copy items into destination group’s folder (if needed). You should now see the todo.sqlite file inside of the resource folder.
Now that we have added the database, we need to load the Objective C libraries so we can use it. Do a control-click (right click) on the Frameworks folder. Click Add -> Existing Frameworks. Now this part is a little strange. It has been my experience that these libraries are not in the same place on all machines. So in the search bar type in libsqlite3. The file we are looking for is called libsqlite3.0.dylib. This may pull up multiple files as OSX has it’s own versions of this file. Just click on the largest of the files that show up and click Add. As you can see, mine is about 1.7 MB.
Now it should add the framework and your directory will look something like this:
We need to create an object to hold our todo information. We will eventually be making an array of these objects to populate a UITableView. Go ahead and click File -> New File… Select NSObject Subclass and click Next.
Name this object todo.m and check the box that says Also create “Todo.h” and click Finish.
Open up todo.h and add the following code.
We see some new things here…First, there is a variable of type sqlite3 called database. This will be a reference to the applications database and will allow the todo object to communicate with it. Make sure you add a #import<sqlite3.h> in your imports.
Next, we see a primary key. Notice that in the property declaration it has the keywords assign and readonly. This tells the compiler that this variable, once assiged, can not be changed again. This is good since each todo will be uniquely identified by this variable.
Also, I have declared a method called initWithPrimaryKey. This will be the contstructor for this object. It takes an integer to assign as the primary key and an sqlite3 object to use as the database reference.
Let’s implement this method…Open up todo.m and add the following code.
There are quite a few new things that need to be explained here. I will just go through it line by line.
static sqlite3_stmt *init_statement = nil
This will hold our initialize statement when retrieving todo data from the database. This statement is static, meaning it is independent of any instance. In other words, there will be only one of them no matter how many todo objects we create. This statement will get compiled and allow us to do some neat things. I’ll explain more in a bit.
The next lines makes sure that the super class (NSObject) initilizes properly before we initilize a todo object. We then set the local primary key and database objects to the parameters passed to the initWithPrimaryKey method.
Now some interesting stuff happens. The next lines checks if our init_statment is null. This will happen only once per launch of the application. If it is null, we create a new string containing an SQL statement. If you are familiar with SQL at all, this should look pretty familiar with one exception. What is a question mark doing in there? Well, I will tell you. After the SQL statement gets compiled, we can bind a value to it that will eventually replace the question mark. So this allows us to have 1 generic SQL statement, but bind different values to it to retrieve different results. So the next line, you guessed it, prepares the statement and stores it in our init_statement. The if statement just checks to see if this finished correctly and prints an error if there was a problem.
Moving on… The line sqlite3_bind_int simply replaces that question mark with the primary key of the current todo object, so what we end up with is statements like this:
SELECT text FROM todo WHERE pk = 1;
SELECT text FROM todo WHERE pk = 2;
SELECT text FROM todo WHERE pk = 3;
SELECT text FROM todo WHERE pk = n;
After that, the sqlite3_step(init_statement) method is called. This method executes the SQL statement on the database. It is contained inside of an if statement to make sure it executed properly. Now we can finally access the todo data. We see this line:
self.text = [NSString stringWithUTF8String:(char*) sqlite3_column_text(init_statement,0)];
Wow, that’s a mouthful… Let’s analyze it. The sqlite3_column_text method tells SQL that we want to retrieve a string object from the database. It has 2 parameters. The first, is just a reference to the SQL statement that was used. The second is the column number that we wish to get text from. So in this case, we only have one column (SELECT text FROM…) so there is only 1 index and that’s the 0th index. Next, the (char *) is just a cast to a string (might not be needed, but good practice). And finally, we build an NSString object with the data returned so that we can assign self.text to it.
This is quite a bit to explain in just text. If I have lost you, feel free to ask me questions in the comments.
We are done with the todo object for now…
Go ahead and open up todoAppDelegate.h and add the following code.
This should look a little familiar with the exception of a few lines. Notice that I have created an NSMutableArray of todo objects. This will be (like the fruit example) an array to hold our todo items. We will eventually use this array to populate a UITableView. The only new lines here are the import of sqlite3.h and the sqlite3 *database line. Now let’s open up todoAppDelegate.m and add some code.
One new thing we see here is a private interface. We declared it here because it’s specific to this object so it does not need to be declared in the .h file. The 2 functions we will implement are createEditableCopyOfDatabaseIfNeeded and initializeDatabase. Much of the code for these has already been written for us inside of Apple’s SQLBooks tutorial. I will going through this code and explaining it the best that I can. Add the following code.
What this method is essentially doing is copying the database from your project folder to the documents folder on your iPhone. This will only happen once as it first checks if the database already exists in the documents folder. I’m not going to go through this line by line as it is fairly self explanitory. Apple does a great job of naming functions and variables so that we can understand what is going on. If I get enough requests in the comments, I’ll do a line-by-line writup of this function.
The next function we will implement is initializeDatabase. Add the following code:
That’s a lot of text! Don’t worry it’s mostly comments. Let’s analyze this code…Some of it is very similar to the fruits example.
The first line creates and initializes a NSMutableArray. We then go on to set this array to our object’s todos array and release the temporary object.
The next 3 lines locate the database we created inside of the documents folder. Following that, the sqlite3_open line open’s the database so we can access its data. If the database opens correctly, we then proceed to retrieve todo items. The first line:
const char *sql = "SELECT pk FROM todo";
is an SQL statement that we will use to get all of the primary keys from the database. We then prepare the statement (as we did inside the todo.m file) only this time there is no “?” in the statement. That is because there is not condition for retrieving the primary keys. We are simply saying “give me all of the primary keys in the database”.
Now we see a while loop that is stepping through the SQL results. Every time we call sqlite3_step, the next result gets retrieved. The line:
int primaryKey = sqlite3_column_int(statement,0);
retrieves the primary key from each result. This is very similar to retrieving the text in the todo.m class only we use the sqlite3_column_int method instead of the sqlite3_column_text method. This is done for obvious reasons.
After we have the primary key, we create a new Todo object and call the initWithPrimaryKey constructor that we created. The primary key gets passed as well as a reference to the database. This allows the Todo object to essentially “look itself up” in the database. Finally, we add the newly created Todo object to our array of todos.
The last statement sqlite3_finalize clears the statement from memory and does some other cleanup.
The last part of this tutorial is calling these functions to create and initialize the database. So add the following code to applicationDidFinishLaunching:
We are simply calling these functions. You can now click Build and Go but your application won’t display anything! You might be quite frustrated that you completed this portion of the tutorial and have yet to see anything. Well, stay tuned! I will have the next portion of this tutorial up soon.
For you ambitious programmers you could move on. If you notice, at this point we are in a similar situation as the fruit tutorial. We have an Array of objects that will eventually populate a UITableView.
This tutorial will be a 4 part series and I will show you how to use a few more controls. We will be adding, editing, and deleting todo items. If you have any questions, please leave them in the comments. Also, if you get lost you can download the sample code here
Happy iCoding!
- Posted by Brandon on 19 Aug 2008 in SQLite, iPhone Programming Tutorials
- Digg |
- Del.icio.us |
- Stumble |
119 Responses
shardayyy Says:
August 19th, 2008 at 1:03 pm
Nice tutorial, Are you planning to do something on Tab’s along with this todo list.
Just like the IPod tabs (Songs | Playlist | Album | etc)
Where each tab uses its own NIB/XIB file.
Thanks.
Brandon Says:
August 19th, 2008 at 1:11 pm
Actually, I am considering it if there is enough interest.
I was thinking of having 3 tabs: all todos, completed todos, and incomplete todos.
Thanks for reading!
textual Says:
August 19th, 2008 at 3:34 pm
i was building this very project for my own ‘learn as you go’ project
very similar to this, it is based off of the sql lite tutorial on ADC
Phil Says:
August 19th, 2008 at 3:45 pm
great tutorial Brandon, i’ve created my database using a SQLite GUI application rather than through the terminal - i’ve ended up with a todo.rsd file rather than a todo.sql file will this still work ok?
I would also like to see how to work with the tabs,so would be great if you could include it in this tutorial set as an example.
One other point - could you include the source code for each part of the tutorial?
Thanks.
bobcubsfan Says:
August 19th, 2008 at 4:53 pm
Brandon, you have done it again. Thanks for “Part 1.” Although I am tempted to continue on before you post the remaining parts, I make so many typos, that I am willing to wait for you.
Thanks again for your great teaching.
Nebkiwi Says:
August 19th, 2008 at 6:14 pm
able to do a navigation based application with a different nib for each item? little stuck on that and that would be a great help, left a post on forum as well if you want to help me on there.
Darryl Says:
August 19th, 2008 at 8:44 pm
Great tutorials, definite book material.
The combination of type as I do, followed by explanations of what the code does suits the way I learn.
I am trying to create a hybrid of your helloworld (without navigation controller) and the fruit example.
Basically picking the “fruit” takes you to a view that has several textfields and a button instead of the single textview like in the original fruit example. The second view is the same for all “fruit”.
you enter some numbers, press the button, some calcs are done and the results are placed in some other textfields.
The problem I am having is filling out the input textfields. When I click on the textfields the keyboard comes up and I can enter the numbers but then the keyboard won’t go away. I have tried several combinations in interface builder of auto enter and showing the done key but nothing works. If I hard wire some numbers in the Viewdidload function, so I don’t have to invoke the keyboard, and press the button everything else works ok.
I am thinking that it has something to do with the fact that the transition is a navigation controller and I need to do something else to release the keyboard or let that controller know about the keyboard.
I have noticed in other examples that the navigation bar has separate edit or save buttons that you have to press before getting the keyboard and am wondering if I need to take another step.
If all of this makes any sense I would appreciate a nudge in the right direction. I have succeeded in writing this app as a dashcode webapp but I would like to implement it as a native iphone app.
Again, Thanx for the tutorials so far and keep em coming.
Ron Says:
August 19th, 2008 at 11:44 pm
Brandon: Thanks so much for the tutorials! They’ve been extremely helpful.
kubi Says:
August 20th, 2008 at 6:53 am
Thanks you ver much for this Tutorial! Came at the right time, as i was stuck on the SQLite-Books-Example. Really looking forward to the next parts!
Brandon Says:
August 20th, 2008 at 7:43 am
@Darryl
To hide the keyboard, put this code
[txtName resignFirstResponder];
in the method that gets called when your button is pressed. txtName of course will be the variable associated with your text box. That should hide the keyboard.
Thanks for posting!
ragingfire Says:
August 20th, 2008 at 11:07 am
great tutorials! And yes, I am very interested in the tabs that shardayyy mentioned. keep ‘em coming! i am learning a lot.
shardayyy Says:
August 20th, 2008 at 11:48 am
I came across this nice GUI for the SQLite Database that is an addon to firefox. Check out
Brandon Says:
August 20th, 2008 at 11:59 am
@shardayyy
that seems very helpful, thanks for the link.
Todd Says:
August 20th, 2008 at 1:27 pm
Hi Brandon, basic question for you. Why does the DB get copied to the documents folder? Can’t it be accessed or edited in it’s default location?
bobcubsfan Says:
August 20th, 2008 at 3:28 pm
Hi Brandon
What is the best way to use a variable instead of hard-coding text in a file?
For example, “todo.sqlite” is used in several places. Could we not use a constants.h file with often used text? That way if something changes, only the entry in the constants file would have to be changed.
bobcubsfan Says:
August 20th, 2008 at 4:36 pm
shardayyy
thanks for the firefox link. What a time saver!
bobcubsfan Says:
August 20th, 2008 at 4:58 pm
Question about sqlite manager. how does one create a field of a specific length? Example: State should be 2 VARCHAR.
Brandon Says:
August 20th, 2008 at 6:46 pm
@Todd,
Good question. I’m not 100% sure I was just following Apple’s conventions. But if I had to make an educated guess I would say it’s a permissions thing.
From my experience with iPhone hacking, directory security is strange for apps. The Documents folder should have the most open permissions.
Sorry I can’t give you a strait answer.
Brandon Says:
August 20th, 2008 at 7:06 pm
@bob,
Yea, you could assign a variable to the db name however I don’t foresee changing the db name and if so, there is always find and replace :). But yea you could do a static constants object and called like:
Constants.DATABASE_NAME
I think this would only be practical when your app has a lot of global constants. Usually a game would warrant this type of design. Otherwise, you shouldn’t allocate another object for 1 or 2 constants.
Probably the best practice for this situation is declaring an NSString property for the database name inside of todoAppDelegate.h and initialize it in the applicationDidFinishLauching method before the db methods get called.
Darryl Says:
August 20th, 2008 at 7:08 pm
Just wanted to say thanks on the resignFirst Responder tip.
I tried it and it would work once and then any subsequent attempts would crash the program. I thought it was the first responder thing and went and looked at all the class info for textfield and UIResponders and couldn’t figure it out and then after looking closer at my own code I noticed a mistake in allocating and releasing an NSString.
After fixing that the resignFirstResponder works great.
Also in response to Todd’s question I thought I’d look in the comments found in the SQLiteBooks example and found this
// The application ships with a default database in its bundle. If anything in the application
// bundle is altered, the code sign will fail. We want the database to be editable by users,
// so we need to create a copy of it in the application’s Documents directory.
Evidently the iphone does some kind of a hash on the app bundle and if you change the original sql file it changes that code and the iphone refuses to run the app anymore.
Brandon Says:
August 20th, 2008 at 7:10 pm
@Darryl
Glad it worked for you and great find on the db thing! That really clears that up.
kubi Says:
August 21st, 2008 at 12:14 am
@Todd, @Brandon
Looking at Apples SQLiteBooks Example the code signing might be the reason why the database is copied. Look at the comment in “applicationDidFinishLaunching” in “AppDelegate.m” in the SQLiteBooks Example.
“[...]If anything in the application bundes is altered, the code sign will fail. We want the database to be editable by users, so we need to create a copy [...]“
rramnel Says:
August 24th, 2008 at 7:32 am
Hi Brandon, really useful stuff. Learning a lot from your tutorials. Thanks a lot…
FYI below is a website where you can create/browse sqlite databases using a GUI, easy to use..
http://sqlitebrowser.sourceforge.net/
and when is part 2 of the tutorial? can’t wait…
fernando Says:
August 25th, 2008 at 4:44 am
great tutorial
my only problem is with the updating of data on the UITableView
your tutorial yet does not adress it and so am very interested how this is achieved
been trying lots of different methods but it doesn’t work the tableview in my root controller does not update with the new data and I know my app inserts new data cuz u see it only after I close and open by app
so really eager for your next tutorial
Brandon Says:
August 25th, 2008 at 12:35 pm
Hahaha,
Prob explains why it has been weeks and I have yet to be accepted to the developer program. I’ll prob get a cease and desist letter soon…LOL
bobcubsfan Says:
August 25th, 2008 at 1:04 pm
I hope not. You have been a great teacher, Master Yoda!
Aaron Says:
August 25th, 2008 at 2:35 pm
I cannot seem to get the data to display in the table. There are no errors.
Also when I use your code I just get a callstack.
Thanks
Brandon Says:
August 25th, 2008 at 2:58 pm
Aaron,
When you say “use your code” are you referring to the sample code?
This current tutorial will not display data in the table (yet). I will release the part 2 some time this week which will detail this.
If you would like to post some of your callstack here, I would be happy to help you with your issue.
Quick question: Are you running the latest version of the iPhone SDK?
nandihno Says:
August 25th, 2008 at 4:27 pm
cool so some time this week the next tut??
like i said..i want to know how the RootViewController gets refreshed with data…
!
Adam Says:
August 26th, 2008 at 9:30 am
Wow Brandon, you are putting your ass on the line for us. haha
kiwi Says:
August 26th, 2008 at 9:29 pm
Nice examples mate!
Would be nice to have the conclusion to this series soon
Also, another tutorial on how to save stuff from a NSMutableArray to the iPhone but NOT using SQLLite
thanks!
Aaron Says:
August 27th, 2008 at 8:58 am
Hi Brandon, I am running 3.1
Ill try the latest to see if that helps.
Thanks
Michael Says:
August 27th, 2008 at 4:43 pm
I built a small program using your tutorial as a template. Great template by the way. Works beautifully on my mac. However, when I go to deploy it to the phone, I get “2008-08-27 18:40:38.658 iQuit[344:20b] *** Terminating app due to uncaught exception ‘NSInternalInconsistencyException’, reason: ‘Failed to copy the database. Error: Operation could not be completed. No such file or directory.’”
Ahhhg - Any ideas?
Brandon Says:
August 28th, 2008 at 8:21 am
They are coming sorry for the delay.
School > me
Part 2 will be up by Monday if not earlier…I promise.
bobcubsfan Says:
August 29th, 2008 at 9:03 am
Maybe it was in a different thread. How about a link to “My Posts?” Apple does this. Apple also has a link to “My questions.”
Brandon Says:
August 29th, 2008 at 9:54 am
I’ll look for a Wordpress plugin to do that. I you find one, send me the link.
If one does not exist, I can write one at some point…
bobcubsfan Says:
August 29th, 2008 at 4:12 pm
Went to Wordpress web. Difficult to find anything meaningful.
Lali Says:
August 30th, 2008 at 8:48 am
Great tutorial! It helped me a lot. I like your explications, they are clear and comprehensible.
Can you explain please the CreateEditableCopyIfNeeded procedure as well?
I’m new to this platform.
Bruce Says:
August 31st, 2008 at 8:42 pm
Nice tut!! and nice blog too, I am sharing this tut on my own blog, thanks dude!
iPhone Programming Tutorial - Creating a ToDo List Using SQLite Part 2 | iCodeBlog Says:
September 2nd, 2008 at 11:35 am
[...] iPhone Programming Tutorial - Creating a ToDo List Using SQLite Part 1 [...]
Jenny Says:
September 2nd, 2008 at 1:40 pm
Thank you! These are great tutorials. They’re well done, easy to follow and work! I know it must take a lot of time for you to create one and I’m anxious for more when you get a chance. Thanks again — You’re a great teacher!
Simon Marshall Says:
September 10th, 2008 at 5:58 am
Great tutorial…just wondering how you set your “sqlite3_stmt” and “SQLITE_OK” to those colours?
any info or tips greatly appreciated.
keep up the good work
Brandon Says:
September 10th, 2008 at 11:33 am
@simon,
Are you talking about the syntax highlighting. XCode should be doing that automatically for you. I didn’t do anything special.
Perhaps you need to update XCode. Also, try quitting your app and starting it again…
iPhone Programming Tutorial - Creating a ToDo List Using SQLite Part 3 | iCodeBlog Says:
September 10th, 2008 at 7:39 pm
[...] iPhone Programming Tutorial - Creating a ToDo List Using SQLite Part 1 [...]
Pete Says:
September 16th, 2008 at 11:32 pm
Thanks Brandon, this has been a great help…
I hope you’ll reconsider doing a tutorial on audio & video aspects of the iPhone. Like recording and playing audio and video’s and saving them to sqlite or even displaying them on table views for the user to browse through…
Am I asking too much? Hehehehehe Hope not…
Thnks again!
Varun Prakash Says:
September 19th, 2008 at 1:55 am
Hi,
I did everything exactly the same as mentioned in the tutorial, still when I build and go the application, it crashes.
Can you tell me what is a possible issue?
Brandon Says:
September 19th, 2008 at 8:23 am
@Varun,
Have you tried downloading the sample code? Post some of your errors so that I can help you troubleshoot it. Also, one major source of error is not importing the sqlite library into your project. Did you complete that step.
@Prayag
Soon…It’s almost done. Will be out sometime next week.
pfargo Says:
September 20th, 2008 at 10:43 am
Kinda redundant to add my thanks Brandon but want you to know new people are glomming on.
Your efforts are greatly appreciated.
Dave Says:
September 21st, 2008 at 4:55 pm
Learned a lot about databases in XCode. Thanks for the excellent examples, but when I compile and run I get no errors but it quits unexpectedly and crashes. I’m guessing I forgot to release something?
Thanks.
Dave Says:
September 21st, 2008 at 5:08 pm
Update:
The console error message was that :
‘Assertion failure in -[todoAppDelegate initializeDatabase]
Terminating due to uncaught exception. Failed to open
database with message ‘not an error’.
Weird.
Dave Says:
September 21st, 2008 at 5:50 pm
Ooops! My bad. Missing last couple lines of code!
Fixed now.
Thanks.
iPhone Programming Tutorial - Creating a ToDo List Using SQLite Part 4 | iCodeBlog Says:
September 22nd, 2008 at 5:47 pm
[...] iPhone Programming Tutorial - Creating a ToDo List Using SQLite Part 1 [...]
Chip Says:
September 24th, 2008 at 11:20 am
Hey Brandon, I am extremely thankful for your 4 part SQL Lite database driven application. My background is that I use to be a dynamic database driven application development developer (5 years ago). I mainly used Microsoft’s Active Server Pages with SQL Server and Oracle Databases. I feel very confident with your SQL language and calls. However, though I typed every line of code that you laid out in your tutorial . . . AND . . . have printed out EVERY PAGE of the Apple iPhone & C related documentation from the iPhone Developers Program, I do not understand how to create an application on my own. I would have NO IDEA how (and why) you do what you do, when you do it. I am VERY lost. PLEASE lead me to an “iPhone Applications for Dummy’s” website or book or something . . . EVEN BETTER . . . do you know of a training facility that I could attend in Southern California? Because as it stands now. I am just mimicking your code (and appreciating you for it) and my light is not coming on as to how I would have been able to have done what you do on my own. As you can see, I am anxious to learn. PLEASE HELP ME, Brandon. Thanks.
Brandon Says:
September 24th, 2008 at 11:53 am
@Chip
Have you read any of my Hello World Tutorials. I have a few more on the site…They should provide a little more insight for you as the SQL tutorials are a little more advanced. Also, in these tutorials, I have assumed you completed my previous tutorials.
I don’t know of any training areas. I’m not even sure if they exist as Apple is pretty lame when it comes to their NDA. Even me having this site violates it.
Read through the earlier tutorials on my site and feel free to post any questions either in the comments section or in the forums.
thank for reading…
iPhone Application And Website Development: All Tools And Tutorials You Need | Athena Design - The Lounge Says:
September 29th, 2008 at 12:25 am
[...] a ToDo List Using SQLite - Part 1 -Part 2 - Part [...]
Ramit Says:
October 1st, 2008 at 2:09 am
Good job Brandon, nicely presented… would’nt be possible without this tutorial
Redth Says:
October 1st, 2008 at 11:47 am
I’m just getting into iphone dev as a c# coder, and it’s a pretty different experience. I’ve had a heck of a time finding some solid concrete examples to start with that are between hello world, and complicated…
This looks like the first set of tutorials that hit just the right spot! I’ll be using this first series to start working on. Kudos, and thanks so much!
Brandon Says:
October 1st, 2008 at 11:57 am
@redth,
no problem! I would not suggest starting here though. I have some easier tutorials that will get you started on understanding Interface Builder.
Look in the sidebar of the site. Under the “tutorials” heading. They are listed in the order I write them.
Deamb Says:
October 2nd, 2008 at 12:38 pm
Hello Brandon,
I have 3 Apps published on the AppStore, but I love your tutorials !
They are very basic apps, oriented towards aviation needs, and I am learning a lot from you !
Please keep it up !
One question though, already asked by Phil on Aug 19th (but I haven’t seen a response) :
Can we build the database with a GUI ?
The one I have in mind has about 200 entries, and it would take me forever in Terminal !
Thanks
backwardselvis Says:
October 2nd, 2008 at 1:10 pm
@Deamb
You do not have to do each sql statement line by line
1) enter into the sqlite3 command prompt
sqlite3
2)Copy and paste the text below and press enter
CREATE TABLE test(pk INTEGER PRIMARY KEY, text VARCHAR(25), priority INTEGER, complete BOOLEAN);
INSERT INTO test(text,priority,complete) VALUES(’Take out the trash’,3,0);
INSERT INTO test(text,priority,complete) VALUES(’Do Computer Science homework’,1,0);
INSERT INTO test(text,priority,complete) VALUES(’Learn Objective C’,1,0);
INSERT INTO test(text,priority,complete) VALUES(’DIGG this tutorial’,2,0);
This should allow you to easily recreate databases.
Shani Says:
October 3rd, 2008 at 6:38 am
Hello Brandon,,
i have changed mine data base file but my application give old reference . and does not leave old reference. mine application could not fine updated database even if i removed old database reference and also deleted it. so can u plz help me for that . what should i do ? i also removed build filed. then also i could not use new db.
i am too much confusing and project deadline is near . so plz as soon as give solution for that.
Thanks Regards
sunny.
Deamb Says:
October 3rd, 2008 at 6:47 am
@ Backwardselvis :
Thanks for the tip, but I still have to write all 200 entries into something like text editor before I paste it into terminal !
Anyway, I guess I’ll do it !
Brandon Says:
October 3rd, 2008 at 8:23 am
@Shani,
If I understand your question correctly, you simply need to uninstall you application from the simulator. This is done by clicking and holding your mouse down on the “home” screen in the simulator. The app icons will “wiggle” and you can click the “X” in the corner. It will prompt you to remove.
After doing this, add your new database to your project and the reference will now match up.
@Deamb
Have you tried Googleing an SQLite GUI. I haven’t seen one. I think in the comments of another post someone suggests a web based one. I’ll post if I find one.
Serge ostrowsky (Deamb) Says:
October 4th, 2008 at 4:24 am
No brandon, no luck so far, except 250$ ones, I haven’t found anything…
I’ll look for a web-based one though
Thanks again
Ulthor Says:
October 4th, 2008 at 12:10 pm
Brandon:
I’ll add my kudos to the others. Thanks very much for your effort in writing these tutorials. I would not be able to get started as an iPhone developer without you. You Rock!!!
Ulthor Says:
October 4th, 2008 at 12:30 pm
OK, an actual question:
About
@interface todoAppDelegate (private)
The syntax here looks like a category named “private”. Is that true?
Brandon Says:
October 4th, 2008 at 12:37 pm
Not quite… What this line is say is, we are declaring a private interface for this class. It’s sort of like declaring a road map for this class.
Thanks for reading…
Neo Says:
October 6th, 2008 at 3:33 am
Hey guys,
Thanks for this tutorial Brandon!
For those of you who want to test the code after completing this tutorial, just add this code to RootViewController.m:
First:
#import “todo.h”
..and then..
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
todoAppDelegate *appDelegate = (todoAppDelegate *) [[UIApplication sharedApplication]delegate];
return appDelegate.todos.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *MyIdentifier = @”MyIdentifier”;
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:MyIdentifier] autorelease];
}
todoAppDelegate *appDelegate = (todoAppDelegate *) [[UIApplication sharedApplication]delegate];
todo *t = (todo *)[appDelegate.todos objectAtIndex:indexPath.row];
[cell setText:[NSString stringWithFormat:@"%@ - #%d", t.text, t.primaryKey]];
// Set up the cell
return cell;
}
Maybe you want to add this to your tutorial Brandon, shows some progress ![]()
Thanks anyway!
Brandon Says:
October 6th, 2008 at 10:33 am
@Neo
Thanks for posting this. The reason that I didn’t add that code to this tutorial is I show how this is done is part 2 of the todo series. I should probably make that more clear by linking to the next tutorial at the bottom.
Anyway, great job man!
Neo Says:
October 7th, 2008 at 2:57 am
Brandon: Yes you’re right, I know that there are 4 parts. But I just did it for myself - to check if the sql database works -before starting with part 2!
Thanks!
Albert Says:
October 15th, 2008 at 4:40 pm
Brandon,
not sure what’s going on but when I try to launch the App I get a “Unable to read symbols for “/System/Library/Frameworks/UIKit.framework/UIKit” (file not found).” I know this is not where the UIKit framework is located and I can’t figure out how to change it. Your help is much appreciated.
Ade C Says:
October 19th, 2008 at 8:35 am
Just wanted to say Thanks
As a tip for noobs like me, i have found that hitting the Build and Go button frequently through the tut helps in finding errors/typos earlier which makes them easier to fix. Maybe a suggestion for Brandon to put a B&G instruction after each section to suggest that the app should build at that point with no errors. Believe me, I do this a lot now and it helps me a lot!!!
Anyway, gotta go… I’ve got the next part to follow
See you in Prt2 comments!!
Joe Says:
October 20th, 2008 at 8:09 am
Hey, great tutorial, however I seem to be having trouble with something.
I’ve followed your instructions quite closely, but cannot seem to make changes to the database in a programmatic way. That is to say that, I have a dehydrate routine, but the changes it makes do not seem to persist, the next time I load the program, the previous values are still present… this is also true when I directly edit the todo.sqlite db from the terminal… so far as the project is concerned everything should be working…
Any ideas ?
.joe
vBharat.com » iPhone Programming Tutorial - Creating a ToDo List Using SQLite Part 1 Says:
October 21st, 2008 at 9:17 pm
From vBharat.com » iPhone Programming Tutorial - Creating a ToDo List Using SQLite Part 1…
Learn to create a simple todo list for the iphone. Great iPhone programming tutorial for beginners to intermediate….
Stefan Says:
October 30th, 2008 at 5:03 pm
You should’ve at least mentioned in one sentenced that your example is borrowed from the Apple examples … but good comments, though!
Brandon Says:
October 31st, 2008 at 7:24 am
@Stefan,
What do you mean man? Did you miss the line that states
“Much of the code for these has already been written for us inside of Apple’s SQLBooks tutorial”
implying that some of this code came from Apple’s examples. What else would you like me to add?
Shani Says:
November 3rd, 2008 at 8:08 am
Hello Bradon..
it’s nice tutorial and too much help in to create new application.
one problem i have …
i have changed the name of project then i got error in sqlite3_prepare_v2 statement. it gives error in sqlite_error.
so can please give any solution for this ? what should do now ?
Thanks Regards,
Shani
Jerod Says:
November 17th, 2008 at 3:23 pm
Just wondering where the NSAssert1 will output its errors. I am not seeing it print anything to the Xcode console.
Kyle Hayes Says:
November 23rd, 2008 at 5:02 pm
How come in the initializeDatabase method you don’t initialize the self.todos directly with the alloc/init methods and instead you create a temp object?
Dmitry Says:
November 24th, 2008 at 2:05 pm
if I use NON Navigation Based Application Project with link to SQLITE i have error of building aapp. Why?????? Just try to add link and build empty project. Please HELP!!!
ObiOne Says:
December 1st, 2008 at 1:39 am
It’ll be awesome if you showed us how to do this with some Tabs. Also, I’m new and I’ve been following along your tutorial. I’m trying to add another text box called text1 and I added it to the sql table. But everytime I run the app… it crashes and says there’s no column called text1. Any help would be appreciated.
Coach Hire Says:
December 20th, 2008 at 9:00 pm
Hello. I think you are eactly thinking like Sukrat. I really loved the post.
Todo List Application -iphone programming | iPhone App Devs Says:
December 26th, 2008 at 12:43 pm
[...] Add the SQLite3 Framework [...]
Adelaide Web Design Says:
December 27th, 2008 at 2:40 am
Cheers, this is an amazing tutorial, great work.
Good luck with the child too!!
links for 2008-12-27 « Brent Sordyl’s Blog Says:
December 27th, 2008 at 8:00 am
[...] iPhone Programming Tutorial - Creating a ToDo List Using SQLite Part 1 | iCodeBlog If you have been following my tutorials, you know that we have been working primarily with UITableViews. This is mostly because SO many applications can be developed using this simple control. This final UITableView tutorial will be taking all of the skills learned from previous tutorials, putting them all together, and adding SQLite to create a prioritized To-Do list. I will also be showing you how to add multiple columns to your table cells and we will be exploring some of the other controls that the iPhone has to offer. What good would the tutorials be if we didn’t use them to create something useful. (tags: iphone development) [...]
rob Says:
January 11th, 2009 at 9:06 am
What a waste of time! This is just an incomplete rehash of the apple sqlbooks tutorial.
christian Says:
January 23rd, 2009 at 1:52 am
Thanks a lot for the tutorial!
One question, how should I do if I would like to bind more parameters? For instance:
const char *sql = “SELECT text FROM todo WHERE pk=? AND px=?”;
Thanks, chr
christian Says:
January 23rd, 2009 at 2:09 am
Ops, I presume the second parameter specifies the index
Inscrutable Says:
February 2nd, 2009 at 6:11 pm
Maybe this is simpler than I think, but how do we use multiple tables? how do I tell the app which one i’m trying to get at?
In the project it has something like this: const char *sql = “SELECT pk FROM thetable”;
If i want to initialize another table and be able to query it independently, do I do something like this:
const char *sql1 = “SELECT pk FROM thetable2″; ?
Thanks for any input (BTW your tutorial explained alot).
Ben Ipsen Says:
February 3rd, 2009 at 10:08 am
I’m getting “error: ‘database’ undeclared (first use in this function)” compile-time for two lines:
in todoAppDelegate.m:
if(sqlite3_open([path UTF8String], &database) == SQLITE_OK)
and in Todo.m:
NSAssert1(0, @”Failed to prepare statement with message ‘%s’.”,sqlite_errmsg(databse));
I’m curious about my arguments not working correctly in the second case as I also had trouble with the initWithName method arguments in the fruit example. That aside, it seems the first instance (passed as reference) should definitely work. Please let me know if there’s a place I may have missed the declaration.
Thanks!
Ben
Another Ben Says:
February 6th, 2009 at 8:17 pm
I’m getting a “_TERMINATING_DUE_TO_UNCAUGHT_EXCEPTION__” error and am unsure how to use the debugger - no matter how early in the program I set my break point, I get that error!
Could I get a link to a debugger-how to, or some advice in general? Thanks!
Brandon Says:
February 12th, 2009 at 10:44 am
While my answer to this question is probably in this list of tutorials, I have yet to find it.
I have an app setup and working. It consists of 3 tab’s. Two tabs display a tableview with a list of names from two plist files. These lists are in alphabetical order with an index. The third view simply shows my logo and details about the app.
I would like to take the first view, a table view, and have it display details about the name that the user click on in the table. I want to keep my list grouped in ABC order and keep the index on the side. My details for each name are going to consists of many lines of text, and maybe a picture icon.
I am having trouble finding a place to store this detail data and how to call it in when a user click on the related name in my first tableview.
The second tableview is just a tableview, with no details.
I have been stuck on this for days. Any help would be great! Thanks for all the great advice in these tutorials!!
rabi Says:
March 4th, 2009 at 11:23 pm
Hey i am very big fan of iCodeBlog.. i learned so much from here !!
now i have some confusion regarding ,
“create a new folder in iPhone” while running my application;
so i can store user modify data !!
since i dont know the directory structure of iPhone ..
so please help me regarding this..
i request other reader as well …
Y Ramesh Rao Says:
March 7th, 2009 at 6:32 am
Hey Man Bardon… Thanks man…
I was working on this simple app where in the user sets in the to-do list then the app helps him to remember things by just giving him simple Alarm type thing… How should i work that out…
Please help me out with this Dude…
Andy Says:
March 14th, 2009 at 7:30 pm
Great tutorial. Thanks for doing this. One quick (or not) question. How would you modify this to accommodate a database with more than 1 table? In the app I’m working on I have two tables and each table has a corresponding object.
Thanks,
Andy
best fruit machines Says:
March 23rd, 2009 at 2:55 am
the comments here are having a laugh - i’ve added your blog to my netvibes account, keep up the good work
Alok Says:
March 28th, 2009 at 12:56 pm
Hi, i have some simple problem. i am using the sqlite to save data.In My application when i Insert the some record into database table then record inserting properly, but next time when i am re-inserting the record after closing the application. my database file not taking changes. it showing previous record again and again. i not getting any Query error at Console, Console saying that I am inserting the record with row id. but my database file is not getting the Changes.
What am i missing something. or when i close my application suddenly my database file not committed with new record. Did i have to code at the application terminate Point?
Thanks in Advance.
Georg Says:
March 30th, 2009 at 6:27 am
Thanks for an excellent tutorial.
I’m trying to figure out how to get the data from all columns in one row, where primarykey is equal to variable “i” and put the data in a nsmutablearray, one column per array entry.
Here’s an examle
contacts table would contain:
pk(INTEGER), surname (VARCHAR), lastname (VARCHAR), email (VARCHAR).
sql: “SELECT * FROM contacts WHERE pk = ?”
and ? would be replaced by variable “i”
Any pointers in the right direction would be much appreciated
thanks
G
Tim Says:
April 16th, 2009 at 5:55 am
Great tutorials Brandon,
I’m getting a Linking errors
“_sqlite3_colum_int” , referenced from:
-[totdoAppDelegate initializeDatabase] in todoAppDelegate.0
“_sqlite3_colum_text”, referenced from:
-[Todo initWithPrimaryKey:database:] in Todo.o
symbol(s) not found
collect2: ld returned 1 exit status.
any help would be appreciated I really want to continue to part 2.
Tim Says:
April 16th, 2009 at 6:00 am
Might Help if I spell Column right. All fixed thanks for the great tutorials.
pierre Says:
May 10th, 2009 at 9:33 am
thanks Brandon, I’m learning the new trade. I went through your tutorials. I got stuck with the Sqlite3 part one. What do I do with the follow error, “__terminating_due_to_uncaught_exception__” when I run my app with the simulator. When compiled no errors shown.
thanks
Joel Says:
June 8th, 2009 at 9:58 am
I must say I am learning alot from your tutorials. Much appreciated.
if you could help with this that would be great:
on todo.m I am getting an error on @synthesize primaryKey,text;
its saying “error: type of property ‘primaryKey’ does not match type of ivar ‘primaryKey’
this is the property I have for it
@property (assign,nonatomic,readonly) NSInteger primaryKey;
Tommy Says:
June 11th, 2009 at 7:17 am
Thanks for the tutorial. It has taught me a lot. Just one question. If I wanted to do a QUERY for a specific record, from a picker for example. Would I add a new method and select statement or just alter the original “SELECT text FROM todo WHERE pk=?”
Thanks in advance.
Matt Says:
June 15th, 2009 at 10:09 pm
Hey Brandon!
I’ve written tons of db code, but never used sqlite3, so this is just the right amount of info for me. Thanks.
One thing I’d suggest for efficiency’s sake is to do avoid doing nested queries to to retrieve all of the values from the table.
I wrote the code for my app to essentially do the following:
syntactical, schema and other liberties taken…
pepare(db, “select pk, text from table”, &statement);
while(ROW == step(statement))
{
Todo * t = [[Todo alloc] initWithStatement:statement];
[todos addObject:t];
[t release];
}
initWithStatement retrieves all of the columns into the Todo object rather than issuing another select statement. Nested queries are expensive. Since sqlite is in-memory and file-based, it likely doesn’t make that much of a difference, but if you were accessing a DB over a network connection it would make a huge difference.
Matt
Alejandra Gonzalez Says:
June 22nd, 2009 at 8:23 am
Hi Brandon!! great Tutorial!
I have a question, can I have my SQLite database in a remote server and make queries from my iPhone app?
do you have another tutorial showing something like that?
thanks in advance for the info
Alejandra
Ben Says:
June 22nd, 2009 at 5:49 pm
if you are getting the error:
“_TERMINATING_DUE_TO_UNCAUGHT_EXCEPTION__” (or your app just quits and there is a popup asking you to report it)
replace
“self.text = [NSString stringWithUTF8String:(char *)sqlite3_column_int(init_statement, 0)];” in Todo.m with
“char *pointer = (void *)sqlite3_column_int(init_statement, 0);
if (pointer == NULL)
self.text = nil;
else
self.text = [NSString stringWithUTF8String: pointer];”
Graham Says:
June 26th, 2009 at 4:01 pm
I don’t know if you did it already, but I’d love a line-by-line breakdown of some of the more dense code blocks. It’d help me understand what’s going on
Paul Says:
June 29th, 2009 at 1:28 am
Hi,
Thanks for this detailed tutorial on SQLite.
However, I’m a bit concerned on the number of round trips required to fetch the information. Would you also have information on how to use callbacks properly?
Thanks again.

















