Build Native Cross-Platform Apps with Appcelerator: A beginner's guide for web developers by Ricardo Alcocer
Author:Ricardo Alcocer [Alcocer, Ricardo]
Language: eng
Format: epub
Publisher: JB Orion
Published: 2015-03-24T22:00:00+00:00
5.4 SQLite
Both iOS and Android have native support for SQLite databases. SQLite is a server-less database library that allows you to have a database that you access locally. The main advantage is that the data will come pre-loaded with the app, instead of having to load it at runtime as well as store or modify data at runtime. Going forward, SQLite will give you the flexibility of having multiple tables and query them using SQL Syntax, although that’ll not be needed for our app.
To create databases you need to use third-party software. In my opinion, the easiest is the free SQLite Manager Firefox Extension you can find at https://addons.mozilla.org/en-US/firefox/addon/sqlite-manager/. Download and Install this extension on your Firefox browser, and then go to Firefox >Tools >SQLite manager to launch it. Select Database >New Database and enter ‘scheduledata’ for its name and then select your app/assets folder as its home (Figure 5.19). This will create the file scheduledata.sqlite in /app/assets.
Figure 5.19: Create SQLite Database You’ve successfully created an empty database. Now we need to import our JSON files onto it. SQLite Manager has an import function, but only accepts SQL and CSV files. The easiest way to go around this is to convert our JSON files to CSV, and for this we can use a free online tool called ConvertCSV found at http://www.convertcsv.com/json-to-csv.htm, shown in Figure 5.20.
Figure 5.20: Convert JSON to CSV On this page, select your day1.json and click on “Convert JSON to CSV”. This will load your JSON file and display a comma-separated values version below. The “Include header in first row” checkbox will make the next step easier. Then select Save to Disk to save the CSV file in your local computer.
Now go back to your SQLite Manager and select Import Wizard. From this screen select the file from the previous step, enter day1 as the database name and if the first row contains the column names, make sure you specify it in this screen as seen in Figure 5.21
Figure 5.21: Import CSV to SQLite
Select Ok and the next screen will show you the fields that where found (Figure 5.22). Make sure everything’s correct and hit Ok.
Figure 5.22: Confirm Table Schema
Figure 5.23 shows the results after importing the CSV file. You can make changes to the data at this point, or add new records if needed.
Figure 5.23: SQLite Data Browser Now, repeat the same process for day2 and day3 until you have all the data in your scheduledata.sqlite database.
Having the data ready, it’s now time to modify our code to use SQLite databases instead of Global Properties as before. Titanium has a very robust implementation of SQL found at http://docs.appcelerator.com/titanium/ latest/#!/api/Titanium.Database. Working with SQLite however is not very natural to JavaScript developers. For this reason I’ll be using a library named JSSQL which you can find at https://github.com/ricardoalcocer/ jssql/blob/master/app/lib/com.alcoapps.dbhelper.js . I designed this library to make Titanium SQL Databases feel like working with JavaScript objects. Using this library will make your code more readable and easier to implement the changes in our existing code.
Download
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
Android | E-Readers |
Handheld & Mobile Devices | iPad |
iPhone | Programming & App Development |
Tablets |
Secrets of the JavaScript Ninja by John Resig & Bear Bibeault(6532)
Secrets of the JavaScript Ninja by John Resig Bear Bibeault(6409)
Kotlin in Action by Dmitry Jemerov(5061)
Odoo 15 Development Essentials - Fifth Edition by Daniel Reis & Greg Mader(3318)
Odoo 15 Development Essentials by Daniel Reis(2811)
React Native - Building Mobile Apps with JavaScript by Novick Vladimir(2526)
Learning Angular - Second Edition by Christoffer Noring(2355)
Pride and Prejudice by Jane Austen(2346)
Mobile Forensics Cookbook by Igor Mikhaylov(2014)
Computers For Seniors For Dummies by Nancy C. Muir(1995)
Bulletproof Android: Practical Advice for Building Secure Apps (Developer's Library) by Godfrey Nolan(1868)
Android Development with Kotlin by Marcin Moskala & Igor Wojda(1794)
Building Android UIs with Custom Views by Raimon Ràfols Montané(1786)
1936941139 (N) by Bob Rosenthal(1731)
Building Progressive Web Apps: Bringing the Power of Native to the Browser by Ater Tal(1722)
Hands-On Internet of Things with MQTT by Tim Pulver(1702)
Android App Development by Franceschi Hervé J.;(1700)
Ember.js in Action by Joachim Haagen Skeie(1686)
Hands-On Design Patterns with React Native by Mateusz Grzesiukiewicz(1652)
