<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>iPhone Programming Tutorials &#187; database</title>
	<atom:link href="/tag/database/feed/" rel="self" type="application/rss+xml" />
	<link>http://icodeblog.com</link>
	<description>iPhone Programming Tutorials</description>
	<lastBuildDate>Tue, 19 Nov 2013 19:34:46 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=4.1.1</generator>
	<item>
		<title>Simple Sqlite Database Interaction Using FMDB</title>
		<link>http://icodeblog.com/2011/11/04/simple-sqlite-database-interaction-using-fmdb/</link>
		<comments>http://icodeblog.com/2011/11/04/simple-sqlite-database-interaction-using-fmdb/#comments</comments>
		<pubDate>Fri, 04 Nov 2011 19:52:19 +0000</pubDate>
		<dc:creator><![CDATA[brandontreb]]></dc:creator>
				<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[fmdb]]></category>
		<category><![CDATA[Intermediate]]></category>
		<category><![CDATA[SQLite]]></category>

		<guid isPermaLink="false">http://icodeblog.com/?p=3595</guid>
		<description><![CDATA[Introduction
In the age where Core Data is king, the database that started it all is often overlooked. I&#8217;m talking of course about sqlite. As you may or may not know, prior to core data, sqlite was the preferred method of storing relational data on iOS devices.
Although, most developers don&#8217;t interact with sqlite directly, they still use it under the hood as the primary data store for core data. This is great and all, but there are often times when raw  ...]]></description>
				<content:encoded><![CDATA[<h4>Introduction</h4>
<p>In the age where Core Data is king, the database that started it all is often overlooked. I&#8217;m talking of course about sqlite. As you may or may not know, prior to core data, sqlite was the preferred method of storing relational data on iOS devices.</p>
<p>Although, most developers don&#8217;t interact with sqlite directly, they still use it under the hood as the primary data store for core data. This is great and all, but there are often times when raw sqlite is still the preferred storage method.</p>
<p>A few of these might include:</p>
<ul>
<li>Caching</li>
<li>Preferences</li>
<li>Simple objects</li>
<li>Portability</li>
<li>Cross platform applications</li>
</ul>
<p>Recently, I have had to make heave use of raw sqlite as a caching strategy in a new project that I&#8217;m working on. Being that we are developing a framework for other developers to include in their projects, we can&#8217;t always assume that they have their environment set up to use core data. When I was but a n00b iOS developer I did all of the crazy sqlite management by hand. See <a href="/2008/08/19/iphone-programming-tutorial-creating-a-todo-list-using-sqlite-part-1/">This post series</a>, but don&#8217;t spend too much time there because it&#8217;s embarrassing.</p>
<p>Gross right? Now, there is a much easier way to manage and interact with your sqlite databases. This library has been around for quite some time and I wish I had known about it earyly on.</p>
<h4>FMDB</h4>
<p>FMDB stands for Flying Meat Database. What a great name&#8230; This project aims to be a fully featured wrapper for sqlite.</p>
<p>You can clone their repository on <a href="https://github.com/ccgus/fmdb">their github</a>.</p>
<p>This tutorial will give you a brief introduction to using FMDB to create a database, create a table, insert, fetch, and delete data.</p>
<h4>Project Set Up</h4>
<p>The first step is to download/clone fmdb from the url above. Once downloaded drag everything inside of the src folder into your project <strong>except</strong> fmdb.m. That file contains unit tests and a main, which will cause some conflicts in your project.</p>
<p>The next step is to link in the sqlite library. To do this:</p>
<ol>
<li>Click your project in the left column of XCode</li>
<li>Click the main target in the middle column. In our case it&#8217;s &#8220;FMDBTest&#8221;</li>
<li>Click the &#8220;Build Phases&#8221; tab in the third column</li>
<li>Expand the arrow next to &#8220;Link Binary With Libraries&#8221;</li>
<li>Click the &#8220;+&#8221; button</li>
<li>Search for libsqlite3.0.dylib and double click it</li>
</ol>
<p>When you are all done, it should look like this:</p>
<p><img title="" src="/wp-content/uploads/2011/11/Screen-Shot-2011-11-04-at-12.48.10-PM.png" alt="Screenshot" /></p>
<div style="clear: both;">
<p>Now, that we have the library in place, let&#8217;s write some code.</p>
<h4>Creating A Database</h4>
<p>Obviously <em>where</em> you create your database is up to you, but we are going to do it in the appDelegate.</p>
<p>In addition to working with existing databases, fmdb can <em>easily</em> create any number of databases for you on the fly. After importing FMDatabase.h in our AppDelegate.m file, we can add the following code to the <code>application:didFinishLaunching</code> method.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span>paths <span style="color: #002200;">=</span> NSSearchPathForDirectoriesInDomains<span style="color: #002200;">&#40;</span>NSDocumentDirectory, NSUserDomainMask, <span style="color: #a61390;">YES</span><span style="color: #002200;">&#41;</span>;
<span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>docsPath <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>paths objectAtIndex<span style="color: #002200;">:</span><span style="color: #2400d9;">0</span><span style="color: #002200;">&#93;</span>;
<span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>path <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>docsPath stringByAppendingPathComponent<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;database.sqlite&quot;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
FMDatabase <span style="color: #002200;">*</span>database <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>FMDatabase databaseWithPath<span style="color: #002200;">:</span>path<span style="color: #002200;">&#93;</span>;</pre></td></tr></table></div>

<p>First, we resolve the path to the documents directory. Be careful, if you don&#8217;t <em>need</em> your database to be backed up, use the cache directory instead. When you send a path to the databaseWithPath method of fmdb, it first checks if the database exists, and if not, it creates it. Similarly, we could copy an existing database to the documents directory and source it the exact same way.</p>
<h4>Opening The Database And Creating Tables</h4>
<p>In order to perform any action on the database, it must first be opened. Here is the code to open the database and create a users table. Don&#8217;t worry about closing it right now, we will do that when we are all done.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">&#91;</span>database open<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#91;</span>database executeUpdate<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;create table user(name text primary key, age int)&quot;</span><span style="color: #002200;">&#93;</span>;</pre></td></tr></table></div>

<p>Here we first call the open method of the database to open it. Next, we use the executeUpdate method to create the table. Make sure you use this method and <strong>not</strong> executeQuery when creating a table. This is a common error. The database should look like this after our update:</p>
<p><img title="" src="/wp-content/uploads/2011/11/Screen-Shot-2011-11-04-at-1.05.12-PM.png" alt="Screenshot" width="550" /></p>
<div style="clear: both;">
<p>After we are all done here we close the database.</p>
<h4>Inserting And Deleting Data</h4>
<p>Inserting data using sqlite is very straight forward. You can either build your strings and pass them in directly OR use the sqlite format using &#8220;?&#8217;s&#8221; and letting fmdb do the work. Below is an example of each:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">// Building the string ourself</span>
<span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>query <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSString</span> stringWithFormat<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;insert into user values ('%@', %d)&quot;</span>,
<span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;brandontreb&quot;</span>, <span style="color: #2400d9;">25</span><span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#91;</span>database executeUpdate<span style="color: #002200;">:</span>query<span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #11740a; font-style: italic;">// Let fmdb do the work</span>
<span style="color: #002200;">&#91;</span>database executeUpdate<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;insert into user(name, age) values(?,?)&quot;</span>,
<span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;cruffenach&quot;</span>,<span style="color: #002200;">&#91;</span><span style="color: #400080;">NSNumber</span> numberWithInt<span style="color: #002200;">:</span><span style="color: #2400d9;">25</span><span style="color: #002200;">&#93;</span>,<span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;</pre></td></tr></table></div>

<p>Generally, the second route is preferred as fmdb will do some of the sanitizing for your (such as add slashes to single quotes).</p>
<p>Now that we have some data in our database, let&#8217;s delete it. The following code will delete all users with an age of 25:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">&#91;</span>database executeUpdate<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;delete from user where age = 25&quot;</span><span style="color: #002200;">&#93;</span>;</pre></td></tr></table></div>

<p>And that should remove both of the records that we inserted.</p>
<h4>Querying The Database</h4>
<p>Querying the database is a bit more tricky than inserting and deleting. FMDB has some great utility methods for helping us out in certain circumstances, but for this tutorial, we will assume the don&#8217;t exist and show you how to fetch data out. If you are following along in a sample application, put this code before your delete code (so that we actually have some data to work with).</p>
<p>Below is an example of fetching all users from the database:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="objc" style="font-family:monospace;">FMResultSet <span style="color: #002200;">*</span>results <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>database executeQuery<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;select * from user&quot;</span><span style="color: #002200;">&#93;</span>;
<span style="color: #a61390;">while</span><span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span>results next<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
    <span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>name <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>results stringForColumn<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;name&quot;</span><span style="color: #002200;">&#93;</span>;
    NSInteger age  <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>results intForColumn<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;age&quot;</span><span style="color: #002200;">&#93;</span>;        
    NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;User: %@ - %d&quot;</span>,name, age<span style="color: #002200;">&#41;</span>;
<span style="color: #002200;">&#125;</span>
<span style="color: #002200;">&#91;</span>database close<span style="color: #002200;">&#93;</span>;</pre></td></tr></table></div>

<p>We first query the database using the <code>executeQuery</code> method. This returns to us an FMResultsSet. Next we start a while loop that continues while there are results to be retrieved and fetch out each of our data points. Finally, we just print out each user.</p>
<p>And finally, our database gets close&#8230;</p>
<h4>Conclusion</h4>
<p>This concludes our sqlite using fmdb tutorial. As always, if you have any questions, please leave them here or <a href="http://twitter.com/brandontreb">write me on twitter</a>.</p>
<p>You can download the source for this tutorial <a href="/?attachment_id=3599">here</a></p>
<p>Happy iCoding!</p>
]]></content:encoded>
			<wfw:commentRss>http://icodeblog.com/2011/11/04/simple-sqlite-database-interaction-using-fmdb/feed/</wfw:commentRss>
		<slash:comments>16</slash:comments>
		</item>
		<item>
		<title>iPhone Programming Tutorial &#8211; Creating a ToDo List Using SQLite Part 4</title>
		<link>http://icodeblog.com/2008/09/22/iphone-programming-tutorial-creating-a-todo-list-using-sqlite-part-4/</link>
		<comments>http://icodeblog.com/2008/09/22/iphone-programming-tutorial-creating-a-todo-list-using-sqlite-part-4/#comments</comments>
		<pubDate>Mon, 22 Sep 2008 23:47:39 +0000</pubDate>
		<dc:creator><![CDATA[brandontreb]]></dc:creator>
				<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[Intermediate]]></category>
		<category><![CDATA[iphone]]></category>
		<category><![CDATA[iphone programming]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[SQLite]]></category>
		<category><![CDATA[sqlite3]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://icodeblog.com/?p=392</guid>
		<description><![CDATA[This is the final installment of our 4 part series of creating a Todo list for the iPhone.  In this tutorial, I will detail how to add and delete new todo objects from the SQLite database.  Make sure that you have completed the following tutorials before you begin this one:

<a title="Permanent Link to iPhone Programming Tutorial - Creating a ToDo List Using SQLite Part 1" rel="bookmark" href="/2008/08/19/iphone-programming-tutorial-creating-a-todo-list-using-sqlite-part-1/">iPhone Programming Tutorial &#8211; Creating a ToDo List Using SQLite Part 1</a>
<a  ...]]></description>
				<content:encoded><![CDATA[<p style="text-align: left;">This is the final installment of our 4 part series of creating a Todo list for the iPhone.  In this tutorial, I will detail how to add and delete new todo objects from the SQLite database.  Make sure that you have completed the following tutorials before you begin this one:</p>
<ul style="text-align: left;">
<li><a title="Permanent Link to iPhone Programming Tutorial - Creating a ToDo List Using SQLite Part 1" rel="bookmark" href="/2008/08/19/iphone-programming-tutorial-creating-a-todo-list-using-sqlite-part-1/">iPhone Programming Tutorial &#8211; Creating a ToDo List Using SQLite Part 1</a></li>
<li><a title="Permanent Link to iPhone Programming Tutorial - Creating a ToDo List Using SQLite Part 1" rel="bookmark" href="/2008/09/02/iphone-programming-tutorial-creating-a-todo-list-using-sqlite-part-2/">iPhone Programming Tutorial &#8211; Creating a ToDo List Using SQLite Part 2</a></li>
<li><a href="/2008/09/10/iphone-programming-tutorial-creating-a-todo-list-using-sqlite-part-3/">iPhone Programming Tutorial &#8211; Creating a ToDo List Using SQLite Part 3</a></li>
</ul>
<p style="text-align: left;">When you have completed this tutorial, you should have a main screen that looks something like this:</p>
<p style="text-align: left;"><a href="/wp-content/uploads/2008/09/mainscreen1.png"><img class="size-full wp-image-394 aligncenter" title="screenshot" src="/wp-content/uploads/2008/09/mainscreen1.png" alt="" width="386" height="742" /></a></p>
<p style="text-align: left;">Let&#8217;s get started&#8230;</p>
<p style="text-align: left;">The first thing we need to do is add the UIBarButtonItem items to the NavigationBar so that we get the &#8220;Edit&#8221; and &#8220;Add&#8221; button.  Open up <strong>RootViewController.m</strong> and add the following code to the <strong>viewDidLoad</strong> method.</p>
<p style="text-align: left;"><a href="/wp-content/uploads/2008/09/viewdidload1.png"><img class="size-full wp-image-396 aligncenter" title="viewdidload" src="/wp-content/uploads/2008/09/viewdidload1.png" alt="" width="500" height="107" /></a></p>
<p style="text-align: left;">The first thing we see is the line that sets the leftBarButtonItem to self.editButtonItem.  This automatically adds the &#8220;Edit&#8221; button to the NavigationController.  Also, it sets up the functionality that allows the &#8220;delete&#8221; buttons to be displayed when the button is pressed.  You can see this functionality if you do a &#8220;Build and Go&#8221; at this step.  Next, I have manually created a UIBarButtonItem and added it to the navigationbar.  This can be done in Interface Builder, but I wanted to show you how to do it manually and assign an action to it (I&#8217;m sure you will require this functionality in a future program).  Here is a break down of the parameters:</p>
<ul style="text-align: left;">
<li>initWithTitle &#8211; The text to be displayed on the button</li>
<li>style &#8211; How the button will look</li>
<li>target &#8211; The class object that handles the messages sent from this button</li>
<li>action &#8211; The method to be called when the button is passed.  We can use @selector and give it the name of the function to call.</li>
</ul>
<p style="text-align: left;">Finally, we assign this button to the rightBarButtonItem.  If you do a Build and Go, it should error since we haven&#8217;t created the addTodo method. We will do that in a bit.  Now, let&#8217;s create a method inside of our Todo object that will add new Todos to the database.</p>
<p style="text-align: left;">Open up <strong>Todo.h</strong> and add the following code:</p>
<p style="text-align: left;"><a href="/wp-content/uploads/2008/09/todo1.png"><img class="alignnone size-full wp-image-398" title="todo" src="/wp-content/uploads/2008/09/todo1.png" alt="" width="363" height="33" /></a></p>
<p style="text-align: left;">So in addition to the <strong>insertNewTodoIntoDatabase</strong> method, we also see the deleteFromDatabase method signature.  I have just added this so I don&#8217;t have to come back to it later.  We will be implementing this when I show you how to delete todos from the database.  One thing to note about the <strong>insertNewTodoIntoDatabase </strong>method is it has a &#8220;+&#8221; rather than a &#8220;-&#8221; sign.  This means that it is a static method.  Static methods are associated with the class not the instance meaning we can call this method without instanciating this class.  So we can do stuff like Todo.insertNewTodoIntoDatabase.  Now we will implement this method.</p>
<p style="text-align: left;">Before we can do this, we must declare a few more static sqlite3_statement&#8217;s.  Add the following statements to the top of <strong>Todo.m</strong></p>
<p style="text-align: left;"><a href="/wp-content/uploads/2008/09/todomstatic1.png"><img class="size-full wp-image-400 aligncenter" title="todomstatic" src="/wp-content/uploads/2008/09/todomstatic1.png" alt="" width="270" height="30" /></a></p>
<p style="text-align: left;">Nothing new here&#8230;Now implement the following method:</p>
<p style="text-align: left;"><a href="/wp-content/uploads/2008/09/todom31.png"><img class="size-full wp-image-399 aligncenter" title="todom3" src="/wp-content/uploads/2008/09/todom31.png" alt="" width="499" height="197" /></a></p>
<p style="text-align: left;">This is similar to our update method.  Notice that we are inserting default values into the database.  This is so we don&#8217;t run into any problems with null or nil values.  The most important part of this method is the fact that it returns the primary key of the newly created todo object.  This will be used later so we can immediately transition to the todo when the &#8220;Add&#8221; button is pressed.  The last thing we need to do to the todo object is update the dehydrate method so that the todoText gets saved if it gets changed.  Update the dehydrate method to look like this:</p>
<p style="text-align: left;"><a href="/wp-content/uploads/2008/09/dehydrate1.png"><img class="size-full wp-image-401 aligncenter" title="dehydrate" src="/wp-content/uploads/2008/09/dehydrate1.png" alt="" width="500" height="257" /></a></p>
<p style="text-align: left;">There are only a few minor changes here.  First we see the &#8220;text = ?&#8221; part added to the sql statement.  This is simply so we can update the text of the todo.  The other change is we bound the self.text property to the 1st question mark in the sql statement.  One thing to notice is we call [self.text UTF8String].  This is because sqlite3_bind_text takes a (char *).  This will convert an NSString to an acceptable format.</p>
<p style="text-align: left;">Now we need to add a method inside of our RootViewController to add a todo.  This is the method that will be called when the user presses the &#8220;Add&#8221; button.  Inside of RootViewController.m add the following code:</p>
<p><a href="/wp-content/uploads/2008/09/1-rootviewcontroller1.png"><img class="size-full wp-image-422 aligncenter" title="1-rootviewcontroller" src="/wp-content/uploads/2008/09/1-rootviewcontroller1.png" alt="" width="500" height="179" /></a></p>
<p style="text-align: left;">First, we get a reference to the appDelegate object.  This is because we need to call its addTodo method.  Next, we instantiate the TodoViewController if it has not already been instantiated.  We need this around because we will push it on to the view stack and transition to it after we create our new todo object.  After this is done, we call the addTodo method of the appDelegate.  It will return the newly created todo object and the view will be transitioned to its detail screen in order to update its details.  Now we need to implement the method addTodo inside of our appDelegate.  Open up todoAppDelegate.h and add the following code to create the method signature.</p>
<p style="text-align: center;"><a href="/wp-content/uploads/2008/09/2-appdelegateh1.png"><img class="size-full wp-image-423 aligncenter" title="2-appdelegateh" src="/wp-content/uploads/2008/09/2-appdelegateh1.png" alt="" width="107" height="18" /></a></p>
<p style="text-align: left;">
<p style="text-align: left;">Now, let&#8217;s implement this method.  Open up todoAppDelegate.m and add the following code:</p>
<p style="text-align: left;">
<p style="text-align: left;"><a href="/wp-content/uploads/2008/09/3-appdelegatem1.png"><img class="size-full wp-image-424 aligncenter" title="3-appdelegatem" src="/wp-content/uploads/2008/09/3-appdelegatem1.png" alt="" width="500" height="101" /></a></p>
<p style="text-align: left;">
<p>First, we are calling the insertNewTodoIntoDatabase method of the Todo object.  Notice that we are simply calling the method without first building an instance of a todo object.  As I said in tutorial 3, this is because that method is static and gets called without building an instance of the class.  Next, we insatiate the todo object that was just created by calling its initWithPrimaryKey method.  This will give us reference to the new todo object.  Finally, we append this todo to the end of our todos array.  Since our UITableView is updated with this array, it will automatically include the new todo object.  The last line just returns this todo object.</p>
<p>Remember is the last tutorial we made it so the users could update the status and the priority of a todo?  Well, now we also need to give them the ability to update the text of the todo.  So open up TodoViewController.h and add the following code:</p>
<p style="text-align: center;">
<p><a href="/wp-content/uploads/2008/09/5-todoviewcontrollerh1.png"><img class="alignnone size-full wp-image-425" title="5-todoviewcontrollerh" src="/wp-content/uploads/2008/09/5-todoviewcontrollerh1.png" alt="" width="430" height="271" /></a><a href="/wp-content/uploads/2008/09/5-todoviewcontrollerh1.png"><br />
</a></p>
<p style="text-align: left;">
<p>Ok, so I&#8217;m guessing you are wondering why the UITextView for the todoText object has been changed to a UITextField.  Well, I will tell you.  UITextView doesn&#8217;t have the methods that we need to save the text with our current design.  We will also be changing this on our Interface inside of Interface Builder.  So for now, just believe me and anywhere it says UITextView, change it to UITextField.  The only additional code we added here is the method signature for the updateText method.  It&#8217;s an IBAction that will get called when the user presses the &#8220;Done&#8221; button on the keyboard after setting the text for the todo.  Next, we need to implement this method.  Open up TodoViewController.m and add the following code:</p>
<p style="text-align: center;"><a href="/wp-content/uploads/2008/09/5-todoviewcontrollerm1.png"><img class="size-full wp-image-426 aligncenter" title="5-todoviewcontrollerm" src="/wp-content/uploads/2008/09/5-todoviewcontrollerm1.png" alt="" width="245" height="53" /></a></p>
<p style="text-align: left;">All this does is update the text of the todo to the text that the user entered inside of the UITextField.  The last thing we need to do in order to add a todo is to replace the UITextView with a UITextField and connect it to our updateText method.  Double click on your TodoViewController.xib file to open it in Interface Builder.</p>
<p>Now click on the UITextView on your interface and press the delete key on your keyboard to delete it.  Now, drag a UITextField from the library and drop it onto your interface.  Resize it to fit.  When you have completed that, your interface should look something like this:</p>
<p style="text-align: center;"><a href="/wp-content/uploads/2008/09/6-interfacebuilder1.png"><img class="size-full wp-image-427 aligncenter" title="6-interfacebuilder" src="/wp-content/uploads/2008/09/6-interfacebuilder1.png" alt="" width="320" height="502" /></a></p>
<p style="text-align: left;">Now we need to connect this component.  Make sure it is selected and click Tools -&gt; Connections Inspector to open up the connections inspector.  Drag from the circle next to the method &#8220;Did End On Exit&#8221; to the &#8220;File&#8217;s Owner&#8221; object.  The words udpateText should pop up.  Click on them to make the connection.  Next, click in the circle next to &#8220;New Referencing Outlet&#8221; and drag it to the &#8220;File&#8217;s Owner&#8221; object.  Select todoText  when it pops up.  The Connections Inspector should look like this:</p>
<p style="text-align: center;"><a href="/wp-content/uploads/2008/09/12-ib1.png"><img class="size-full wp-image-434 aligncenter" title="12-ib" src="/wp-content/uploads/2008/09/12-ib1.png" alt="" width="287" height="708" /></a></p>
<p>Now we are done with Interface Builder.  Go ahead and close it.  We are now able to add todos.  The last thing we need to do is give the ability to delete todos from the list as well as our database.  This is all done in code, and we won&#8217;t need interface builder for this.</p>
<p>Let&#8217;s start by adding the methods to the appDelegate to handle the deletion of todos.  Open up todoAppDelegate.h and add the following code:</p>
<p style="text-align: center;"><a href="/wp-content/uploads/2008/09/7-appdelegateh1.png"><img class="alignnone size-full wp-image-428" title="7-appdelegateh" src="/wp-content/uploads/2008/09/7-appdelegateh1.png" alt="" width="194" height="16" /></a></p>
<p style="text-align: left;">All we see here is a signature for the removeTodo method.  Also, be sure to add a #import &#8220;Todo.h&#8221; statement to the top of this file so that we can interface with the todo objects. Now let&#8217;s implement the removeTodo method.  Open up todoAppDelegate.m and add the following code:</p>
<p style="text-align: center;"><a href="/wp-content/uploads/2008/09/8-appdelegatem1.png"><img class="size-full wp-image-429 aligncenter" title="8-appdelegatem" src="/wp-content/uploads/2008/09/8-appdelegatem1.png" alt="" width="308" height="119" /></a></p>
<p style="text-align: left;">The first line looks up the todo in the todos NSArray.  It returns the index in the array of the todo to be deleted.  Then, we call the deleteFromDatabase method on the todo object and then remove it from the todos array.  Since the UITableView is updated via this array, it will automatically remove the todo without any additional code on our part.</p>
<p>Now, let&#8217;s create the removeTodo method for the todo object. We have already written the method signature in Todo.h in a previous step, so open up Todo.m and add the following code:</p>
<p style="text-align: center;"><a href="/wp-content/uploads/2008/09/10-todom1.png"><img class="size-full wp-image-431 aligncenter" title="10-todom" src="/wp-content/uploads/2008/09/10-todom1.png" alt="" width="500" height="182" /></a></p>
<p style="text-align: left;">Remember the delete_statement variable is a static sqlite3_stmt that we declared in a previous step.  First, we check to see if it is nil.  If it is we compile the statement using the sqlite3_prepare statement.  Next, we bind the primary key of the current todo to the &#8220;?&#8221; in the sqlite3 statement.  Next, we just step the statement to execute it and reset it.  The last thing we need to do to delete todos from the database is to specify what happens when the user presses the &#8220;delete&#8221; button.  Open up RootViewController.m and add the following code:</p>
<p style="text-align: center;"><a href="/wp-content/uploads/2008/09/11-rootviewcontroller11.png"><img class="size-full wp-image-432 aligncenter" title="11-rootviewcontroller1" src="/wp-content/uploads/2008/09/11-rootviewcontroller11.png" alt="" width="499" height="143" /></a></p>
<p style="text-align: left;">
<p style="text-align: left;">The first step (like the first step of many functions) is to get a reference to the appDelegate.  Next, we check to see if we are currently editing.  If so, call the removeTodo method on appDelegate.  The next line, removes the row from the UITableView at the given indexPath.</p>
<p>Now click Build and Go!  You should now be able to add and delete todo items from the database.  This concludes our four part series of creating a todo list for the iPhone.  As always, if you have any questions feel free to leave them in the comments section.  If you get lost at any time you can download the sample code <a href="/wp-content/uploads/2008/09/todo-part-41.zip">here</a>.</p>
<p>Happy iCoding!</p>
<p style="text-align: left;">
]]></content:encoded>
			<wfw:commentRss>http://icodeblog.com/2008/09/22/iphone-programming-tutorial-creating-a-todo-list-using-sqlite-part-4/feed/</wfw:commentRss>
		<slash:comments>115</slash:comments>
		</item>
	</channel>
</rss>
