Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQLite database on PhoneGap

Tags:

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?

like image 418
donkarai Avatar asked Feb 28 '11 07:02

donkarai


2 Answers

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.

like image 77
James Avatar answered Oct 05 '22 15:10

James


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.

like image 25
Jacob Oscarson Avatar answered Oct 05 '22 15:10

Jacob Oscarson