<?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>sqlite Archives - CodeJourney.net</title>
	<atom:link href="https://www.codejourney.net/tag/sqlite/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.codejourney.net/tag/sqlite/</link>
	<description>Become a better .NET full stack web developer</description>
	<lastBuildDate>Sat, 17 Nov 2018 13:11:02 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9</generator>

<image>
	<url>https://i0.wp.com/www.codejourney.net/wp-content/uploads/2018/10/cropped-512px-na-512px-JPEG-BEZ-NAPISU-1.jpg?fit=32%2C32&#038;ssl=1</url>
	<title>sqlite Archives - CodeJourney.net</title>
	<link>https://www.codejourney.net/tag/sqlite/</link>
	<width>32</width>
	<height>32</height>
</image> 
<site xmlns="com-wordpress:feed-additions:1">123174533</site>	<item>
		<title>Using SQLite database in Xamarin.Android</title>
		<link>https://www.codejourney.net/using-sqlite-database-in-xamarin-android/</link>
					<comments>https://www.codejourney.net/using-sqlite-database-in-xamarin-android/#comments</comments>
		
		<dc:creator><![CDATA[Dawid Sibiński]]></dc:creator>
		<pubDate>Sun, 19 Mar 2017 22:32:29 +0000</pubDate>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[android]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[dajsiepoznac2017]]></category>
		<category><![CDATA[sqlite]]></category>
		<category><![CDATA[xamarin]]></category>
		<guid isPermaLink="false">http://www.dsibinski.pl/?p=970</guid>

					<description><![CDATA[<p>I started to define development tasks to be done within MoneyBack project in the coming days (maybe I&#8217;ll even publish my Trello board soon 🙂 ) and it turned out that each functionality the app is going to offer needs persistent storage (e.g. to store people, payments, events etc.). The obvious choice is the local database in which&#8230;</p>
<p>The post <a href="https://www.codejourney.net/using-sqlite-database-in-xamarin-android/">Using SQLite database in Xamarin.Android</a> appeared first on <a href="https://www.codejourney.net">CodeJourney.net</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>I started to define development tasks to be done within <em>MoneyBack</em> project in the coming days (maybe I&#8217;ll even publish my Trello board soon <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f642.png" alt="🙂" class="wp-smiley" style="height: 1em; max-height: 1em;" /> ) and it turned out that each functionality the app is going to offer needs persistent storage (e.g. to store people, payments, events etc.). The obvious choice is the local database in which I could store my entities and application&#8217;s data.</p>
<p>In this post, I&#8217;d like to show you how quickly and easily SQLite database can be added and started to be used in Xamarin.Android project using <a href="https://www.nuget.org/packages/sqlite-net/" target="_blank" rel="noopener">SQLite.NET</a> and Visual Studio 2017.<br />
<span id="more-970"></span></p>
<h2>What is SQLite?</h2>
<p><a href="http://sqlite.org/" target="_blank" rel="noopener">SQLite</a> is an open-source database engine, which allows to store data in SQL structures. The whole database is stored in a single file, which makes it easily manageable on mobile devices. It&#8217;s lightweight, small and easily portable. It&#8217;s also prepared to work on multiple platforms.</p>
<p>There are some limitations in contrast to &#8220;classic&#8221; SQL database engines, including:</p>
<ul>
<li>OUTER join syntax is not fully supported</li>
<li>for altering tables, only &#8220;RENAME&#8221; and &#8220;ADDCOLUMN&#8221; operations are available</li>
<li>writing to views is impossible (views are read-only).</li>
</ul>
<p>SQLite database can be easily operated using SQLite.NET ORM library available to download and install via Nuget. Let&#8217;s see how to do it.</p>
<h2>Installing SQLite.NET package</h2>
<p>In order to install SQLite.NET package, simply open Android.Xamarin solution in Visual Studio, open Package Manager Console and type the following command:</p>
<pre><pre class="brush: csharp; title: ; notranslate">install-package Sqlite-Net</pre>
<p>After few seconds, two files are added to our project:</p>
<figure id="attachment_1007" aria-describedby="caption-attachment-1007" style="width: 185px" class="wp-caption aligncenter"><img data-recalc-dims="1" decoding="async" data-attachment-id="1007" data-permalink="https://www.codejourney.net/using-sqlite-database-in-xamarin-android/sqlitenetfiles/" data-orig-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2017/03/SQLiteNetFiles.png?fit=315%2C420&amp;ssl=1" data-orig-size="315,420" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="SQLiteNetFiles" data-image-description="" data-image-caption="" data-medium-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2017/03/SQLiteNetFiles.png?fit=225%2C300&amp;ssl=1" data-large-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2017/03/SQLiteNetFiles.png?fit=315%2C420&amp;ssl=1" class=" wp-image-1007 aligncenter" src="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2017/03/SQLiteNetFiles.png?resize=185%2C247&#038;ssl=1" alt="SQLiteNetFiles" width="185" height="247" srcset="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2017/03/SQLiteNetFiles.png?w=315&amp;ssl=1 315w, https://i0.wp.com/www.codejourney.net/wp-content/uploads/2017/03/SQLiteNetFiles.png?resize=225%2C300&amp;ssl=1 225w" sizes="(max-width: 185px) 100vw, 185px" /><figcaption id="caption-attachment-1007" class="wp-caption-text">Files added by SQLite.NET</figcaption></figure>
<p>It means we&#8217;re all set <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f642.png" alt="🙂" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>
<h2>Entity definition</h2>
<p>We have SQLite ORM ready to use. Now we need to define our first entity. In case of <em>MoneyBack</em>, I decided to start with defining <em>Person </em>(plural: <em>People</em>) entity. First thing we need to do is to define entity class. I created &#8220;Entities&#8221; folder for that and added the following class file:</p>
<p><style>.gist table { margin-bottom: 0; }</style>
<div style="tab-size: 8" id="gist45864679" class="gist">
<div class="gist-file" translate="no" data-color-mode="light" data-light-theme="light">
<div class="gist-data">
<div class="js-gist-file-update-container js-task-list-container">
<div id="file-person-cs" class="file my-2">
<div itemprop="text"
      class="Box-body p-0 blob-wrapper data type-c  "
      style="overflow: auto" tabindex="0" role="region"
      aria-label="Person.cs content, created by dsibinski on 09:06PM on March 19, 2017."
    ></p>
<div class="js-check-hidden-unicode js-blob-code-container blob-code-content">
<p>  <template class="js-file-alert-template"></p>
<div data-view-component="true" class="flash flash-warn flash-full d-flex flex-items-center">
  <svg aria-hidden="true" height="16" viewBox="0 0 16 16" version="1.1" width="16" data-view-component="true" class="octicon octicon-alert">
    <path d="M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z"></path>
</svg><br />
    <span><br />
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.<br />
      <a class="Link--inTextBlock" href="https://github.co/hiddenchars" target="_blank">Learn more about bidirectional Unicode characters</a><br />
    </span></p>
<div data-view-component="true" class="flash-action">        <a href="{{ revealButtonHref }}" data-view-component="true" class="btn-sm btn">    Show hidden characters<br />
</a>
</div>
</div>
<p></template><br />
<template class="js-line-alert-template"><br />
  <span aria-label="This line has hidden Unicode characters" data-view-component="true" class="line-alert tooltipped tooltipped-e"><br />
    <svg aria-hidden="true" height="16" viewBox="0 0 16 16" version="1.1" width="16" data-view-component="true" class="octicon octicon-alert">
    <path d="M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z"></path>
</svg><br />
</span></template></p>
<table data-hpc class="highlight tab-size js-file-line-container" data-tab-size="4" data-paste-markdown-skip data-tagsearch-path="Person.cs">
<tr>
<td id="file-person-cs-L1" class="blob-num js-line-number js-blob-rnum" data-line-number="1"></td>
<td id="file-person-cs-LC1" class="blob-code blob-code-inner js-file-line">namespace MoneyBack.Entities</td>
</tr>
<tr>
<td id="file-person-cs-L2" class="blob-num js-line-number js-blob-rnum" data-line-number="2"></td>
<td id="file-person-cs-LC2" class="blob-code blob-code-inner js-file-line">{</td>
</tr>
<tr>
<td id="file-person-cs-L3" class="blob-num js-line-number js-blob-rnum" data-line-number="3"></td>
<td id="file-person-cs-LC3" class="blob-code blob-code-inner js-file-line">    [Table(&quot;People&quot;)]</td>
</tr>
<tr>
<td id="file-person-cs-L4" class="blob-num js-line-number js-blob-rnum" data-line-number="4"></td>
<td id="file-person-cs-LC4" class="blob-code blob-code-inner js-file-line">    public class Person</td>
</tr>
<tr>
<td id="file-person-cs-L5" class="blob-num js-line-number js-blob-rnum" data-line-number="5"></td>
<td id="file-person-cs-LC5" class="blob-code blob-code-inner js-file-line">    {</td>
</tr>
<tr>
<td id="file-person-cs-L6" class="blob-num js-line-number js-blob-rnum" data-line-number="6"></td>
<td id="file-person-cs-LC6" class="blob-code blob-code-inner js-file-line">        [PrimaryKey, AutoIncrement]</td>
</tr>
<tr>
<td id="file-person-cs-L7" class="blob-num js-line-number js-blob-rnum" data-line-number="7"></td>
<td id="file-person-cs-LC7" class="blob-code blob-code-inner js-file-line">        public int Id { get; set; }</td>
</tr>
<tr>
<td id="file-person-cs-L8" class="blob-num js-line-number js-blob-rnum" data-line-number="8"></td>
<td id="file-person-cs-LC8" class="blob-code blob-code-inner js-file-line">
</td>
</tr>
<tr>
<td id="file-person-cs-L9" class="blob-num js-line-number js-blob-rnum" data-line-number="9"></td>
<td id="file-person-cs-LC9" class="blob-code blob-code-inner js-file-line">        public string Name { get; set; }</td>
</tr>
<tr>
<td id="file-person-cs-L10" class="blob-num js-line-number js-blob-rnum" data-line-number="10"></td>
<td id="file-person-cs-LC10" class="blob-code blob-code-inner js-file-line">
</td>
</tr>
<tr>
<td id="file-person-cs-L11" class="blob-num js-line-number js-blob-rnum" data-line-number="11"></td>
<td id="file-person-cs-LC11" class="blob-code blob-code-inner js-file-line">        public string LastName { get; set; }</td>
</tr>
<tr>
<td id="file-person-cs-L12" class="blob-num js-line-number js-blob-rnum" data-line-number="12"></td>
<td id="file-person-cs-LC12" class="blob-code blob-code-inner js-file-line">
</td>
</tr>
<tr>
<td id="file-person-cs-L13" class="blob-num js-line-number js-blob-rnum" data-line-number="13"></td>
<td id="file-person-cs-LC13" class="blob-code blob-code-inner js-file-line">        public string PhoneNumber { get; set; }</td>
</tr>
<tr>
<td id="file-person-cs-L14" class="blob-num js-line-number js-blob-rnum" data-line-number="14"></td>
<td id="file-person-cs-LC14" class="blob-code blob-code-inner js-file-line">
</td>
</tr>
<tr>
<td id="file-person-cs-L15" class="blob-num js-line-number js-blob-rnum" data-line-number="15"></td>
<td id="file-person-cs-LC15" class="blob-code blob-code-inner js-file-line">        public string Email { get; set; }</td>
</tr>
<tr>
<td id="file-person-cs-L16" class="blob-num js-line-number js-blob-rnum" data-line-number="16"></td>
<td id="file-person-cs-LC16" class="blob-code blob-code-inner js-file-line">
</td>
</tr>
<tr>
<td id="file-person-cs-L17" class="blob-num js-line-number js-blob-rnum" data-line-number="17"></td>
<td id="file-person-cs-LC17" class="blob-code blob-code-inner js-file-line">        public override string ToString()</td>
</tr>
<tr>
<td id="file-person-cs-L18" class="blob-num js-line-number js-blob-rnum" data-line-number="18"></td>
<td id="file-person-cs-LC18" class="blob-code blob-code-inner js-file-line">        {</td>
</tr>
<tr>
<td id="file-person-cs-L19" class="blob-num js-line-number js-blob-rnum" data-line-number="19"></td>
<td id="file-person-cs-LC19" class="blob-code blob-code-inner js-file-line">            return $&quot;[Person: Id={Id}, Name={Name}, LastName={LastName}, PhoneNumber={PhoneNumber}, Email={Email}]&quot;;</td>
</tr>
<tr>
<td id="file-person-cs-L20" class="blob-num js-line-number js-blob-rnum" data-line-number="20"></td>
<td id="file-person-cs-LC20" class="blob-code blob-code-inner js-file-line">        }</td>
</tr>
<tr>
<td id="file-person-cs-L21" class="blob-num js-line-number js-blob-rnum" data-line-number="21"></td>
<td id="file-person-cs-LC21" class="blob-code blob-code-inner js-file-line">    }</td>
</tr>
<tr>
<td id="file-person-cs-L22" class="blob-num js-line-number js-blob-rnum" data-line-number="22"></td>
<td id="file-person-cs-LC22" class="blob-code blob-code-inner js-file-line">}</td>
</tr>
</table>
</div></div>
</p></div>
</div></div>
<div class="gist-meta">
        <a href="https://gist.github.com/dsibinski/a1d1ca6c5380e0acc5c439f3060da9cc/raw/afc1d9b4c0442489fdb760ba88e681d3663b9e07/Person.cs" style="float:right" class="Link--inTextBlock">view raw</a><br />
        <a href="https://gist.github.com/dsibinski/a1d1ca6c5380e0acc5c439f3060da9cc#file-person-cs" class="Link--inTextBlock"><br />
          Person.cs<br />
        </a><br />
        hosted with &#10084; by <a class="Link--inTextBlock" href="https://github.com">GitHub</a>
      </div>
</p></div>
</div>
<p>Just a class, in which we define basic properties of a Person &#8211;<em> Name, LastName, PhoneNumber</em> and <em>Email</em>. As we are designing a database entities, we also need to have keys for each of them &#8211; in this case we add <em>Id</em> property, and decorate it with two attributes:</p>
<ul>
<li><em>PrimaryKeyAttribute</em> &#8211; marks the column as PRIMARY KEY</li>
<li><em>AutoIncrementAttribute</em> &#8211; marks the column with AUTO_INCREMENT (unique number is generated when a new record is added into a table).</li>
</ul>
<p>We also have <em>TableAttribute</em> defined on <em>Person</em> class. This attribute&#8217;s constructor takes a single argument <em>name</em>, which is the name of the table which will be created underneath. In our case, single entity is called <em>Person</em>, but the table should be named in plural (table == set of entities) so we want it to be called <em>People</em>.</p>
<p>I also implemented <em>ToString() </em>method in order to be able to easily see details of our entity if needed.</p>
<h2>Location of database file</h2>
<p>Before database can be used, a database file must be created on the device. ORM does it for us before any query is made on the database, but we need to specify where we want to keep the db file. On Android, it can be stored either on internal or external storage.</p>
<p>I created <em>Constants.cs</em> class in which I put a read-only constant string returning path to database file. As a folder to store db file, I used <em>Environment.GetFolderPath(Environment.SpecialFolder.Personal) </em>which returns local path defined in Android system&#8217;s environmental variable $HOME:</p>
<p><style>.gist table { margin-bottom: 0; }</style>
<div style="tab-size: 8" id="gist45865151" class="gist">
<div class="gist-file" translate="no" data-color-mode="light" data-light-theme="light">
<div class="gist-data">
<div class="js-gist-file-update-container js-task-list-container">
<div id="file-constants_dbpath-cs" class="file my-2">
<div itemprop="text"
      class="Box-body p-0 blob-wrapper data type-c  "
      style="overflow: auto" tabindex="0" role="region"
      aria-label="Constants_dbPath.cs content, created by dsibinski on 09:30PM on March 19, 2017."
    ></p>
<div class="js-check-hidden-unicode js-blob-code-container blob-code-content">
<p>  <template class="js-file-alert-template"></p>
<div data-view-component="true" class="flash flash-warn flash-full d-flex flex-items-center">
  <svg aria-hidden="true" height="16" viewBox="0 0 16 16" version="1.1" width="16" data-view-component="true" class="octicon octicon-alert">
    <path d="M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z"></path>
</svg><br />
    <span><br />
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.<br />
      <a class="Link--inTextBlock" href="https://github.co/hiddenchars" target="_blank">Learn more about bidirectional Unicode characters</a><br />
    </span></p>
<div data-view-component="true" class="flash-action">        <a href="{{ revealButtonHref }}" data-view-component="true" class="btn-sm btn">    Show hidden characters<br />
</a>
</div>
</div>
<p></template><br />
<template class="js-line-alert-template"><br />
  <span aria-label="This line has hidden Unicode characters" data-view-component="true" class="line-alert tooltipped tooltipped-e"><br />
    <svg aria-hidden="true" height="16" viewBox="0 0 16 16" version="1.1" width="16" data-view-component="true" class="octicon octicon-alert">
    <path d="M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z"></path>
</svg><br />
</span></template></p>
<table data-hpc class="highlight tab-size js-file-line-container" data-tab-size="4" data-paste-markdown-skip data-tagsearch-path="Constants_dbPath.cs">
<tr>
<td id="file-constants_dbpath-cs-L1" class="blob-num js-line-number js-blob-rnum" data-line-number="1"></td>
<td id="file-constants_dbpath-cs-LC1" class="blob-code blob-code-inner js-file-line">public class Constants</td>
</tr>
<tr>
<td id="file-constants_dbpath-cs-L2" class="blob-num js-line-number js-blob-rnum" data-line-number="2"></td>
<td id="file-constants_dbpath-cs-LC2" class="blob-code blob-code-inner js-file-line">{</td>
</tr>
<tr>
<td id="file-constants_dbpath-cs-L3" class="blob-num js-line-number js-blob-rnum" data-line-number="3"></td>
<td id="file-constants_dbpath-cs-LC3" class="blob-code blob-code-inner js-file-line">    public static readonly string DbFilePath = </td>
</tr>
<tr>
<td id="file-constants_dbpath-cs-L4" class="blob-num js-line-number js-blob-rnum" data-line-number="4"></td>
<td id="file-constants_dbpath-cs-LC4" class="blob-code blob-code-inner js-file-line">        Path.Combine(</td>
</tr>
<tr>
<td id="file-constants_dbpath-cs-L5" class="blob-num js-line-number js-blob-rnum" data-line-number="5"></td>
<td id="file-constants_dbpath-cs-LC5" class="blob-code blob-code-inner js-file-line">                Environment.GetFolderPath(Environment.SpecialFolder.Personal), </td>
</tr>
<tr>
<td id="file-constants_dbpath-cs-L6" class="blob-num js-line-number js-blob-rnum" data-line-number="6"></td>
<td id="file-constants_dbpath-cs-LC6" class="blob-code blob-code-inner js-file-line">                &quot;moneyback.db&quot;</td>
</tr>
<tr>
<td id="file-constants_dbpath-cs-L7" class="blob-num js-line-number js-blob-rnum" data-line-number="7"></td>
<td id="file-constants_dbpath-cs-LC7" class="blob-code blob-code-inner js-file-line">        );</td>
</tr>
<tr>
<td id="file-constants_dbpath-cs-L8" class="blob-num js-line-number js-blob-rnum" data-line-number="8"></td>
<td id="file-constants_dbpath-cs-LC8" class="blob-code blob-code-inner js-file-line">}</td>
</tr>
</table>
</div></div>
</p></div>
</div></div>
<div class="gist-meta">
        <a href="https://gist.github.com/dsibinski/d5fb45ea47ee86b0540bf9f68666d103/raw/f01a0588740a5477cc78d7ce8e9e89b80d95d299/Constants_dbPath.cs" style="float:right" class="Link--inTextBlock">view raw</a><br />
        <a href="https://gist.github.com/dsibinski/d5fb45ea47ee86b0540bf9f68666d103#file-constants_dbpath-cs" class="Link--inTextBlock"><br />
          Constants_dbPath.cs<br />
        </a><br />
        hosted with &#10084; by <a class="Link--inTextBlock" href="https://github.com">GitHub</a>
      </div>
</p></div>
</div>
<h2>Database access methods</h2>
<p>As soon as we have entity class defined and location of database file, we can start performing operations on the db &#8211; define tables, add new entities (rows), retrieve rows from the database, delete them etc. In general, it comes down to creating <em>SQLiteConnection </em>object and performing operations on it.</p>
<p>There are several approaches of exposing Data Access Layer services/methods &#8211; we can create a database service, entities repository or use complex services location and dependency injection in order to operate our persistent data. I don&#8217;t know what is a standard way in Android applications, but I decided to create a very simple repository for our <em>Person</em> entity and expose database-access methods in it:</p>
<p><style>.gist table { margin-bottom: 0; }</style>
<div style="tab-size: 8" id="gist45865287" class="gist">
<div class="gist-file" translate="no" data-color-mode="light" data-light-theme="light">
<div class="gist-data">
<div class="js-gist-file-update-container js-task-list-container">
<div id="file-peoplerepository-cs" class="file my-2">
<div itemprop="text"
      class="Box-body p-0 blob-wrapper data type-c  "
      style="overflow: auto" tabindex="0" role="region"
      aria-label="PeopleRepository.cs content, created by dsibinski on 09:39PM on March 19, 2017."
    ></p>
<div class="js-check-hidden-unicode js-blob-code-container blob-code-content">
<p>  <template class="js-file-alert-template"></p>
<div data-view-component="true" class="flash flash-warn flash-full d-flex flex-items-center">
  <svg aria-hidden="true" height="16" viewBox="0 0 16 16" version="1.1" width="16" data-view-component="true" class="octicon octicon-alert">
    <path d="M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z"></path>
</svg><br />
    <span><br />
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.<br />
      <a class="Link--inTextBlock" href="https://github.co/hiddenchars" target="_blank">Learn more about bidirectional Unicode characters</a><br />
    </span></p>
<div data-view-component="true" class="flash-action">        <a href="{{ revealButtonHref }}" data-view-component="true" class="btn-sm btn">    Show hidden characters<br />
</a>
</div>
</div>
<p></template><br />
<template class="js-line-alert-template"><br />
  <span aria-label="This line has hidden Unicode characters" data-view-component="true" class="line-alert tooltipped tooltipped-e"><br />
    <svg aria-hidden="true" height="16" viewBox="0 0 16 16" version="1.1" width="16" data-view-component="true" class="octicon octicon-alert">
    <path d="M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z"></path>
</svg><br />
</span></template></p>
<table data-hpc class="highlight tab-size js-file-line-container" data-tab-size="4" data-paste-markdown-skip data-tagsearch-path="PeopleRepository.cs">
<tr>
<td id="file-peoplerepository-cs-L1" class="blob-num js-line-number js-blob-rnum" data-line-number="1"></td>
<td id="file-peoplerepository-cs-LC1" class="blob-code blob-code-inner js-file-line">public class PeopleRepository</td>
</tr>
<tr>
<td id="file-peoplerepository-cs-L2" class="blob-num js-line-number js-blob-rnum" data-line-number="2"></td>
<td id="file-peoplerepository-cs-LC2" class="blob-code blob-code-inner js-file-line">{</td>
</tr>
<tr>
<td id="file-peoplerepository-cs-L3" class="blob-num js-line-number js-blob-rnum" data-line-number="3"></td>
<td id="file-peoplerepository-cs-LC3" class="blob-code blob-code-inner js-file-line">    private SQLiteConnection db = null;</td>
</tr>
<tr>
<td id="file-peoplerepository-cs-L4" class="blob-num js-line-number js-blob-rnum" data-line-number="4"></td>
<td id="file-peoplerepository-cs-LC4" class="blob-code blob-code-inner js-file-line">    protected static PeopleRepository me;</td>
</tr>
<tr>
<td id="file-peoplerepository-cs-L5" class="blob-num js-line-number js-blob-rnum" data-line-number="5"></td>
<td id="file-peoplerepository-cs-LC5" class="blob-code blob-code-inner js-file-line">
</td>
</tr>
<tr>
<td id="file-peoplerepository-cs-L6" class="blob-num js-line-number js-blob-rnum" data-line-number="6"></td>
<td id="file-peoplerepository-cs-LC6" class="blob-code blob-code-inner js-file-line">    static PeopleRepository()</td>
</tr>
<tr>
<td id="file-peoplerepository-cs-L7" class="blob-num js-line-number js-blob-rnum" data-line-number="7"></td>
<td id="file-peoplerepository-cs-LC7" class="blob-code blob-code-inner js-file-line">    {</td>
</tr>
<tr>
<td id="file-peoplerepository-cs-L8" class="blob-num js-line-number js-blob-rnum" data-line-number="8"></td>
<td id="file-peoplerepository-cs-LC8" class="blob-code blob-code-inner js-file-line">        me = new PeopleRepository();</td>
</tr>
<tr>
<td id="file-peoplerepository-cs-L9" class="blob-num js-line-number js-blob-rnum" data-line-number="9"></td>
<td id="file-peoplerepository-cs-LC9" class="blob-code blob-code-inner js-file-line">    }</td>
</tr>
<tr>
<td id="file-peoplerepository-cs-L10" class="blob-num js-line-number js-blob-rnum" data-line-number="10"></td>
<td id="file-peoplerepository-cs-LC10" class="blob-code blob-code-inner js-file-line">
</td>
</tr>
<tr>
<td id="file-peoplerepository-cs-L11" class="blob-num js-line-number js-blob-rnum" data-line-number="11"></td>
<td id="file-peoplerepository-cs-LC11" class="blob-code blob-code-inner js-file-line">    protected PeopleRepository()</td>
</tr>
<tr>
<td id="file-peoplerepository-cs-L12" class="blob-num js-line-number js-blob-rnum" data-line-number="12"></td>
<td id="file-peoplerepository-cs-LC12" class="blob-code blob-code-inner js-file-line">    {</td>
</tr>
<tr>
<td id="file-peoplerepository-cs-L13" class="blob-num js-line-number js-blob-rnum" data-line-number="13"></td>
<td id="file-peoplerepository-cs-LC13" class="blob-code blob-code-inner js-file-line">        db = new SQLiteConnection(Constants.DbFilePath);</td>
</tr>
<tr>
<td id="file-peoplerepository-cs-L14" class="blob-num js-line-number js-blob-rnum" data-line-number="14"></td>
<td id="file-peoplerepository-cs-LC14" class="blob-code blob-code-inner js-file-line">        db.CreateTable&lt;Person&gt;();</td>
</tr>
<tr>
<td id="file-peoplerepository-cs-L15" class="blob-num js-line-number js-blob-rnum" data-line-number="15"></td>
<td id="file-peoplerepository-cs-LC15" class="blob-code blob-code-inner js-file-line">    }</td>
</tr>
<tr>
<td id="file-peoplerepository-cs-L16" class="blob-num js-line-number js-blob-rnum" data-line-number="16"></td>
<td id="file-peoplerepository-cs-LC16" class="blob-code blob-code-inner js-file-line">
</td>
</tr>
<tr>
<td id="file-peoplerepository-cs-L17" class="blob-num js-line-number js-blob-rnum" data-line-number="17"></td>
<td id="file-peoplerepository-cs-LC17" class="blob-code blob-code-inner js-file-line">    public static int SavePerson(Person person)</td>
</tr>
<tr>
<td id="file-peoplerepository-cs-L18" class="blob-num js-line-number js-blob-rnum" data-line-number="18"></td>
<td id="file-peoplerepository-cs-LC18" class="blob-code blob-code-inner js-file-line">    {</td>
</tr>
<tr>
<td id="file-peoplerepository-cs-L19" class="blob-num js-line-number js-blob-rnum" data-line-number="19"></td>
<td id="file-peoplerepository-cs-LC19" class="blob-code blob-code-inner js-file-line">        me.db.Insert(person);</td>
</tr>
<tr>
<td id="file-peoplerepository-cs-L20" class="blob-num js-line-number js-blob-rnum" data-line-number="20"></td>
<td id="file-peoplerepository-cs-LC20" class="blob-code blob-code-inner js-file-line">        return person.Id;</td>
</tr>
<tr>
<td id="file-peoplerepository-cs-L21" class="blob-num js-line-number js-blob-rnum" data-line-number="21"></td>
<td id="file-peoplerepository-cs-LC21" class="blob-code blob-code-inner js-file-line">    }</td>
</tr>
<tr>
<td id="file-peoplerepository-cs-L22" class="blob-num js-line-number js-blob-rnum" data-line-number="22"></td>
<td id="file-peoplerepository-cs-LC22" class="blob-code blob-code-inner js-file-line">
</td>
</tr>
<tr>
<td id="file-peoplerepository-cs-L23" class="blob-num js-line-number js-blob-rnum" data-line-number="23"></td>
<td id="file-peoplerepository-cs-LC23" class="blob-code blob-code-inner js-file-line">    public static Person GetPerson(int id)</td>
</tr>
<tr>
<td id="file-peoplerepository-cs-L24" class="blob-num js-line-number js-blob-rnum" data-line-number="24"></td>
<td id="file-peoplerepository-cs-LC24" class="blob-code blob-code-inner js-file-line">    {</td>
</tr>
<tr>
<td id="file-peoplerepository-cs-L25" class="blob-num js-line-number js-blob-rnum" data-line-number="25"></td>
<td id="file-peoplerepository-cs-LC25" class="blob-code blob-code-inner js-file-line">        return me.db.Get&lt;Person&gt;(p =&gt; p.Id == id);</td>
</tr>
<tr>
<td id="file-peoplerepository-cs-L26" class="blob-num js-line-number js-blob-rnum" data-line-number="26"></td>
<td id="file-peoplerepository-cs-LC26" class="blob-code blob-code-inner js-file-line">    }</td>
</tr>
<tr>
<td id="file-peoplerepository-cs-L27" class="blob-num js-line-number js-blob-rnum" data-line-number="27"></td>
<td id="file-peoplerepository-cs-LC27" class="blob-code blob-code-inner js-file-line">}</td>
</tr>
</table>
</div></div>
</p></div>
</div></div>
<div class="gist-meta">
        <a href="https://gist.github.com/dsibinski/c8256dbaaf5970a512062aa3a9fe5ad3/raw/5371465e526cbf189e77c720e9f97e2b37748042/PeopleRepository.cs" style="float:right" class="Link--inTextBlock">view raw</a><br />
        <a href="https://gist.github.com/dsibinski/c8256dbaaf5970a512062aa3a9fe5ad3#file-peoplerepository-cs" class="Link--inTextBlock"><br />
          PeopleRepository.cs<br />
        </a><br />
        hosted with &#10084; by <a class="Link--inTextBlock" href="https://github.com">GitHub</a>
      </div>
</p></div>
</div>
<p>I haven&#8217;t even created any abstract layer for the repository (e.g. interface), as I simply don&#8217;t know what approach I will use for the next entities. I&#8217;m not sure if the repository is the right choice. On the other hand, I don&#8217;t think such rather simple app requires something more. <span style="text-decoration: underline;"><strong>What do you think ?</p>
<p></strong></span></p>
<p>As you can see, <em>PeopleRepository</em> executes its static constructor when any of static methods is called for the first time, in which it instantiates <em>db</em> variable of type <em>SQLiteConnection</em>, giving it the path to db file we defined. If database file doesn&#8217;t exist, it will be automatically created. Then, <em>db.CreateTable&lt;Person&gt;()</em> creates <em>People</em> table if it doesn&#8217;t exist, according to what we defined in <em>Person</em> entity model.</p>
<p>Two static methods &#8211; <em>SavePerson</em> and <em>GetPerson </em>allow to save and retrieve <em>Person</em> entity to/from the database accordingly. The first one is additionally returning the integer <em>Id</em> of newly added row.</p>
<h2>Usage in the code</h2>
<p>I added two text fields to <em>MoneyBack</em> app to enter name and last name of person to be added and one button which actually allows to perform saving operation:</p>
<figure id="attachment_1088" aria-describedby="caption-attachment-1088" style="width: 233px" class="wp-caption aligncenter"><img data-recalc-dims="1" fetchpriority="high" decoding="async" data-attachment-id="1088" data-permalink="https://www.codejourney.net/using-sqlite-database-in-xamarin-android/moneyback_savingperson/" data-orig-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2017/03/MoneyBack_SavingPerson.png?fit=768%2C1280&amp;ssl=1" data-orig-size="768,1280" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="MoneyBack_SavingPerson" data-image-description="" data-image-caption="" data-medium-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2017/03/MoneyBack_SavingPerson.png?fit=180%2C300&amp;ssl=1" data-large-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2017/03/MoneyBack_SavingPerson.png?fit=614%2C1024&amp;ssl=1" class=" wp-image-1088 aligncenter" src="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2017/03/MoneyBack_SavingPerson.png?resize=233%2C388&#038;ssl=1" alt="MoneyBack_SavingPerson.png" width="233" height="388" srcset="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2017/03/MoneyBack_SavingPerson.png?w=768&amp;ssl=1 768w, https://i0.wp.com/www.codejourney.net/wp-content/uploads/2017/03/MoneyBack_SavingPerson.png?resize=180%2C300&amp;ssl=1 180w, https://i0.wp.com/www.codejourney.net/wp-content/uploads/2017/03/MoneyBack_SavingPerson.png?resize=614%2C1024&amp;ssl=1 614w, https://i0.wp.com/www.codejourney.net/wp-content/uploads/2017/03/MoneyBack_SavingPerson.png?resize=720%2C1200&amp;ssl=1 720w" sizes="(max-width: 233px) 100vw, 233px" /><figcaption id="caption-attachment-1088" class="wp-caption-text">Adding new Person</figcaption></figure>
<p>On clicking the button, the following method is executed:</p>
<p><style>.gist table { margin-bottom: 0; }</style>
<div style="tab-size: 8" id="gist45865616" class="gist">
<div class="gist-file" translate="no" data-color-mode="light" data-light-theme="light">
<div class="gist-data">
<div class="js-gist-file-update-container js-task-list-container">
<div id="file-addpersonbuttonclick-cs" class="file my-2">
<div itemprop="text"
      class="Box-body p-0 blob-wrapper data type-c  "
      style="overflow: auto" tabindex="0" role="region"
      aria-label="addPersonButtonClick.cs content, created by dsibinski on 09:58PM on March 19, 2017."
    ></p>
<div class="js-check-hidden-unicode js-blob-code-container blob-code-content">
<p>  <template class="js-file-alert-template"></p>
<div data-view-component="true" class="flash flash-warn flash-full d-flex flex-items-center">
  <svg aria-hidden="true" height="16" viewBox="0 0 16 16" version="1.1" width="16" data-view-component="true" class="octicon octicon-alert">
    <path d="M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z"></path>
</svg><br />
    <span><br />
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.<br />
      <a class="Link--inTextBlock" href="https://github.co/hiddenchars" target="_blank">Learn more about bidirectional Unicode characters</a><br />
    </span></p>
<div data-view-component="true" class="flash-action">        <a href="{{ revealButtonHref }}" data-view-component="true" class="btn-sm btn">    Show hidden characters<br />
</a>
</div>
</div>
<p></template><br />
<template class="js-line-alert-template"><br />
  <span aria-label="This line has hidden Unicode characters" data-view-component="true" class="line-alert tooltipped tooltipped-e"><br />
    <svg aria-hidden="true" height="16" viewBox="0 0 16 16" version="1.1" width="16" data-view-component="true" class="octicon octicon-alert">
    <path d="M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z"></path>
</svg><br />
</span></template></p>
<table data-hpc class="highlight tab-size js-file-line-container" data-tab-size="4" data-paste-markdown-skip data-tagsearch-path="addPersonButtonClick.cs">
<tr>
<td id="file-addpersonbuttonclick-cs-L1" class="blob-num js-line-number js-blob-rnum" data-line-number="1"></td>
<td id="file-addpersonbuttonclick-cs-LC1" class="blob-code blob-code-inner js-file-line">private void _btnAddPerson_Click(object sender, EventArgs e)</td>
</tr>
<tr>
<td id="file-addpersonbuttonclick-cs-L2" class="blob-num js-line-number js-blob-rnum" data-line-number="2"></td>
<td id="file-addpersonbuttonclick-cs-LC2" class="blob-code blob-code-inner js-file-line">{</td>
</tr>
<tr>
<td id="file-addpersonbuttonclick-cs-L3" class="blob-num js-line-number js-blob-rnum" data-line-number="3"></td>
<td id="file-addpersonbuttonclick-cs-LC3" class="blob-code blob-code-inner js-file-line">    var name = _inputName.Text;</td>
</tr>
<tr>
<td id="file-addpersonbuttonclick-cs-L4" class="blob-num js-line-number js-blob-rnum" data-line-number="4"></td>
<td id="file-addpersonbuttonclick-cs-LC4" class="blob-code blob-code-inner js-file-line">    var lastName = _inputLastName.Text;</td>
</tr>
<tr>
<td id="file-addpersonbuttonclick-cs-L5" class="blob-num js-line-number js-blob-rnum" data-line-number="5"></td>
<td id="file-addpersonbuttonclick-cs-LC5" class="blob-code blob-code-inner js-file-line">
</td>
</tr>
<tr>
<td id="file-addpersonbuttonclick-cs-L6" class="blob-num js-line-number js-blob-rnum" data-line-number="6"></td>
<td id="file-addpersonbuttonclick-cs-LC6" class="blob-code blob-code-inner js-file-line">    var id = PeopleRepository.SavePerson(new Person</td>
</tr>
<tr>
<td id="file-addpersonbuttonclick-cs-L7" class="blob-num js-line-number js-blob-rnum" data-line-number="7"></td>
<td id="file-addpersonbuttonclick-cs-LC7" class="blob-code blob-code-inner js-file-line">    {</td>
</tr>
<tr>
<td id="file-addpersonbuttonclick-cs-L8" class="blob-num js-line-number js-blob-rnum" data-line-number="8"></td>
<td id="file-addpersonbuttonclick-cs-LC8" class="blob-code blob-code-inner js-file-line">        Name = name,</td>
</tr>
<tr>
<td id="file-addpersonbuttonclick-cs-L9" class="blob-num js-line-number js-blob-rnum" data-line-number="9"></td>
<td id="file-addpersonbuttonclick-cs-LC9" class="blob-code blob-code-inner js-file-line">        LastName = lastName</td>
</tr>
<tr>
<td id="file-addpersonbuttonclick-cs-L10" class="blob-num js-line-number js-blob-rnum" data-line-number="10"></td>
<td id="file-addpersonbuttonclick-cs-LC10" class="blob-code blob-code-inner js-file-line">    });</td>
</tr>
<tr>
<td id="file-addpersonbuttonclick-cs-L11" class="blob-num js-line-number js-blob-rnum" data-line-number="11"></td>
<td id="file-addpersonbuttonclick-cs-LC11" class="blob-code blob-code-inner js-file-line">
</td>
</tr>
<tr>
<td id="file-addpersonbuttonclick-cs-L12" class="blob-num js-line-number js-blob-rnum" data-line-number="12"></td>
<td id="file-addpersonbuttonclick-cs-LC12" class="blob-code blob-code-inner js-file-line">    var person = PeopleRepository.GetPerson(id);</td>
</tr>
<tr>
<td id="file-addpersonbuttonclick-cs-L13" class="blob-num js-line-number js-blob-rnum" data-line-number="13"></td>
<td id="file-addpersonbuttonclick-cs-LC13" class="blob-code blob-code-inner js-file-line">
</td>
</tr>
<tr>
<td id="file-addpersonbuttonclick-cs-L14" class="blob-num js-line-number js-blob-rnum" data-line-number="14"></td>
<td id="file-addpersonbuttonclick-cs-LC14" class="blob-code blob-code-inner js-file-line">    if (person == null)</td>
</tr>
<tr>
<td id="file-addpersonbuttonclick-cs-L15" class="blob-num js-line-number js-blob-rnum" data-line-number="15"></td>
<td id="file-addpersonbuttonclick-cs-LC15" class="blob-code blob-code-inner js-file-line">        Toast.MakeText(this, $&quot;Person: Name={name}, LastName={lastName} wasn&#39;t properly saved!&quot;, ToastLength.Long).Show();</td>
</tr>
<tr>
<td id="file-addpersonbuttonclick-cs-L16" class="blob-num js-line-number js-blob-rnum" data-line-number="16"></td>
<td id="file-addpersonbuttonclick-cs-LC16" class="blob-code blob-code-inner js-file-line">    else</td>
</tr>
<tr>
<td id="file-addpersonbuttonclick-cs-L17" class="blob-num js-line-number js-blob-rnum" data-line-number="17"></td>
<td id="file-addpersonbuttonclick-cs-LC17" class="blob-code blob-code-inner js-file-line">        Toast.MakeText(this, $&quot;Person saved, details: {person}&quot;, ToastLength.Long).Show();</td>
</tr>
<tr>
<td id="file-addpersonbuttonclick-cs-L18" class="blob-num js-line-number js-blob-rnum" data-line-number="18"></td>
<td id="file-addpersonbuttonclick-cs-LC18" class="blob-code blob-code-inner js-file-line">
</td>
</tr>
<tr>
<td id="file-addpersonbuttonclick-cs-L19" class="blob-num js-line-number js-blob-rnum" data-line-number="19"></td>
<td id="file-addpersonbuttonclick-cs-LC19" class="blob-code blob-code-inner js-file-line">}</td>
</tr>
</table>
</div></div>
</p></div>
</div></div>
<div class="gist-meta">
        <a href="https://gist.github.com/dsibinski/4b9472f0a5f787a265fee0982dfcec96/raw/5b9be5b6240b78b71654a2a3651315b9e508b3c6/addPersonButtonClick.cs" style="float:right" class="Link--inTextBlock">view raw</a><br />
        <a href="https://gist.github.com/dsibinski/4b9472f0a5f787a265fee0982dfcec96#file-addpersonbuttonclick-cs" class="Link--inTextBlock"><br />
          addPersonButtonClick.cs<br />
        </a><br />
        hosted with &#10084; by <a class="Link--inTextBlock" href="https://github.com">GitHub</a>
      </div>
</p></div>
</div>
<p>&nbsp;</p>
<p>Here&#8217;s the result of clicking the button in the app:</p>
<figure id="attachment_1093" aria-describedby="caption-attachment-1093" style="width: 249px" class="wp-caption aligncenter"><img data-recalc-dims="1" decoding="async" data-attachment-id="1093" data-permalink="https://www.codejourney.net/using-sqlite-database-in-xamarin-android/moneyback_savingperson_success/" data-orig-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2017/03/MoneyBack_SavingPerson_Success.png?fit=768%2C1280&amp;ssl=1" data-orig-size="768,1280" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="MoneyBack_SavingPerson_Success" data-image-description="" data-image-caption="" data-medium-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2017/03/MoneyBack_SavingPerson_Success.png?fit=180%2C300&amp;ssl=1" data-large-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2017/03/MoneyBack_SavingPerson_Success.png?fit=614%2C1024&amp;ssl=1" class=" wp-image-1093 aligncenter" src="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2017/03/MoneyBack_SavingPerson_Success.png?resize=249%2C415&#038;ssl=1" alt="MoneyBack_SavingPerson_Success.png" width="249" height="415" srcset="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2017/03/MoneyBack_SavingPerson_Success.png?w=768&amp;ssl=1 768w, https://i0.wp.com/www.codejourney.net/wp-content/uploads/2017/03/MoneyBack_SavingPerson_Success.png?resize=180%2C300&amp;ssl=1 180w, https://i0.wp.com/www.codejourney.net/wp-content/uploads/2017/03/MoneyBack_SavingPerson_Success.png?resize=614%2C1024&amp;ssl=1 614w, https://i0.wp.com/www.codejourney.net/wp-content/uploads/2017/03/MoneyBack_SavingPerson_Success.png?resize=720%2C1200&amp;ssl=1 720w" sizes="(max-width: 249px) 100vw, 249px" /><figcaption id="caption-attachment-1093" class="wp-caption-text">Person saved successfully</figcaption></figure>
<p>As you can see, in the toast message received, <em>Id</em> of the <em>Person</em> added has value 3. That&#8217;s because when I launched the app previously I&#8217;ve already added two new people. It shows that the data is really persistently stored and doesn&#8217;t disappear when the app is restarted.</p>
<h2>Summary</h2>
<p>Today we&#8217;ve seen that using SQLite file database in Xamarin.Android is very easy. Thanks to SQLite.NET lightweight ORM library we were able to quickly start working with the db.</p>
<p>To make the code ready for &#8220;production&#8221; use, I should also make it thread-safe (more details e.g. <a href="https://developer.xamarin.com/guides/android/application_fundamentals/data/part_2_configuration/" target="_blank" rel="noopener">here</a>) and add unit tests (which I&#8217;m BTW not able to do for now, I&#8217;m getting <em>TargetInvocationException</em> when trying to run any NUnit tests adding <em>Unit Test App (Android)</em> project in VS 2017&#8230;).</p>
<p>I&#8217;m also not sure if repository is a proper concept for managing local db entities in Android project. <span style="text-decoration: underline;"><strong>If you have more experience, I&#8217;d be grateful to hear how it should be done in a really &#8220;Android way&#8221; <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f642.png" alt="🙂" class="wp-smiley" style="height: 1em; max-height: 1em;" /></strong></span></p>
<p>The post <a href="https://www.codejourney.net/using-sqlite-database-in-xamarin-android/">Using SQLite database in Xamarin.Android</a> appeared first on <a href="https://www.codejourney.net">CodeJourney.net</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.codejourney.net/using-sqlite-database-in-xamarin-android/feed/</wfw:commentRss>
			<slash:comments>7</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">970</post-id>	</item>
	</channel>
</rss>
