How to implement an SQLite database in Phonegap?

前端 未结 4 1914
长发绾君心
长发绾君心 2020-12-29 11:22

I\'m implementing a cross-platform app for Android, iOS, and BlackBerry. I\'m using PhoneGap to produce native language versions for each platform. I want to know how to cre

相关标签:
4条回答
  • 2020-12-29 11:30

    The PhoneGap documentation on storage is pretty explicit here, and includes some example code. The storage API is modelled on the Javascript API developed under HTML5 used in Opera and Webkit. Here's the relevant page:

    Original 2011 link: http://docs.phonegap.com/phonegap_storage_storage.md.html

    2017 update: now all out of date, but see this: http://docs.phonegap.com/en/1.2.0/phonegap_storage_storage.md.html

    0 讨论(0)
  • 2020-12-29 11:38
    • SQLite database on PhoneGap
    0 讨论(0)
  • 2020-12-29 11:43
     **html**
    
     <input id="show" type="button" value="Show">
    
     **js**
    
     function globalError(tx, error)
       {
         alert("Error: " + error.message);
       }
    
     var db = window.openDatabase('TabOrder', '', 'Bar Tab Orders', 2500000);
     db.transaction(function(tx) {
     tx.executeSql('DROP TABLE IF EXISTS SubmiteData;', null, null, globalError);
     tx.executeSql('CREATE TABLE IF NOT EXISTS SubmiteData (SubmiteDataId integer 
     primary  key, UserId text, AuthNo number, LocId number,ProdId number, 
     CardId number, OrgLat text, OrgLng text, OrgTime text)', 
              null, 
              function()
              {
                SubmiteData("USER1",12345678,23434, 21212, 220232,
                "9", "45", "23/06/2014");
    
              },
              globalError);
       });
    
     function SubmiteData(UserId, AuthNo, LocId,ProdId, CardId, OrgLat, OrgLng, OrgTime){
     db.transaction(function(tx){
     tx.executeSql('INSERT INTO SubmiteData(UserId, AuthNo, LocId, ProdId, CardId, 
     OrgLat, OrgLng, OrgTime) VALUES (?,?,?,?,?,?,?,?)', [UserId, AuthNo, LocId,
     ProdId, CardId, OrgLat, OrgLng, OrgTime], 
                null,
                globalError
               );
      });
     }
    
    
    function read(UserId, AuthNo, LocId,ProdId, CardId, OrgLat, OrgLng, OrgTime){
    
    db.transaction(function(tx) {
    tx.executeSql('SELECT * FROM SubmiteData',
             [],
             function(tx, results)
             { 
               for (var i=0; i<results.rows.length; i++) 
               {   
                   var row=results.rows.item(i);
                  // alert("Id: " + row['UserId']);
                  var stringout = "LocId: " + row['LocId'] + "\n"; 
                   alert(stringout); 
               } 
             },                
             globalError
            );
      });
    };
    
    $(function()
    {
        $('#show').click(read);
    });
    
    0 讨论(0)
  • 2020-12-29 11:45

    Take a look at Lawnchair (http://brian.io/lawnchair/), its 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 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 讨论(0)
提交回复
热议问题