开发者

Phonegap: easiest way to store data / Phonegap reference

I'm looking for 开发者_开发知识库the following info:

  • Full Phonegap reference (couldn't find it on their site)
  • What's the easiest way to implement a database within Phonegap?


Update 2015

The answer below is very out date (and since it recently got an upvote I thought I better readdress it), the Cordova Docs is now the definitive place for documentation, though currently the plugin docs link to the NPM pages which are kind of in transition and a mess. As for the best way to store date, sqllite database that was built into webkit is either gone or deprecated. Lawnchair has been the defacto standard for the past 5 years, as you could swap storage engines in and out, LocalStorage is the simplest way to store data, but beware you around a 5 megs maximum (and I have run into corruption issues with it). I would recommend looking at something like sqllite plugin if you need more than 5 megs (it also has a LawnChair adaptor), or PouchDB and its Cordova Adaptor if you need syncronization to the cloud.

I've left the original answer for posterity...

Original Answer - Circa 2010

The Wiki is the closest thing that phonegap has to a full reference, though it isn't the easiest thing to find on their site. The Javascript API page is probably the most complete reference on external functions available, although it may be a little out of date. The best thing you can do, if something isn't functioning as documented, is go to the Javascript API is to go to the Javascript source (which differs from device to device, in iphone for example, you can go to github and look at the javascript classes and see their exact parameters (you may have to dig into other parts of the source code to see exactly what is going on, but all of the code is pretty straightforward).

As for implementing a database, the best thing to use is the sqlite database built into webkit. Jonathan Stark's excellent book, Building iPhone Apps with HTML, CSS, and JavaScript, has a chapter on doing this.


The easiest way to implement a database is probably to use Lawnchair. It's pretty easy to use and out-of-the box probably does most of what you need (including searching). It's cross-browser, battle tested and degrades nicely through the use of adapters. There is an adapter for Blackberry, and a plugin that supports queries. Here is a quick example using the WebKit adapter, which is good for Android and iPhone, to show how simple it is.

<script type="text/javascript" src="Lawnchair.js" charset="utf-8"></script> 
<script type="text/javascript" src="webkit-sqlite.js" charset="utf-8"></script>
// Open local DB connection
var lawnchair = new Lawnchair({table:'mytable', adaptor:'webkit'}, function(){
    // Lawnchair setup! 
});
// Getting some data out of the lawnchair database
lawnchair.get('my_data_key', function(obj) {
    if (obj !== undefined) {
        lastSyncDate = obj.lastSync;
        dataList = obj.dataList;
    }
});
// Saving to the database
lawnchair.save({key:'my_data_key', lastSync: currentTime, dataList: someData});
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜