Shruti
Shruti

Reputation: 1574

How to get value from table in sqlite Phonegap

I am using Phonegap sqlite.

I have one table in my database named "Phone" in which I have 3 columns.

Table structure is:

ID     PhoneName  Version
1       A          1.3
2       B           3.4

I am getting the value of ID.

How do I retrieve the value of PhoneName from table?

Upvotes: 5

Views: 12824

Answers (2)

Om Shankar
Om Shankar

Reputation: 283

Use this.

             function devicereadyFun(){
                var dBase = window.sqlitePlugin.openDatabase({
                    name: 'ebe.db',
                    location: 'default'
                });
                dBase.executeSql("select * from Phone where ID = ?", ['1'], function(rsp){
                    alert(rsp.rows.item(0).ID);
                    alert(rsp.rows.item(0).PhoneName);
                    alert(rsp.rows.item(0).Version);
                });


            }           
            document.addEventListener('deviceready', devicereadyFun, false);

Upvotes: 0

Amol Chakane
Amol Chakane

Reputation: 1511

Using cordova 2.7.0 I did it the following way.

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

function onDeviceReady() 
{
     app.initialize();
     getSingleRow(10);   
     getMultipleRows();
}

//Single row
function getSingleRow(id)
{
  db.transaction
  (
       function (tx)
       {
            tx.executeSql
            (
                'SELECT ColumnName FROM tableName WHERE ID=?',
                [id],
                function(tx,results)
                {
                    var len = results.rows.length;
                    if(len>0)
                    {
                        alert(results.rows.item(0)['ColumnName']);
                    }
                }, errorCB
            );
       },errorCB,successCB
   );
}

//Multiple records
function getMultipleRows()
{
  db.transaction
  (
       function (tx)
       {
            tx.executeSql
            (
                'SELECT ColumnName FROM tableName',
                [],
                function(tx,results)
                {
                    var len = results.rows.length;
                    if(len>0)
                    {
                        for (var i = 0; i < len; i++) 
                        {
                            alert(results.rows.item(i)['ColumnName']);
                        }
                    }
                }, errorCB
            );
       },errorCB,successCB
   );
}

Hope that helps.

Upvotes: 9

Related Questions