{"id":512,"date":"2013-07-13T16:25:53","date_gmt":"2013-07-13T14:25:53","guid":{"rendered":"http:\/\/blog.ansuz.nl\/?p=512"},"modified":"2013-07-13T16:25:53","modified_gmt":"2013-07-13T14:25:53","slug":"sqlite-on-android","status":"publish","type":"post","link":"http:\/\/blog.ansuz.nl\/index.php\/2013\/07\/13\/sqlite-on-android\/","title":{"rendered":"SQLite on Android"},"content":{"rendered":"<p>In the past week I started working with SQLite databases on Android.<\/p>\n<p>It can be quite cumbersome to explore the data stored in the DB on an actual device. What has helped me a lot is grabbing the DB from the device and loading the SQLite DB in a third party SQLite browser \/ manager. This will give you a lot more flexibility on exploring the data that is stored and will also be a great help for testing your queries.<br \/>\nYou can retrieve your database from the device using ADB: <\/p>\n<div id=\"ig-sh-1\" class=\"syntax_hilite\">\n\n\t\t<div class=\"toolbar\">\n\n\t\t<div class=\"view-different-container\">\n\t\t\t\t\t\t<a href=\"#\" class=\"view-different\">&lt; View <span>plain text<\/span> &gt;<\/a>\n\t\t\t\t\t<\/div>\n\n\t\t<div class=\"language-name\">code<\/div>\n\n\t\t\n\t\t<br clear=\"both\">\n\n\t<\/div>\n\t\n\t<div class=\"code\">\n\t\t<ol class=\"code\" style=\"font-family:monospace\"><li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">adb pull \/data\/data\/[app_package]\/databases\/[db_file] .<\/div><\/li>\n<\/ol>\t<\/div>\n\n<\/div>\n\n<p>I&#8217;ve been using <a href=\"http:\/\/sourceforge.net\/projects\/sqlitebrowser\/\" target=\"_blank\">SQLiteBrowser<\/a> to explore the DB and test my queries.<\/p>\n<p>One thing I noticed is that inserting around a hundred rows took quite some time, about 350ms. I was inserting one row at a time and after some Googling on how to insert multiple rows in one query, I found an <a href=\"http:\/\/stackoverflow.com\/a\/5009740\" target=\"_blank\">answer on StackOverflow<\/a>. The suggestion was to do something like the following:<\/p>\n<div id=\"ig-sh-2\" class=\"syntax_hilite\">\n\n\t\t<div class=\"toolbar\">\n\n\t\t<div class=\"view-different-container\">\n\t\t\t\t\t\t<a href=\"#\" class=\"view-different\">&lt; View <span>plain text<\/span> &gt;<\/a>\n\t\t\t\t\t<\/div>\n\n\t\t<div class=\"language-name\">sql<\/div>\n\n\t\t\n\t\t<br clear=\"both\">\n\n\t<\/div>\n\t\n\t<div class=\"code\">\n\t\t<ol class=\"sql\" style=\"font-family:monospace\"><li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #993333;font-weight: bold\">INSERT<\/span> <span style=\"color: #993333;font-weight: bold\">INTO<\/span> <span style=\"color: #ff0000\">'tablename'<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp; &nbsp; <span style=\"color: #993333;font-weight: bold\">SELECT<\/span> <span style=\"color: #ff0000\">'data1'<\/span> <span style=\"color: #993333;font-weight: bold\">AS<\/span> <span style=\"color: #ff0000\">'column1'<\/span><span style=\"color: #66cc66\">,<\/span> <span style=\"color: #ff0000\">'data2'<\/span> <span style=\"color: #993333;font-weight: bold\">AS<\/span> <span style=\"color: #ff0000\">'column2'<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp; &nbsp; <span style=\"color: #993333;font-weight: bold\">UNION<\/span> <span style=\"color: #993333;font-weight: bold\">SELECT<\/span> <span style=\"color: #ff0000\">'data3'<\/span><span style=\"color: #66cc66\">,<\/span> <span style=\"color: #ff0000\">'data4'<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp; &nbsp; <span style=\"color: #993333;font-weight: bold\">UNION<\/span> <span style=\"color: #993333;font-weight: bold\">SELECT<\/span> <span style=\"color: #ff0000\">'data5'<\/span><span style=\"color: #66cc66\">,<\/span> <span style=\"color: #ff0000\">'data6'<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp; &nbsp; <span style=\"color: #993333;font-weight: bold\">UNION<\/span> <span style=\"color: #993333;font-weight: bold\">SELECT<\/span> <span style=\"color: #ff0000\">'data7'<\/span><span style=\"color: #66cc66\">,<\/span> <span style=\"color: #ff0000\">'data8'<\/span><\/div><\/li>\n<\/ol>\t<\/div>\n\n<\/div>\n\n<p>After trying this approach it cut the insertion time in half near the 170ms mark, still quite slow.<br \/>\nSome more searching brought me to an article about &#8220;<a href=\"http:\/\/tech.vg.no\/2011\/04\/04\/speeding-up-sqlite-insert-operations\/\" target=\"_blank\">Speeding up SQLite insert operations<\/a>&#8220;. What this article advises is to use transactions, an example:<\/p>\n<div id=\"ig-sh-3\" class=\"syntax_hilite\">\n\n\t\t<div class=\"toolbar\">\n\n\t\t<div class=\"view-different-container\">\n\t\t\t\t\t\t<a href=\"#\" class=\"view-different\">&lt; View <span>plain text<\/span> &gt;<\/a>\n\t\t\t\t\t<\/div>\n\n\t\t<div class=\"language-name\">java<\/div>\n\n\t\t\n\t\t<br clear=\"both\">\n\n\t<\/div>\n\t\n\t<div class=\"code\">\n\t\t<ol class=\"java\" style=\"font-family:monospace\"><li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">database.<span style=\"color: #006633\">beginTransaction<\/span><span style=\"color: #009900\">&#040;<\/span><span style=\"color: #009900\">&#041;<\/span><span style=\"color: #339933\">;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #000000;font-weight: bold\">for<\/span> <span style=\"color: #009900\">&#040;<\/span>...<span style=\"color: #009900\">&#041;<\/span> <span style=\"color: #009900\">&#123;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp; &nbsp; database.<span style=\"color: #006633\">insert<\/span><span style=\"color: #009900\">&#040;<\/span>...<span style=\"color: #009900\">&#041;<\/span><span style=\"color: #339933\">;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #009900\">&#125;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">database.<span style=\"color: #006633\">setTransactionSuccessful<\/span><span style=\"color: #009900\">&#040;<\/span><span style=\"color: #009900\">&#041;<\/span><span style=\"color: #339933\">;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">database.<span style=\"color: #006633\">endTransaction<\/span><span style=\"color: #009900\">&#040;<\/span><span style=\"color: #009900\">&#041;<\/span><span style=\"color: #339933\">;<\/span><\/div><\/li>\n<\/ol>\t<\/div>\n\n<\/div>\n\n<p>Using this technique the insertion was sped up to roughly 25ms, much better!<\/p>\n<p>As an alternative to using a database, you could just store the data on disk. This works fine especially when you just use a database to store data and don&#8217;t need to sort it or do more complex queries. Even if you needed sorting, you could use the <a href=\"http:\/\/developer.android.com\/reference\/java\/util\/SortedMap.html\" target=\"_blank\">SortedMap<\/a> class.<\/p>\n<!-- AddThis Advanced Settings generic via filter on the_content --><!-- AddThis Share Buttons generic via filter on the_content -->","protected":false},"excerpt":{"rendered":"<p>In the past week I started working with SQLite databases on Android. It can be quite cumbersome to explore the data stored in the DB on an actual device. What has helped me a lot is grabbing the DB from &hellip; <a href=\"http:\/\/blog.ansuz.nl\/index.php\/2013\/07\/13\/sqlite-on-android\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><!-- AddThis Advanced Settings generic via filter on get_the_excerpt --><!-- AddThis Share Buttons generic via filter on get_the_excerpt --><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-512","post","type-post","status-publish","format-standard","hentry","category-random"],"_links":{"self":[{"href":"http:\/\/blog.ansuz.nl\/index.php\/wp-json\/wp\/v2\/posts\/512","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/blog.ansuz.nl\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/blog.ansuz.nl\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/blog.ansuz.nl\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/blog.ansuz.nl\/index.php\/wp-json\/wp\/v2\/comments?post=512"}],"version-history":[{"count":2,"href":"http:\/\/blog.ansuz.nl\/index.php\/wp-json\/wp\/v2\/posts\/512\/revisions"}],"predecessor-version":[{"id":514,"href":"http:\/\/blog.ansuz.nl\/index.php\/wp-json\/wp\/v2\/posts\/512\/revisions\/514"}],"wp:attachment":[{"href":"http:\/\/blog.ansuz.nl\/index.php\/wp-json\/wp\/v2\/media?parent=512"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/blog.ansuz.nl\/index.php\/wp-json\/wp\/v2\/categories?post=512"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/blog.ansuz.nl\/index.php\/wp-json\/wp\/v2\/tags?post=512"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}