Tuesday, May 8, 2012

SQLite database on PhoneGap


I want to implement SQLite database for iPhone using PhoneGap. I know some basics SQLite database in iPhone native application. But how can I implement SQLite database in PhoneGap?



Source: Tips4all

4 comments:

  1. It's important to remember that PhoneGap is web apps packaged in a browser component. Everything that applies to mobile WebKit will apply to PhoneGap as well, and the environment in PhoneGap is also very similar to opening an HTML-file in a desktop browser.

    You want what's called a 'Web SQL Database'.

    http://www.w3.org/TR/webdatabase/

    Edit: This specification has been marked as deprecated since the writing of this answer and it's now an officially Bad Idea™ to depend on it.

    In reality, it's based on SQLite in most browsers that support it, but it won't be exactly the SQLite implementation. But it's close. In Chrome or Safari, you can go have a look at it's contents with your developer tools, look at the 'Resources' tab -> Databases (you want to test out basic functionality on a desktop browser before trying in PhoneGap).

    It will work exactly the same in PhoneGap as in desktop browsers.

    Web SQL databases are one implementations of what's more broadly referred to as "local storage". I think that the best introductionary text on that topic can be found in Mark Pilgrim's "Dive into HTML5":

    http://diveintohtml5.info/storage.html

    Still just as valid for PhoneGap as for desktop browsers.

    ReplyDelete
  2. We ended up using the PhoneGap SQLite plugin, here's why:

    We started off using a plain old Web SQL Database in our PhoneGap app, but ran into the following limitations:


    Size of database is limited to 5MB (the user can be prompted to allow more, but we didn't want such prompts in our app)
    Pre-populating the database via javascript adds to initial app load time


    We initially worked around this issue by copying a pre-populated sqlite3 database to a special directory where it would be picked up by WebKit (e.g. as described here)

    However, in our latest attempt to submit the app, we were told the app violated iOS Data Storage Guidelines, presumably because it was copying the database file to a non-standard location.

    So we went with using the PhoneGap SQLite plugin instead: This allowed us to include a large pre-populated sqlite3 database in our app and access it from javascript via the plugin. Unfortunately, the plugin doesn't provide exactly the same javascript interface as the browser, but it's fairly close.

    ReplyDelete
  3. There is an article about it on the phonegap wiki here:
    http://wiki.phonegap.com/w/page/16494756/Adding-SQL-Database-support-to-your-iPhone-App

    ReplyDelete
  4. Everything that applies to mobile WebKit will apply to PhoneGap as well,
    and the environment in PhoneGap is also very similar to opening an HTML-file
    in a desktop browser.


    Not strictly true; for example with the iPhone, UIWebView can't create a relatively large websql database, which can be created programmatically in Javascript in mobile/desktop Safari, without making a call to a delegate method that's considered a private API by Apple (and therefore won't be accepted in the app store). It's actually an real issue.

    ReplyDelete