<?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>Databases &#8211; Wiebe Elsinga</title>
	<atom:link href="http://wiebe-elsinga.com/blog/tag/databases/feed/" rel="self" type="application/rss+xml" />
	<link>http://wiebe-elsinga.com/blog</link>
	<description>Blog</description>
	<lastBuildDate>Fri, 21 Oct 2011 04:15:21 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=5.8.1</generator>
	<item>
		<title>How-To: Android and Databases</title>
		<link>http://wiebe-elsinga.com/blog/android-and-databases/</link>
					<comments>http://wiebe-elsinga.com/blog/android-and-databases/#comments</comments>
		
		<dc:creator><![CDATA[W.Elsinga]]></dc:creator>
		<pubDate>Tue, 19 Oct 2010 04:52:03 +0000</pubDate>
				<category><![CDATA[Business]]></category>
		<category><![CDATA[How-To]]></category>
		<category><![CDATA[Android]]></category>
		<category><![CDATA[Databases]]></category>
		<category><![CDATA[DB4o]]></category>
		<category><![CDATA[SQLite]]></category>
		<category><![CDATA[Tutorial]]></category>
		<guid isPermaLink="false">http://wiebe-elsinga.com/blog/?p=415</guid>

					<description><![CDATA[Applications usually uses databases to store application data. Inside this article I will be explaining two implementation types: SQLite DB4o I will use the Android application which was developed inside my previous post. SQLite SQLite is a lightweight relational database engine. SQLite is fast and has a small footprint, making it perfect for Android devices. Instead of the heavyweight server-based databases like Oracle and Microsoft SQL Server, each SQLite database is stored within a single file on disk. So lets set-up an implementation. Create a DB Helper Class public DbHelper&#40;Context context&#41; &#123; &#160; &#160; &#160;super&#40;context, DATABASE_NAME, null, DATABASE_VERSION&#41;; &#125; @Override public void onCreate&#40;SQLiteDatabase sqliteDb&#41; &#123; &#160; &#160; &#160;String sql = &#34;CREATE TABLE &#34; + TABLE_NAME + &#34; (&#34; + COLOMN_NAME+ &#34; LONGTEXT&#34; + &#34;);&#34;; &#160; &#160; &#160;sqliteDb.execSQL&#40;sql&#41;; &#125; @Override public void onUpgrade&#40;SQLiteDatabase sqliteDb, int oldVersion, &#160; &#160; &#160; &#160; &#160; int newVersion&#41; &#123; &#160; &#160; &#160;Log.i&#40;DbHelper.class.getName&#40;&#41;, &#34;Upgrading database from version &#34; + oldVersion + &#34; to &#34; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160;+ newVersion + &#34;, which will destroy all old data&#34;&#41;; &#160; &#160; &#160;sqliteDb.execSQL&#40;&#34;DROP TABLE IF EXISTS &#34; + TABLE_NAME&#41;; &#160; &#160; &#160;onCreate&#40;sqliteDb&#41;; &#125; Create a Data layer class. This class will be responsible for using our DbHelper to perform our desired CRUD functions. public void addSearchResult&#40;String text&#41; &#123; &#160; &#160; &#160;SQLiteDatabase db = _dbHelper.getWritableDatabase&#40;&#41;; &#160; &#160; &#160;try &#123; &#160; &#160; &#160; &#160; &#160; ContentValues values = new ContentValues&#40;&#41;; &#160; &#160; &#160; &#160; &#160; values.put&#40;_dbHelper.COLOMN_NAME, text&#41;; &#160; &#160; &#160; &#160; &#160; db.insert&#40;_dbHelper.TABLE_NAME, &#34;&#34;, values&#41;; &#160; &#160; &#160;&#125; finally &#123; &#160; &#160; &#160; &#160; &#160; if &#40;db != null&#41; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160;db.close&#40;&#41;; &#160; &#160; &#160;&#125; &#125; public void clearHistory&#40;&#41; &#123; &#160; &#160; &#160;SQLiteDatabase db = _dbHelper.getWritableDatabase&#40;&#41;; &#160; &#160; &#160;try &#123; &#160; &#160; &#160; &#160; &#160; db.delete&#40;_dbHelper.TABLE_NAME, &#34;&#34;, null&#41;; &#160; &#160; &#160;&#125; finally &#123; &#160; &#160; &#160; &#160; &#160; if &#40;db != null&#41; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160;db.close&#40;&#41;; &#160; &#160; &#160;&#125; &#125; public List&#60;String&#62; getHistory&#40;&#41; &#123; &#160; &#160; &#160;SQLiteDatabase db = _dbHelper.getWritableDatabase&#40;&#41;; &#160; &#160; &#160;try &#123; &#160; &#160; &#160; &#160; &#160; List&#60;String&#62; results = new ArrayList&#60;String&#62;&#40;&#41;; &#160; &#160; &#160; &#160; &#160; Cursor c = db.rawQuery&#40;&#34;select * from &#34; + _dbHelper.TABLE_NAME, null&#41;; &#160; &#160; &#160; &#160; &#160; if &#40;c.getCount&#40;&#41; &#62; 0&#41; &#123; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160;c.moveToFirst&#40;&#41;; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160;do &#123; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; results.add&#40;c.getString&#40;c.getColumnIndex&#40;_dbHelper.COLOMN_NAME&#41;&#41;&#41;; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160;&#125; while &#40;c.moveToNext&#40;&#41;&#41;; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160;&#125; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160;return results; &#160; &#160; &#160;&#125; finally &#123; &#160; &#160; &#160; &#160; &#160; if &#40;db != null&#41; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160;db.close&#40;&#41;; &#160; &#160; &#160;&#125; &#125; Add CRUD functions to your Android application Db dB = new Db&#40;getBaseContext&#40;&#41;&#41;; dB.addSearchResult&#40;result&#41;; private void clearHistory&#40;&#41; &#123; &#160; &#160; &#160;Db dB = new Db&#40;getBaseContext&#40;&#41;&#41;; &#160; &#160; &#160;dB.clearHistory&#40;&#41;; &#125; private void displayHistory&#40;&#41; &#123; &#160; &#160; &#160;Db dB = new Db&#40;getBaseContext&#40;&#41;&#41;; &#160; &#160; &#160;List&#60;String&#62; history = dB.getHistory&#40;&#41;; &#160; &#160; &#160; &#160; &#160; &#160;ArrayAdapter&#60;String&#62; mHistory = new ArrayAdapter&#60;String&#62;&#40;this, R.layout.row, R.id.ENTRY_CELL, history&#41;; &#160; &#160; &#160; &#160; &#160; &#160;historyList.setAdapter&#40;mHistory&#41;; &#125; Download source code AndroidSQLLite.zip [20kB] DB4o db4o is an object database, so no mapping of tables in a relational model. You can persist complex objects with nested collections or other complex objects, any level of complexity in your hierarchy. You&#8217;re not limited to flat objects with primitive types to get a reasonable performance. So lets set-up an implementation. Create a persistent Object public class HistoryItem &#123; &#160; &#160; &#160; &#160; &#160; &#160;private String item; &#160; &#160; &#160; &#160; &#160; &#160;public HistoryItem&#40;&#41; &#123; &#160; &#160; &#160;&#125; &#160; &#160; &#160; &#160; &#160; &#160;public HistoryItem&#40;String item&#41; &#123; &#160; &#160; &#160; &#160; &#160; this.item= item; &#160; &#160; &#160;&#125; &#160; &#160; &#160;public void setItem&#40;String item&#41; &#123; &#160; &#160; &#160; &#160; &#160; this.item = item; &#160; &#160; &#160;&#125; &#160; &#160; &#160;public String getItem&#40;&#41; &#123; &#160; &#160; &#160; &#160; &#160; return item; &#160; &#160; &#160;&#125; &#125; Create a DB Helper Class public DbHelper&#40;Context context&#41; &#123; &#160; &#160; &#160;try &#123; &#160; &#160; &#160; &#160; &#160; if &#40;database == null &#124;&#124; database.ext&#40;&#41;.isClosed&#40;&#41;&#41; &#123; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160;database = Db4oEmbedded.openFile&#40;configure&#40;&#41;, db4oDBFullPath&#40;context&#41;&#41;; &#160; &#160; &#160; &#160; &#160; &#125; &#160; &#160; &#160;&#125; catch &#40;Exception ie&#41; &#123; &#160; &#160; &#160; &#160; &#160; Log.e&#40;DbHelper.class.getName&#40;&#41;, ie.toString&#40;&#41;&#41;; &#160; &#160; &#160;&#125; &#125; private static EmbeddedConfiguration configure&#40;&#41; &#123; &#160; &#160; &#160;EmbeddedConfiguration configuration = Db4oEmbedded.newConfiguration&#40;&#41;; &#160; &#160; &#160;configuration.common&#40;&#41;.objectClass&#40;HistoryItem.class&#41;.objectField&#40; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160;&#34;item&#34;&#41;.indexed&#40;true&#41;; &#160; &#160; &#160;configuration.common&#40;&#41;.objectClass&#40;HistoryItem.class&#41;.cascadeOnUpdate&#40;true&#41;; &#160; &#160; &#160;configuration.common&#40;&#41;.objectClass&#40;HistoryItem.class&#41;.cascadeOnDelete&#40;true&#41;; &#160; &#160; &#160;return configuration; &#125; private String db4oDBFullPath&#40;Context ctx&#41; &#123; &#160; &#160; &#160;return ctx.getDir&#40;&#34;data&#34;, DATABASE_MODE&#41; + &#34;/&#34; + DATABASE_NAME; &#125; public void close&#40;&#41; &#123; &#160; &#160; &#160;if &#40;this.database != null&#41; &#123; &#160; &#160; &#160; &#160; &#160; this.database.close&#40;&#41;; &#160; &#160; &#160;&#125; &#125; Create a Data layer class. public Db&#40;Context c&#41; &#123; &#160; &#160; &#160;if&#40;_dbHelper==null&#41; &#123; &#160; &#160; &#160; &#160; &#160; _dbHelper = new DbHelper&#40;c&#41;; &#160; &#160; &#160;&#125; &#125; public void addSearchResult&#40;String text&#41; &#123; &#160; &#160; &#160;ObjectContainer db = _dbHelper.getDatabase&#40;&#41;; &#160; &#160; &#160;try &#123; &#160; &#160; &#160; &#160; &#160; db.store&#40;new HistoryItem&#40;text&#41;&#41;; &#160; &#160; &#160; &#160; &#160; db.commit&#40;&#41;; &#160; &#160; &#160;&#125; finally &#123; &#160; &#160; &#160; &#160; &#160; if &#40;db != null&#41; &#123; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160;db.close&#40;&#41;; &#160; &#160; &#160; &#160; &#160; &#125; &#160; &#160; &#160;&#125; &#125; public void clearHistory&#40;&#41; &#123; &#160; &#160; &#160;ObjectContainer db = _dbHelper.getDatabase&#40;&#41;; &#160; &#160; &#160;try &#123; &#160; &#160; &#160; &#160; &#160; ObjectSet&#60;HistoryItem&#62; items = db.queryByExample&#40;new HistoryItem&#40;&#41;&#41;; &#160; &#160; &#160; &#160; &#160; for &#40;HistoryItem historyItem : items&#41; &#123; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160;db.delete&#40;historyItem&#41;; &#160; &#160; &#160; &#160; &#160; &#125; &#160; &#160; &#160; &#160; &#160; db.commit&#40;&#41;; &#160; &#160; &#160;&#125; catch &#40;Exception e&#41; &#123; &#160; &#160; &#160; &#160; &#160; Log.e&#40;Db.class.getName&#40;&#41;, e.toString&#40;&#41;&#41;; &#160; &#160; &#160;&#125; finally &#123; &#160; &#160; &#160; &#160; &#160; if &#40;db != null&#41; &#123; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160;db.close&#40;&#41;; &#160; &#160; &#160; &#160; &#160; &#125; &#160; &#160; &#160;&#125; &#125; public List&#60;String&#62; getHistory&#40;&#41; &#123; &#160; &#160; &#160;ObjectContainer db = _dbHelper.getDatabase&#40;&#41;; &#160; &#160; &#160;try &#123; &#160; &#160; &#160; &#160; &#160; List&#60;String&#62; results = new ArrayList&#60;String&#62;&#40;&#41;; &#160; &#160; &#160; [&#8230;]]]></description>
										<content:encoded><![CDATA[<p><a href="http://wiebe-elsinga.com/blog/wp-content/uploads/2010/05/android.jpg" rel="lightbox[415]" title="android"><img loading="lazy" class="alignleft size-thumbnail wp-image-170" style="padding-right: 10px;" title="android" src="http://wiebe-elsinga.com/blog/wp-content/uploads/2010/05/android-150x150.jpg" alt="" width="150" height="150" srcset="http://wiebe-elsinga.com/blog/wp-content/uploads/2010/05/android-150x150.jpg 150w, http://wiebe-elsinga.com/blog/wp-content/uploads/2010/05/android-300x300.jpg 300w, http://wiebe-elsinga.com/blog/wp-content/uploads/2010/05/android-200x200-cropped.jpg 200w, http://wiebe-elsinga.com/blog/wp-content/uploads/2010/05/android.jpg 500w" sizes="(max-width: 150px) 100vw, 150px" /></a></p>
<p>Applications usually uses databases to store application data. Inside this article I will be explaining two implementation types:</p>
<ul>
<li>SQLite</li>
<li>DB4o</li>
</ul>
<p>I will use the Android application which was developed inside my <a href="http://wiebe-elsinga.com/blog/?p=275">previous post</a>.<br />
<span id="more-415"></span></p>
<h2>SQLite</h2>
<p>SQLite is a lightweight relational database engine. SQLite is fast and has a small footprint, making it perfect for Android devices. Instead of the heavyweight server-based databases like Oracle and Microsoft SQL Server, each SQLite database is stored within a single file on disk. So lets set-up an implementation.</p>
<ol>
<li>Create a DB Helper Class</li>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;"><span style="color: #000000; font-weight: bold;">public</span> DbHelper<span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Adocs.oracle.com+javase+docs+api+context"><span style="color: #003399;">Context</span></a> context<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #000000; font-weight: bold;">super</span><span style="color: #009900;">&#40;</span>context, DATABASE_NAME, <span style="color: #000066; font-weight: bold;">null</span>, DATABASE_VERSION<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
@Override<br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> onCreate<span style="color: #009900;">&#40;</span>SQLiteDatabase sqliteDb<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp;<a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Adocs.oracle.com+javase+docs+api+string"><span style="color: #003399;">String</span></a> sql <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;CREATE TABLE &quot;</span> <span style="color: #339933;">+</span> TABLE_NAME <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot; (&quot;</span> <span style="color: #339933;">+</span> COLOMN_NAME<span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot; LONGTEXT&quot;</span> <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;);&quot;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp;sqliteDb.<span style="color: #006633;">execSQL</span><span style="color: #009900;">&#40;</span>sql<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
@Override<br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> onUpgrade<span style="color: #009900;">&#40;</span>SQLiteDatabase sqliteDb, <span style="color: #000066; font-weight: bold;">int</span> oldVersion,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">int</span> newVersion<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp;Log.<span style="color: #006633;">i</span><span style="color: #009900;">&#40;</span>DbHelper.<span style="color: #000000; font-weight: bold;">class</span>.<span style="color: #006633;">getName</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>, <span style="color: #0000ff;">&quot;Upgrading database from version &quot;</span> <span style="color: #339933;">+</span> oldVersion <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot; to &quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #339933;">+</span> newVersion <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;, which will destroy all old data&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp;sqliteDb.<span style="color: #006633;">execSQL</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;DROP TABLE IF EXISTS &quot;</span> <span style="color: #339933;">+</span> TABLE_NAME<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp;onCreate<span style="color: #009900;">&#40;</span>sqliteDb<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<li> Create a Data layer class. This class will be responsible for using our <i>DbHelper</i> to perform our desired CRUD functions.</li>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:100%;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> addSearchResult<span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Adocs.oracle.com+javase+docs+api+string"><span style="color: #003399;">String</span></a> text<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp;SQLiteDatabase db <span style="color: #339933;">=</span> _dbHelper.<span style="color: #006633;">getWritableDatabase</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ContentValues values <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ContentValues<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; values.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span>_dbHelper.<span style="color: #006633;">COLOMN_NAME</span>, text<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; db.<span style="color: #006633;">insert</span><span style="color: #009900;">&#40;</span>_dbHelper.<span style="color: #006633;">TABLE_NAME</span>, <span style="color: #0000ff;">&quot;&quot;</span>, values<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">finally</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>db <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;db.<span style="color: #006633;">close</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> clearHistory<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp;SQLiteDatabase db <span style="color: #339933;">=</span> _dbHelper.<span style="color: #006633;">getWritableDatabase</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; db.<span style="color: #006633;">delete</span><span style="color: #009900;">&#40;</span>_dbHelper.<span style="color: #006633;">TABLE_NAME</span>, <span style="color: #0000ff;">&quot;&quot;</span>, <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">finally</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>db <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;db.<span style="color: #006633;">close</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">public</span> List<span style="color: #339933;">&lt;</span>String<span style="color: #339933;">&gt;</span> getHistory<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp;SQLiteDatabase db <span style="color: #339933;">=</span> _dbHelper.<span style="color: #006633;">getWritableDatabase</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; List<span style="color: #339933;">&lt;</span>String<span style="color: #339933;">&gt;</span> results <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ArrayList<span style="color: #339933;">&lt;</span>String<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Adocs.oracle.com+javase+docs+api+cursor"><span style="color: #003399;">Cursor</span></a> c <span style="color: #339933;">=</span> db.<span style="color: #006633;">rawQuery</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;select * from &quot;</span> <span style="color: #339933;">+</span> _dbHelper.<span style="color: #006633;">TABLE_NAME</span>, <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>c.<span style="color: #006633;">getCount</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&gt;</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;c.<span style="color: #006633;">moveToFirst</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000000; font-weight: bold;">do</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; results.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span>c.<span style="color: #006633;">getString</span><span style="color: #009900;">&#40;</span>c.<span style="color: #006633;">getColumnIndex</span><span style="color: #009900;">&#40;</span>_dbHelper.<span style="color: #006633;">COLOMN_NAME</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">while</span> <span style="color: #009900;">&#40;</span>c.<span style="color: #006633;">moveToNext</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000000; font-weight: bold;">return</span> results<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">finally</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>db <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;db.<span style="color: #006633;">close</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<li>Add CRUD functions to your Android application</li>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;">Db dB <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Db<span style="color: #009900;">&#40;</span>getBaseContext<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
dB.<span style="color: #006633;">addSearchResult</span><span style="color: #009900;">&#40;</span>result<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">void</span> clearHistory<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp;Db dB <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Db<span style="color: #009900;">&#40;</span>getBaseContext<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp;dB.<span style="color: #006633;">clearHistory</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">void</span> displayHistory<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp;Db dB <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Db<span style="color: #009900;">&#40;</span>getBaseContext<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp;List<span style="color: #339933;">&lt;</span>String<span style="color: #339933;">&gt;</span> history <span style="color: #339933;">=</span> dB.<span style="color: #006633;">getHistory</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;ArrayAdapter<span style="color: #339933;">&lt;</span>String<span style="color: #339933;">&gt;</span> mHistory <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ArrayAdapter<span style="color: #339933;">&lt;</span>String<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span>, R.<span style="color: #006633;">layout</span>.<span style="color: #006633;">row</span>, R.<span style="color: #006633;">id</span>.<span style="color: #006633;">ENTRY_CELL</span>, history<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;historyList.<span style="color: #006633;">setAdapter</span><span style="color: #009900;">&#40;</span>mHistory<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #009900;">&#125;</span></div></div>
</ol>
<p>Download source code <a href='http://wiebe-elsinga.com/blog/wp-content/uploads/2010/10/AndroidSQLLite.zip'>AndroidSQLLite.zip</a> [20kB]</p>
<h2>DB4o</h2>
<p>db4o is an object database, so no mapping of tables in a relational model. You can persist complex objects with nested collections or other complex objects, any level of complexity in your hierarchy. You&#8217;re not limited to flat objects with primitive types to get a reasonable performance. So lets set-up an implementation.</p>
<ol>
<li>Create a persistent Object</li>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> HistoryItem <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<span style="color: #000000; font-weight: bold;">private</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Adocs.oracle.com+javase+docs+api+string"><span style="color: #003399;">String</span></a> item<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<span style="color: #000000; font-weight: bold;">public</span> HistoryItem<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<span style="color: #000000; font-weight: bold;">public</span> HistoryItem<span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Adocs.oracle.com+javase+docs+api+string"><span style="color: #003399;">String</span></a> item<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">item</span><span style="color: #339933;">=</span> item<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; &nbsp;<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setItem<span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Adocs.oracle.com+javase+docs+api+string"><span style="color: #003399;">String</span></a> item<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">item</span> <span style="color: #339933;">=</span> item<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; &nbsp;<span style="color: #000000; font-weight: bold;">public</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Adocs.oracle.com+javase+docs+api+string"><span style="color: #003399;">String</span></a> getItem<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> item<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<li>Create a DB Helper Class</li>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:100%;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;"><span style="color: #000000; font-weight: bold;">public</span> DbHelper<span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Adocs.oracle.com+javase+docs+api+context"><span style="color: #003399;">Context</span></a> context<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>database <span style="color: #339933;">==</span> <span style="color: #000066; font-weight: bold;">null</span> <span style="color: #339933;">||</span> database.<span style="color: #006633;">ext</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">isClosed</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;database <span style="color: #339933;">=</span> Db4oEmbedded.<span style="color: #006633;">openFile</span><span style="color: #009900;">&#40;</span>configure<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>, db4oDBFullPath<span style="color: #009900;">&#40;</span>context<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Adocs.oracle.com+javase+docs+api+exception"><span style="color: #003399;">Exception</span></a> ie<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Log.<span style="color: #006633;">e</span><span style="color: #009900;">&#40;</span>DbHelper.<span style="color: #000000; font-weight: bold;">class</span>.<span style="color: #006633;">getName</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>, ie.<span style="color: #006633;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> EmbeddedConfiguration configure<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp;EmbeddedConfiguration configuration <span style="color: #339933;">=</span> Db4oEmbedded.<span style="color: #006633;">newConfiguration</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp;configuration.<span style="color: #006633;">common</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">objectClass</span><span style="color: #009900;">&#40;</span>HistoryItem.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">objectField</span><span style="color: #009900;">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #0000ff;">&quot;item&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">indexed</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp;configuration.<span style="color: #006633;">common</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">objectClass</span><span style="color: #009900;">&#40;</span>HistoryItem.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">cascadeOnUpdate</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp;configuration.<span style="color: #006633;">common</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">objectClass</span><span style="color: #009900;">&#40;</span>HistoryItem.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">cascadeOnDelete</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #000000; font-weight: bold;">return</span> configuration<span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">private</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Adocs.oracle.com+javase+docs+api+string"><span style="color: #003399;">String</span></a> db4oDBFullPath<span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Adocs.oracle.com+javase+docs+api+context"><span style="color: #003399;">Context</span></a> ctx<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #000000; font-weight: bold;">return</span> ctx.<span style="color: #006633;">getDir</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;data&quot;</span>, DATABASE_MODE<span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;/&quot;</span> <span style="color: #339933;">+</span> DATABASE_NAME<span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> close<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">database</span> <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">database</span>.<span style="color: #006633;">close</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<li> Create a Data layer class.</li>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:100%;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;"><span style="color: #000000; font-weight: bold;">public</span> Db<span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Adocs.oracle.com+javase+docs+api+context"><span style="color: #003399;">Context</span></a> c<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>_dbHelper<span style="color: #339933;">==</span><span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; _dbHelper <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> DbHelper<span style="color: #009900;">&#40;</span>c<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> addSearchResult<span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Adocs.oracle.com+javase+docs+api+string"><span style="color: #003399;">String</span></a> text<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp;ObjectContainer db <span style="color: #339933;">=</span> _dbHelper.<span style="color: #006633;">getDatabase</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; db.<span style="color: #006633;">store</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> HistoryItem<span style="color: #009900;">&#40;</span>text<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; db.<span style="color: #006633;">commit</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">finally</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>db <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;db.<span style="color: #006633;">close</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> clearHistory<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp;ObjectContainer db <span style="color: #339933;">=</span> _dbHelper.<span style="color: #006633;">getDatabase</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ObjectSet<span style="color: #339933;">&lt;</span>HistoryItem<span style="color: #339933;">&gt;</span> items <span style="color: #339933;">=</span> db.<span style="color: #006633;">queryByExample</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> HistoryItem<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span>HistoryItem historyItem <span style="color: #339933;">:</span> items<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;db.<span style="color: #006633;">delete</span><span style="color: #009900;">&#40;</span>historyItem<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; db.<span style="color: #006633;">commit</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Adocs.oracle.com+javase+docs+api+exception"><span style="color: #003399;">Exception</span></a> e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Log.<span style="color: #006633;">e</span><span style="color: #009900;">&#40;</span>Db.<span style="color: #000000; font-weight: bold;">class</span>.<span style="color: #006633;">getName</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>, e.<span style="color: #006633;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">finally</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>db <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;db.<span style="color: #006633;">close</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">public</span> List<span style="color: #339933;">&lt;</span>String<span style="color: #339933;">&gt;</span> getHistory<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp;ObjectContainer db <span style="color: #339933;">=</span> _dbHelper.<span style="color: #006633;">getDatabase</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; List<span style="color: #339933;">&lt;</span>String<span style="color: #339933;">&gt;</span> results <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ArrayList<span style="color: #339933;">&lt;</span>String<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ObjectSet<span style="color: #339933;">&lt;</span>HistoryItem<span style="color: #339933;">&gt;</span> items <span style="color: #339933;">=</span> db.<span style="color: #006633;">queryByExample</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> HistoryItem<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span>HistoryItem historyItem <span style="color: #339933;">:</span> items<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;results.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span>historyItem.<span style="color: #006633;">getItem</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> results<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">finally</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>db <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;db.<span style="color: #006633;">close</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<li>Add CRUD functions to your Android application</li>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;">Db dB <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Db<span style="color: #009900;">&#40;</span>getBaseContext<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
dB.<span style="color: #006633;">addSearchResult</span><span style="color: #009900;">&#40;</span>result<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">void</span> clearHistory<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp;Db dB <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Db<span style="color: #009900;">&#40;</span>getBaseContext<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp;dB.<span style="color: #006633;">clearHistory</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">void</span> displayHistory<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp;Db dB <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Db<span style="color: #009900;">&#40;</span>getBaseContext<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp;List<span style="color: #339933;">&lt;</span>String<span style="color: #339933;">&gt;</span> history <span style="color: #339933;">=</span> dB.<span style="color: #006633;">getHistory</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;ArrayAdapter<span style="color: #339933;">&lt;</span>String<span style="color: #339933;">&gt;</span> mHistory <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ArrayAdapter<span style="color: #339933;">&lt;</span>String<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span>, R.<span style="color: #006633;">layout</span>.<span style="color: #006633;">row</span>, R.<span style="color: #006633;">id</span>.<span style="color: #006633;">ENTRY_CELL</span>, history<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;historyList.<span style="color: #006633;">setAdapter</span><span style="color: #009900;">&#40;</span>mHistory<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #009900;">&#125;</span></div></div>
</ol>
<p>Download source code <a href='http://wiebe-elsinga.com/blog/wp-content/uploads/2010/10/AndroidDb4o.zip'>AndroidDb4o</a> [2131kB]</p>
]]></content:encoded>
					
					<wfw:commentRss>http://wiebe-elsinga.com/blog/android-and-databases/feed/</wfw:commentRss>
			<slash:comments>6</slash:comments>
		
		
			</item>
	</channel>
</rss>
