In my job as an Evangelist, i speak to numerous customers of ours. Many a times the customer is in an evaluation mode. They are trying out our tools and have plenty of questions on their mind. My job is to talk to them, understand their problem and provide a solution. I must say I love this exercise, as i get to hear a new problem with every new customer we talk to. Figuring out a solution to a challenge is always a happy moment. Today we discuss one such problem that a customer was facing with respect to Hybrid Mobile Apps.
Customer was trying to go Mobile and was evaluating our AppBuilder. If you have not come across AppBuilder – its an IDE which helps you build Hybrid Mobile Apps & Native Mobile Apps using NativeScript. It lest you code, debug, simulate, package and publish without leaving the IDE. You can know more about AppBuilder here: www.telerik.com/appbuilder. When we spoke to the customer he was trying to create a mobile app and wanted to know if AppBuilder can support a pre-populated SQLite database. That is all we got from him. Lets get to the bottom of this issue now.
Understanding the problem:
Well if i break it down, the issue boils down to below points:
- Customer is developing Hybrid Mobile App
- Customer wants to support SQLite Database in his app
- Customer wants to use a pre-populated SQLite database in his app
Now, all the above are not AppBuilder problem at all. Its got to do with the Hybrid Model of developing mobile apps. So lets see how to solve this problem.
As with any case, i first looked around our documentation resources to see if we have any demos on this scenario. We have a lot of sample apps for our AppBuilder tool. The sample apps can be found here: http://docs.telerik.com/platform/samples/. The samples cover a variety of scenario that you typically would come across when developing a Hybrid Mobile App. One thing to note while developing a Hybrid Mobile App is you need to use Cordova Plugins in order to access any device capabilities.
SQLite To-Do Sample App:
When i searched our samples, i did find a demo which was showcasing how to use a SQLite database in a Hybrid Mobile App. Its known as To-DO App with SQLite. Its a simple To-Do app which uses a SQLite database as a backing store for the todos.
SQLite Cordova Plugin:
In order to use a SQLite database as a backing store for your Hybrid Mobile Apps, you will need to make use of a Cordova Plugin called SQLite Plugin. This plugin has been verified by our team with respect to using it in a production app and has been certified. Here is where you can read more on this plugin: http://plugins.telerik.com/cordova/plugin/sqlite.
SQLite Cordova Plugin Source Code:
Source code for the SQLite plugin is available on the GitHub. Here is the link: https://github.com/Telerik-Verified-Plugins/SQLite. The read me contains the documentation of how to use this plugin in an app.
Using Pre-populated database:
The SQLite To-Do sample app that i talked about earlier was not using a pre-populated database. if you look at the source code of the app, it creates a SQLite database at run time. When the app runs, it opens up the db and creates the table if not already found. Here is the source code which creates the DB and creates the table:
Now how do we support a pre-populated database. Well, it turns out that the SQLite plugin that we have used supports opening a pre-populated database in your app. I went back to the documentation of the SQLite plugin here: https://github.com/Telerik-Verified-Plugins/SQLite. found a section which talked about supporting pre-populated database here: https://github.com/Telerik-Verified-Plugins/SQLite#pre-populated-database. I had to do the following steps to support this:
- Copy the pre-populated SQLite database in the “www” directory
- Modify the open database code as below:
As you can see, the syntax was to let the plugin know that it has to open a pre-populated database instead of creating a new one. The property used is “createFromLocation” and value provided to that property is 1. By this we are telling the plugin to look for the DB in www directory.
The SQLite plugin has the capability to create DB at run time as well as reading a pre-populated db in your app. Depending on your scenario you can use this plugin in your Hybrid Apps. Hope this helps you in your development if you are having a similar situation.
Till next time – Happy Coding.