<?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>radikalFX &#187; iPhone</title>
	<atom:link href="http://radikalfx.com/tag/iphone/feed/" rel="self" type="application/rss+xml" />
	<link>http://radikalfx.com</link>
	<description>In full FX</description>
	<lastBuildDate>Tue, 27 Dec 2011 11:44:39 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3</generator>
		<item>
		<title>No more cookies, client side database on the iPhone</title>
		<link>http://radikalfx.com/2009/03/15/no-more-cookies-client-side-database-on-the-iphone/</link>
		<comments>http://radikalfx.com/2009/03/15/no-more-cookies-client-side-database-on-the-iphone/#comments</comments>
		<pubDate>Sun, 15 Mar 2009 11:29:24 +0000</pubDate>
		<dc:creator>cra5h</dc:creator>
				<category><![CDATA[Client side database]]></category>
		<category><![CDATA[HTML5]]></category>
		<category><![CDATA[iPhone]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[SQLite]]></category>

		<guid isPermaLink="false">http://radikalfx.com/?p=38</guid>
		<description><![CDATA[At the moment I am working on a small iPhone web application. The main goal of creating this application is to check out the capabilities of my favorite phone. While working on this demo application, I wanted to store favorite information for every user. Since I don&#8217;t want to set up server side databases for [...]]]></description>
			<content:encoded><![CDATA[<p>At the moment I am working on a small iPhone web application. The main goal of creating this application is to check out the capabilities of my favorite phone. While working on this demo application, I wanted to store favorite information for every user. Since I don&#8217;t want to set up server side databases for just a small test web application. My first thought was the always hated cookie. But before doing that I wanted to look into the possibilities of the HTML5 database support. I knew that this was implemented in the latest Safari release. But didn&#8217;t know if it was in Safari on the iPhone. But you know what, it is!</p>
<p>I won&#8217;t go into all the details, but I setup a very simple demo page here: <a href="http://radikalfx.com/files/db.html">http://radikalfx.com/files/db.html</a>. Visit that link with your iPhone or with Safari 4 to see how it works. The page lets you creating and dropping a table in your client side browser database. And you can insert, select and delete data in this table.</p>
<p><strong>So how does it work</strong><br />
The first thing to do, is create a connection to the database. This can be done with some code like this:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> setupDatabase<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000066; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>window.<span style="color: #660066;">openDatabase</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #003366; font-weight: bold;">var</span> shortName <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;testdb&quot;</span><span style="color: #339933;">;</span>
      <span style="color: #003366; font-weight: bold;">var</span> version <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;1.0&quot;</span><span style="color: #339933;">;</span>
      <span style="color: #003366; font-weight: bold;">var</span> displayName <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;Test Database&quot;</span><span style="color: #339933;">;</span>
      <span style="color: #003366; font-weight: bold;">var</span> maxSize <span style="color: #339933;">=</span> <span style="color: #CC0000;">65536</span><span style="color: #339933;">;</span>
      mydb <span style="color: #339933;">=</span> openDatabase<span style="color: #009900;">&#40;</span>shortName<span style="color: #339933;">,</span> version<span style="color: #339933;">,</span> displayName<span style="color: #339933;">,</span> maxSize<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      dbReady <span style="color: #339933;">=</span> mydb <span style="color: #339933;">!=</span> <span style="color: #003366; font-weight: bold;">null</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
      writeStatus<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;No JavaScript database support!&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">catch</span><span style="color: #009900;">&#40;</span>e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    dbReady <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">;</span>
    writeStatus<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Exception while setting up database: &quot;</span> <span style="color: #339933;">+</span> e.<span style="color: #660066;">message</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>The most important call in this code is the <code>mydb = openDatabase(shortName, version, displayName, maxSize);</code> which opens the database connection for you. </p>
<p>After this is done you can start transactions on this database in the following way:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">mydb.<span style="color: #660066;">transaction</span><span style="color: #009900;">&#40;</span> <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span>transaction<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  transaction.<span style="color: #660066;">executeSql</span><span style="color: #009900;">&#40;</span>query<span style="color: #339933;">,</span> data<span style="color: #339933;">,</span> dataHandler<span style="color: #339933;">,</span> errorHandler<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>The transaction requires a query, the data added to the query. And a data handler object which is called when the transaction succeeded and a error handler for handling errors in the transaction. </p>
<p>For inserting data I created the following very basic data handler in <a href="http://radikalfx.com/files/db.html">my sample code</a>:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> insertDataHandler<span style="color: #009900;">&#40;</span>transaction<span style="color: #339933;">,</span> results<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  writeStatus<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Insert row with id: &quot;</span> <span style="color: #339933;">+</span> results.<span style="color: #660066;">insertId</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Since this functionality is build with <a href="http://www.sqlite.org">SQLite</a>, you can use the <a href="http://www.sqlite.org/lang.html">documentation of SQLite</a> to figure out what you can do with your queries and how they should be build up.</p>
<p>Since the application I am developing is just for the iPhone, I can safely use this functionality. Of course for normal web development it&#8217;s kinda tricky since most browsers don&#8217;t support it yet.</p>
<p>For more information on this technology you can also visit the <a href="http://developer.apple.com/safari/library/documentation/iPhone/Conceptual/SafariJSDatabaseGuide/UsingtheJavascriptDatabase/chapter_5_section_1.html#//apple_ref/doc/uid/TP40007256-CH3-SW1">Safari Programming Guide</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://radikalfx.com/2009/03/15/no-more-cookies-client-side-database-on-the-iphone/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

