Display data from sqlite to List

#1

Hello,
Could you some one help to display data stored in sqlite table to list using ng-repeat. This is working fine for json strings. however not working for sqlite.

Regards,
Suhas

#2

what are you using for SQLite wrapper ? javascript ?
check the object structure returned by the SQLite

post the code for better understanding of the code

#3

Hi Harshit,
I can’t check the object structure since I am executing it in emulator. However I converted sqllite data to json form usingstringify and assigned it to scope variable however the code is not working as ui is not rendering. I have checked the logs which shows stringify scope variable.

#4

You should not be assigning the string to scope variable, directly assign the object returned by the Cordova function and not the stringified string… If that still doesn’t work, please provide the code and the stringified string…

#5

Hi Harshit,
here is my code
var countQuery = “select id,name,image_url,category,price from tblProduct”;
$cordovaSQLite.execute(LBdb, countQuery).then(function(result){
$scope.items = result.rows
}

however above code does not worked hence i stringified as below

if(result.rows.length > 0){
var itemsColl = [];
for (var i = 0; i < result.rows.length; i++) {
itemsColl[i] = result.rows.item(i);

        };

        items = JSON.stringify(itemsColl);
        console.log("scope of items is " + items);
        return items;
    }

this is also not working

HTML code is



{{
item.name }}

{{ item.category}}

        <br />
        <h4 class="price"> {{ item.price }}</h4>
         <i class="icon ion-ios7-arrow-forward"></i>

    </a>
#7

can you stringify the result object and post the output ?
JSON.stringify(result)

also on eclipse check the error you are getting in logcat console

also to access the object in array, u access it as follows

result.rows[i];

and not as result.rows.item(i);

#8

Hi Harshit,

Finally got it working, earlier I have created separate file for db interaction and returning array from it still dont know why angular was not returning it might be some promise related issues.
Hence I moved code to controller and called function when angular loads, this is working now although as a workaround and not the way I want it.
First I need to understand asynchronous stuff and understand what a promise, deffer, digest and apply means then might be I will achieve what I want to

Thank for helping me out

Regards,
Suhas

1 Like
#9

Hi Harshitgoel96,
I am facing the same problem here is my code.

.controller('databaseCtrl', function($scope, $cordovaSQLite){
      var db= $cordovaSQLite.openDB({name: "mydb.db"});
  
     var query=" SELECT * FROM song" ;
     $scope.results=[]; 

     $cordovaSQLite.execute(db, query, []).then(function(result) {
     if(result.rows.length >0){

    for(var i=0; i<result.rows.length; i++){
        $scope.results.push(result.rows[i]);
    } 
    alert(JSON.stringify($scope.results));
    }
  else{
   console.log("####console######## NO results found #######"+"Table record #: ");
   }
}, function(error){
    console.log(error);
})
})
1 Like
#10

instead of
$scope.results.push(result.rows[i]);

use

$scope.results.push(result.rows.items(i));

Regards,
Suhas

1 Like
#11

Any updates on this? Are the codes working? Thank you in advance.

#12

Just want to share this to you. I think this can help.

#13
    indent preformatted text by 4 spaces

How about this? It is not working though
I need help in this too…

 $scope.select = function (lastname) {
        $scope.statusMessage = "Start Fetching "+ $scope.lname;
        var query = "SELECT firstname, lastname FROM people WHERE lastname = ?";
        $cordovaSQLite.execute(db, query, [$scope.lname]).then(function (res) {
            if (res.rows.length > 0) {
                for (var i = res.rows.length - 1; i <= 0; i++) {
                
                    $scope.people.push({
                            fname:res.rows.item(i).firstname,
                            lname:res.rows.item(i).lastname
                    });
                    // $scope.statusMessage = "SELECTED -> " + res.rows.item(i).firstname + " " + res.rows.item(i).lastname;
                };
             $scope.ppl=people;   
           
            } else {
                $scope.statusMessage = "No results found";
            }
        }, function (err) {
            console.error(err);
        });
    }
1 Like
#14

Thanks a Million. It works like charm.

#15

It would be '$scope.results.push(result.rows.item(i)); Note: ‘item’ instead of ‘items’.

1 Like
#16

Hii…
I m trying to open my pre defined database in ionic…which i placed in www directory…but when ever i run the app i m unable to access tabels of my db so plz buddies if u have any solution plz help me out…i m unable to load my database…hope u got my problem…

#17

Hallo every one, i have 2 problem to display data from sqlite.
1. How to insert data from json to sqlite
2. How to display sqlite data on listview

i have try the code above, but i still stack ?
any one can help me ?
thank you

#18

Try with this example: https://github.com/jdnichollsc/Ionic-Starter-Template

Regards, Nicholls

#19

how can i bind select query result to an array without for loop to improve my speed?

#20

ts

db.executeSql("SELECT * FROM usernameList", []).then((data) => {
            
            if (data.rows.length > 0) {

              
              for (var i = 0; i < data.rows.length; i++) {
                console.log(data.rows.item(i));
                this.list_domain=data.rows.item(i).domain_name;
                this.list_name=data.rows.item(i).name;
                this.list_elements.push({
                  domain:this.list_domain,
                  name:this.list_name
                 });
              }
              console.log(this.list_elements);

            }
          });

html

<ion-list>
        <ion-item *ngFor="let data of list_elements">
            {{data.domain}}
            {{data.name}}
        </ion-item>
      </ion-list>