{"id":11324,"date":"2015-07-12T20:57:03","date_gmt":"2015-07-12T15:57:03","guid":{"rendered":"https:\/\/blazeminds.com\/blog\/?p=11324"},"modified":"2015-07-12T21:10:19","modified_gmt":"2015-07-12T16:10:19","slug":"how-to-make-android-database-application-sqlite-part-1","status":"publish","type":"post","link":"https:\/\/blazeminds.com\/blog\/how-to-make-android-database-application-sqlite-part-1\/","title":{"rendered":"How to make Android Database application (SQLite) Part 1"},"content":{"rendered":"<p>Assalam-u-Alaikum, Welcome again folks. Today in this post I will be guiding you to make Android in App database through SQLite database. So lets get started!<\/p>\n<p><!--more--><\/p>\n<p><b>Step 1<\/b><br \/>\nCreate new Project &amp; name it whatever you want to. After naming Project you will be asked <i>Company Domain, <\/i>this is where you define your Project package.<br \/>\nI am naming Project to <b>&#8220;DBClass&#8221; <\/b>and Company Domain <b>&#8220;tuts.com&#8221;<\/b>. Company Domain counts the most while uploading your app on Google Play Store.<br \/>\nAfter naming proceed to next.<\/p>\n<p><a href=\"https:\/\/blazeminds.com\/blog\/wp-content\/uploads\/2015\/07\/1.png\"><img loading=\"lazy\" class=\"  alignleft wp-image-11325 size-full\" src=\"https:\/\/blazeminds.com\/blog\/wp-content\/uploads\/2015\/07\/1.png\" alt=\"1\" width=\"1366\" height=\"768\" srcset=\"https:\/\/blazeminds.com\/blog\/wp-content\/uploads\/2015\/07\/1.png 1366w, https:\/\/blazeminds.com\/blog\/wp-content\/uploads\/2015\/07\/1-300x169.png 300w, https:\/\/blazeminds.com\/blog\/wp-content\/uploads\/2015\/07\/1-1024x576.png 1024w\" sizes=\"(max-width: 1366px) 100vw, 1366px\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p><b>Step 2<\/b><br \/>\nIn next window you will be asked to choose minimum <b><i>Android OS Level<\/i><\/b> OR\u00a0<i>API Level<\/i>. Best choice is to select <b><i>API 16<\/i><\/b> OR <i>Android 4.1 (JellyBean)<\/i>, as mostly nowadays at minimum Android 4.1 JellyBean is widely used.<br \/>\n<b><br \/>\n<\/b><\/p>\n<p><a href=\"https:\/\/blazeminds.com\/blog\/wp-content\/uploads\/2015\/07\/2.png\"><img loading=\"lazy\" class=\"alignleft wp-image-11326 size-full\" src=\"https:\/\/blazeminds.com\/blog\/wp-content\/uploads\/2015\/07\/2.png\" alt=\"2\" width=\"1366\" height=\"768\" srcset=\"https:\/\/blazeminds.com\/blog\/wp-content\/uploads\/2015\/07\/2.png 1366w, https:\/\/blazeminds.com\/blog\/wp-content\/uploads\/2015\/07\/2-300x169.png 300w, https:\/\/blazeminds.com\/blog\/wp-content\/uploads\/2015\/07\/2-1024x576.png 1024w\" sizes=\"(max-width: 1366px) 100vw, 1366px\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p><b>Step 3<\/b><br \/>\nAfter selecting Android OS Level you will prompted with this window asking you which type of android layout you wish to use. Right now keep it simple &amp; select <b>&#8220;Blank Activity&#8221;<\/b> and proceed to next.<\/p>\n<p>&nbsp;<\/p>\n<p><a href=\"https:\/\/blazeminds.com\/blog\/wp-content\/uploads\/2015\/07\/3.png\"><img loading=\"lazy\" class=\"alignleft wp-image-11327 size-full\" src=\"https:\/\/blazeminds.com\/blog\/wp-content\/uploads\/2015\/07\/3.png\" alt=\"3\" width=\"1366\" height=\"768\" srcset=\"https:\/\/blazeminds.com\/blog\/wp-content\/uploads\/2015\/07\/3.png 1366w, https:\/\/blazeminds.com\/blog\/wp-content\/uploads\/2015\/07\/3-300x169.png 300w, https:\/\/blazeminds.com\/blog\/wp-content\/uploads\/2015\/07\/3-1024x576.png 1024w\" sizes=\"(max-width: 1366px) 100vw, 1366px\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p><b>Step 4<\/b><br \/>\nIn this window you will be asked about the name of your android activity &amp; layout name. By default Activity Name is\u00a0<b>&#8220;Main Activity&#8221; <\/b>&amp; layout name is <b>&#8220;activity_main&#8221;<\/b>.<\/p>\n<p>NOTE:<br \/>\nDo remember to use only small caps while naming layout name.<\/p>\n<p>&nbsp;<\/p>\n<p><a href=\"https:\/\/blazeminds.com\/blog\/wp-content\/uploads\/2015\/07\/4.png\"><img loading=\"lazy\" class=\"alignleft wp-image-11328 size-full\" src=\"https:\/\/blazeminds.com\/blog\/wp-content\/uploads\/2015\/07\/4.png\" alt=\"4\" width=\"1366\" height=\"768\" srcset=\"https:\/\/blazeminds.com\/blog\/wp-content\/uploads\/2015\/07\/4.png 1366w, https:\/\/blazeminds.com\/blog\/wp-content\/uploads\/2015\/07\/4-300x169.png 300w, https:\/\/blazeminds.com\/blog\/wp-content\/uploads\/2015\/07\/4-1024x576.png 1024w\" sizes=\"(max-width: 1366px) 100vw, 1366px\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p><b>Step 5 (a)<\/b><br \/>\nNow your application is ready to get coded. For this app you Right click your package,(<i><b>here its com.tuts.dbclass<\/b><\/i>) &amp; select New then Java Class<\/p>\n<p>Right click package -&gt; New -&gt; Java Class.<\/p>\n<p>&nbsp;<\/p>\n<p><a href=\"https:\/\/blazeminds.com\/blog\/wp-content\/uploads\/2015\/07\/5.png\"><img loading=\"lazy\" class=\"alignleft wp-image-11329 size-full\" src=\"https:\/\/blazeminds.com\/blog\/wp-content\/uploads\/2015\/07\/5.png\" alt=\"5\" width=\"1366\" height=\"768\" srcset=\"https:\/\/blazeminds.com\/blog\/wp-content\/uploads\/2015\/07\/5.png 1366w, https:\/\/blazeminds.com\/blog\/wp-content\/uploads\/2015\/07\/5-300x169.png 300w, https:\/\/blazeminds.com\/blog\/wp-content\/uploads\/2015\/07\/5-1024x576.png 1024w\" sizes=\"(max-width: 1366px) 100vw, 1366px\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p><b>Step 5(b)<\/b><br \/>\nName this Class <b>DBController.<\/b><\/p>\n<p><a href=\"https:\/\/blazeminds.com\/blog\/wp-content\/uploads\/2015\/07\/6.png\"><img loading=\"lazy\" class=\"alignleft wp-image-11330 size-full\" src=\"https:\/\/blazeminds.com\/blog\/wp-content\/uploads\/2015\/07\/6.png\" alt=\"6\" width=\"1366\" height=\"768\" srcset=\"https:\/\/blazeminds.com\/blog\/wp-content\/uploads\/2015\/07\/6.png 1366w, https:\/\/blazeminds.com\/blog\/wp-content\/uploads\/2015\/07\/6-300x169.png 300w, https:\/\/blazeminds.com\/blog\/wp-content\/uploads\/2015\/07\/6-1024x576.png 1024w\" sizes=\"(max-width: 1366px) 100vw, 1366px\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p><b>Step 6(a) &#8211; <i>Real coding starts<\/i><\/b><br \/>\nHere define your database name, tables &amp; version. Main things you have to keep in your mind:<\/p>\n<p><b>Database Version:<\/b>\u00a0Changing this would delete all your previous records &amp; make new entries. Better to change its version when doing the re-engineering of your application<br \/>\n<b><br \/>\n<\/b><b>Database Name: <\/b>Name of your database. better use small caps &amp; if your DB Name consists of 2 words then use underscore(<b>&#8220;_&#8221;<\/b>) in between. Example: <b>&#8220;<i>db_class<\/i>&#8220;<\/b>.<\/p>\n<p>Same applies to Database Table &amp; Table Column names.<\/p>\n<p><a href=\"https:\/\/blazeminds.com\/blog\/wp-content\/uploads\/2015\/07\/7a.png\"><img loading=\"lazy\" class=\"alignleft wp-image-11331 size-full\" src=\"https:\/\/blazeminds.com\/blog\/wp-content\/uploads\/2015\/07\/7a.png\" alt=\"7a\" width=\"1366\" height=\"768\" srcset=\"https:\/\/blazeminds.com\/blog\/wp-content\/uploads\/2015\/07\/7a.png 1366w, https:\/\/blazeminds.com\/blog\/wp-content\/uploads\/2015\/07\/7a-300x169.png 300w, https:\/\/blazeminds.com\/blog\/wp-content\/uploads\/2015\/07\/7a-1024x576.png 1024w\" sizes=\"(max-width: 1366px) 100vw, 1366px\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p><b>Step 6(b)<\/b><br \/>\nThe most important Helping classes used in making database app are DBHelper, SQLiteDatabase<br \/>\n<b><br \/>\n<\/b><b>DBHelper:<\/b>\u00a0DBHelper would be inner class where all of the SQL queries will be defined.<br \/>\n<b>SQLiteDatabase:<\/b>\u00a0This class is the most important of all as all queries will be executed via this.<br \/>\n<b><br \/>\n<\/b><b>Context: <\/b>\u00a0It will get the current activity of the application from where the query is called.<\/p>\n<p>&nbsp;<\/p>\n<p><a href=\"https:\/\/blazeminds.com\/blog\/wp-content\/uploads\/2015\/07\/7b.png\"><img loading=\"lazy\" class=\"alignleft wp-image-11332 size-full\" src=\"https:\/\/blazeminds.com\/blog\/wp-content\/uploads\/2015\/07\/7b.png\" alt=\"7b\" width=\"1366\" height=\"768\" srcset=\"https:\/\/blazeminds.com\/blog\/wp-content\/uploads\/2015\/07\/7b.png 1366w, https:\/\/blazeminds.com\/blog\/wp-content\/uploads\/2015\/07\/7b-300x169.png 300w, https:\/\/blazeminds.com\/blog\/wp-content\/uploads\/2015\/07\/7b-1024x576.png 1024w\" sizes=\"(max-width: 1366px) 100vw, 1366px\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p><b>Step 6(c)<\/b><br \/>\nTwo most important built-in functions of SQLite database, onCreate &amp; onUpgrade.<\/p>\n<p>When your app fresh installs <b><i>onCreate<\/i><\/b> function is called whereas when you re-install the app then <b><i>onUpgrade<\/i><\/b> function is called.<\/p>\n<p>&nbsp;<\/p>\n<p><a href=\"https:\/\/blazeminds.com\/blog\/wp-content\/uploads\/2015\/07\/7c.png\"><img loading=\"lazy\" class=\"alignleft wp-image-11333 size-full\" src=\"https:\/\/blazeminds.com\/blog\/wp-content\/uploads\/2015\/07\/7c.png\" alt=\"7c\" width=\"1366\" height=\"768\" srcset=\"https:\/\/blazeminds.com\/blog\/wp-content\/uploads\/2015\/07\/7c.png 1366w, https:\/\/blazeminds.com\/blog\/wp-content\/uploads\/2015\/07\/7c-300x169.png 300w, https:\/\/blazeminds.com\/blog\/wp-content\/uploads\/2015\/07\/7c-1024x576.png 1024w\" sizes=\"(max-width: 1366px) 100vw, 1366px\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p><b>Step 6(d)<\/b><br \/>\n<b>Most frequently used database functions:<\/b><br \/>\n1) openDatabase<br \/>\n2) closeDatabase<\/p>\n<p><i><u>1) openDatabase:<\/u><\/i><br \/>\n<i><u><br \/>\n<\/u><\/i>You can open database in 2 ways. One through Opening <i>&#8220;db = dbhelper.getWritableDatabase();&#8221; <\/i>\u00a0where as second way is <i>&#8220;db = dbhelper.getReadableDatabase();&#8221;<\/i>.<br \/>\nIn Writable version you can read as well as write database entries.<br \/>\nIn Readable version you can just only read database entries.<br \/>\nBest option is to use Writable database.<br \/>\nIn the fig attached I have shown how to make function of both of these types.<br \/>\n<b><br \/>\n<\/b><i><u>2) closeDatabase:<\/u><\/i><br \/>\n<i><u><br \/>\n<\/u><\/i>closing database simple. You just have to close Database Helper class.<br \/>\n<i>&#8220;dbHelper.close();&#8221;.<\/i><\/p>\n<p><a href=\"https:\/\/blazeminds.com\/blog\/wp-content\/uploads\/2015\/07\/7d.png\"><img loading=\"lazy\" class=\"alignleft wp-image-11334 size-full\" src=\"https:\/\/blazeminds.com\/blog\/wp-content\/uploads\/2015\/07\/7d.png\" alt=\"7d\" width=\"1366\" height=\"768\" srcset=\"https:\/\/blazeminds.com\/blog\/wp-content\/uploads\/2015\/07\/7d.png 1366w, https:\/\/blazeminds.com\/blog\/wp-content\/uploads\/2015\/07\/7d-300x169.png 300w, https:\/\/blazeminds.com\/blog\/wp-content\/uploads\/2015\/07\/7d-1024x576.png 1024w\" sizes=\"(max-width: 1366px) 100vw, 1366px\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>That&#8217;s all for now. Stay tuned for next part of android database connection. Till then Take Care \ud83d\ude42<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Assalam-u-Alaikum, Welcome again folks. Today in this post I will be guiding you to make Android in App database through SQLite database. So lets get started!<\/p>\n","protected":false},"author":113,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_s2mail":"yes"},"categories":[1455],"tags":[186,1469,1470,1471,1460,1466,1468,1467,1472,1473],"_links":{"self":[{"href":"https:\/\/blazeminds.com\/blog\/wp-json\/wp\/v2\/posts\/11324"}],"collection":[{"href":"https:\/\/blazeminds.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blazeminds.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blazeminds.com\/blog\/wp-json\/wp\/v2\/users\/113"}],"replies":[{"embeddable":true,"href":"https:\/\/blazeminds.com\/blog\/wp-json\/wp\/v2\/comments?post=11324"}],"version-history":[{"count":6,"href":"https:\/\/blazeminds.com\/blog\/wp-json\/wp\/v2\/posts\/11324\/revisions"}],"predecessor-version":[{"id":11340,"href":"https:\/\/blazeminds.com\/blog\/wp-json\/wp\/v2\/posts\/11324\/revisions\/11340"}],"wp:attachment":[{"href":"https:\/\/blazeminds.com\/blog\/wp-json\/wp\/v2\/media?parent=11324"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blazeminds.com\/blog\/wp-json\/wp\/v2\/categories?post=11324"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blazeminds.com\/blog\/wp-json\/wp\/v2\/tags?post=11324"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}