开发者

Phonegap-Android-sqlite using Javascript. How to connect?

I am new to Android-Phonegap dev. I am creating a project using Eclipse in Windows XP. I am using sqlite database. I saw the sample code in the docs. But I'm not able to execute this example. I am not getting the required results.

Suppose I want to开发者_如何学C get all the entries in the table demo in tabular format, HTML. What will the code be in index.html? For that, what is the procedure and what is the step by step procedure for doing this? Or else any better tutorials which help me to do this?

Thanks in Advance

Dnyan.


in main.js you add this

rowsDataHandler = function(transaction, results) {
    // Handle the results
    var html = "<ul>";
    for (var i=0; i<results.rows.length; i++) {
        var row = results.rows.item(i);
        html += '<li>'+row['data']+'</li>\n';
    }
    html +='</ul>';
    document.getElementById("mydata").innerHTML = html;
}
// load the currently selected icons

loadRows = function(db) {
    try {
        db.executeSql('SELECT * FROM DEMO',[], rowsDataHandler, errorCB);
    } catch(e) {alert(e.message);}
}   

in index.html you add this row inside body

<div id="mydata"></div>


One thing to bear in mind is that if you aren't testing the application on a device or in an emulator, but rather in a browser like Chrome or Safari,

document.addEventListener("deviceready", onDeviceReady, false);

won't work. What I've done is to comment out this line and just to put in a call to

onDeviceReady(); 

When I then test in the emulator I uncomment the "document…" line and comment out

onDeviceReady();


 **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);
  });


this is the method to connect to a db using javascript

db = openDatabase("bprueba","1.0","Prueba_db",5*1023*1024);

SQL Statement Error Callback Reference

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜