Sqlite integration

Hello guys,
I trying to integrate sqlite in ionic framework. followed each every step mentioned in the blow blog

I was able to create DB and was able to insert data in it.

However when trying to select some data I got undefined erro

My code is as below

in app.js
var LBdb = null;

in .run method
LBdb = $cordovaSQLite.openDB({ name: “lbite.db” , bgType: 1});
$cordovaSQLite.execute(LBdb, “CREATE TABLE IF NOT EXISTS tbl(id )”)

in controller I am acessing it as below

var countQuery = “select id from tbl”;
$cordovaSQLite.execute(LBdb, countQuery).then(function(res){
return res;

Upon searching I came to know that Ionic first loads controllers and services which causes this problem.
Please let me know if you have workaround for this
Regards,
Suhas

1 Like

Hi @suhaschitade,

Glad you checked out my blog article!

Can you confirm a few things?:

  1. What exactly is undefined?
  2. Are you trying to run your SELECT statement inside an onDeviceReady() or $ionicPlatform.ready() method?

Regards,

Hi,
Thanks for your response. I managed to solve the problem. However I came across new problems when trying to bind data to the list from sqlite. Do you have any example of it

What are the problems?

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

ng-click="showDeatails(item.id)"
class=“button_clear item item-thumbnail-left item-icon-right”>

{{ item.name }}

{{ item.category}}

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

</a>

so in short I am unable to find easiest data binding with sqlite.

You should convert results returned from sqlite to an array. Following code does that.

function toArray(results) {
    var output = [];
    var i;
    for (i = 0; i < result.rows.length; i++) {
        output.push(result.rows.item(i));
    }
    return output;
};
1 Like

I agree with @esery1

1 Like

Thanks for the inputs. I’ll try this, however the real issue is setting scope variable. I guess scope variable is not updating somehow and that’s why view is not rendering properly
Regards,
Suhas

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

Thanks everyone for helping me out

Regards,
Suhas