最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin2浏览0评论

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 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.

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 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.

Share Improve this question edited Jul 15, 2012 at 3:42 dda 6,2132 gold badges27 silver badges35 bronze badges asked Mar 17, 2011 at 22:04 DnyanDnyan 211 silver badge2 bronze badges 1
  • How are you testing your code? – Zakaria Commented Mar 17, 2011 at 22:21
Add a ment  | 

4 Answers 4

Reset to default 1

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 ment out this line and just to put in a call to

onDeviceReady(); 

When I then test in the emulator I unment the "document…" line and ment 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)

  1. 暂无评论