Monday, August 19, 2013

Creating an SQLite Driven app for Windows 8 with PhoneGap/ Cordova

A few of us out there might have seen how easy getting an app done is with Cordova for Windows 8.

It's setup is probably the easiest of the lot.

However, while this is true, it lacks the documentation level the other platforms have.

Here is a simple way to have SQLite available in your PhoneGap Windows 8 Project.

1. Open your VS(Visual Studio).
2. Tools > Extensions and Updates.
3. Online >Search for SQLite in the search field and Download SQLite for Windows Runtime.
   Visual Studio Extensions dialog
4. Create a Cordova Windows 8 project (Use Cordova 2.9 as there are isues currently with 3.0).
5. Right click project solution and select add reference.
6. Check Add C++ Runtime and SQLite Runtime
Add Reference dialog
7. Download a WinRT Wrapper Library(Yes PhoneGap still works so don't worry)
8. Extract the content of the file downloaded above.
9. Add the  SQLite3Component\SQLite3Component.vcxproj  from project the project your project (File > Add Existing Project > Select above file from the folder you extracted in 8 above)
8. Right Click project solution once again and and select reference
10. Check Add SQLite3Component\SQLite3Component.vcxproj 
11. Right Click WWW/JS folder and add existing file(Select SQLite3JS\js\SQLite3.js from directory in 8).
12. Drag the reference to the JS into your project and get building.

You can test if configuration works with the following code:

var dbPath = Windows.Storage.ApplicationData.current.localFolder.path + '\\db.sqlite';
SQLite3JS.openAsync(dbPath)
.then(function (db) {
  return db.runAsync('CREATE TABLE Item (name TEXT, price REAL, id INT PRIMARY KEY)')
  .then(function () {
    return db.runAsync('INSERT INTO Item (name, price, id) VALUES (?, ?, ?)', ['Mango', 4.6, 123]);
  })
  .then(function () {
    return db.eachAsync('SELECT * FROM Item', function (row) {
      alert('Get a ' + row.name + ' for $' + row.price);
    });
  })
  .then(function () {
    db.close();
  });
});

*I  used alert in place of the original console.log so you can confirm it works within the project.

You can add plugins you create with no problems.

Special Thanks to Tim Huer for instructions on setting up SQLite for Windows 8 and Marcus Ilgner for creating the the SQLite-WinRT Wrapper for Windows 8.

Ismael O. Jimoh

Saturday, August 10, 2013

Cordova/ PhoneGap 3.0 corrections

Hi all,

In my last post, I mentioned that there might have been an error or 2 with the Download instructions in the Cordova Download Page.

After filing a bug report with the developer team, I got to learn that they were in the process of correcting the Command-Line Interface instructions in the documentation and the documentations as a whole.

This has been corrected and I will like to apologize for any confusion I may have caused.

One new feature it offers customers with a build account is submitting your app to build for devices you do not have set up for your current OS or when you want to build an iOS app on a Windows operating system.

The command:  phonegap build ios when entered to your command line would validate your build account first and if you have one would submit a copy of that app for said version to the Build server.

In summary, PhoneGap 3.0, seems to be the beginning of a merger of sorts of Cordova and the PhoneGap Build products.

This being said however, you would have to ensure that plugins and methods/ function calls used in your app would be able to work on all platforms.

Thanks and sorry again for any confusion I might have caused.

Ismael A.O. E. Jimoh