Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get value from table in sqlite Phonegap

Tags:

sqlite

cordova

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?

like image 624
Shruti Avatar asked Jul 06 '13 18:07

Shruti


1 Answers

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.

like image 79
Amol Chakane Avatar answered Sep 26 '22 13:09

Amol Chakane