# SQLite loaddata problem

**URL:** <https://forum.ionicframework.com/t/sqlite-loaddata-problem/115179>\
**Category:** ionic-v3\
**Created:** [December 12, 2017, 10:25pm UTC](https://forum.ionicframework.com/t/sqlite-loaddata-problem/115179 "2017-12-12T22:25:17Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sanremo2017](https://avatars.discourse-cdn.com/v4/letter/s/8c91f0/32.png) [@Sanremo2017](https://forum.ionicframework.com/u/Sanremo2017)\
**Post date:** [December 12, 2017, 10:25pm UTC](https://forum.ionicframework.com/t/sqlite-loaddata-problem/115179/1 "2017-12-12T22:25:17Z")

</div>

I created a project in Ionic Creator and downloaded it local to implement offline capabilities with SQLite.

**ERROR: TypeError: Cannot read property ‘openDatabase’ of undefined on the line with window.sqlitePlugin.openDatabase in the controller.**

In my controller:

angular.module(‘app.controllers’, [‘ionic’])

.controller(‘berichtenCtrl’, [’$scope’, ‘$stateParams’,’$ionicPlatform’, ‘Message’,‘messageService’,’$cordovaSQLite’,’$ionicHistory’,  
function ($scope,$stateParams,$ionicPlatform, Message, messageService,$cordovaSQLite,$ionicHistory) {  
console.log(‘in controller’);  
$scope.$on("$ionicView.enter", function () {  
$ionicHistory.clearCache();  
$ionicHistory.clearHistory();  
});  
$scope.messages = [];

```
console.log('loaddata start');
db = window.sqlitePlugin.openDatabase({name: "test.db", location:'default'});

var query=" SELECT * FROM messages" ;
db.executeSql(query ,[],function(result) {
    if(result.rows.length >0){

        for(var i=0; i<result.rows.length; i++){
            $scope.messages.push(result.rows.item(i));
            console.log('XXX mess:'+result.rows.item(i).id);
        } 
    }
    else{
        console.log("XXX####console######## NO results found #######"+"Table record #: ");
    }
}, function(tx, error) {
    console.log("ERROR ");

});         
    

$scope.showDelete = false;
$scope.toggleDelete = function(){
    $scope.showDelete = !$scope.showDelete;
}

$scope.deleteItem = function($index){
    Message.delete($scope.messages[$index].id).then(function(){
        $scope.messages.splice($index-1, 1);
    })
}

```

}])

IN services.js:

.run(function($ionicPlatform,$cordovaSQLite,$http,$interval){  
console.log(‘RUN STARTED’);

```
        console.log('RUN: before ready');

$ionicPlatform.ready(function () {
    console.log('RUN: ready');
    //get and store users
    db = window.sqlitePlugin.openDatabase({name: "test.db", location:'default'});
    
    var api_url = 'https://testsite.ie/mobile/getUsers/5a1c472e489de0.87865504';
    db.executeSql('DROP table users');
    db.executeSql('CREATE TABLE IF NOT EXISTS users (id, fname, nr,picture,type,userfunction,userids,users)');        

    return $http.get(api_url).then(function(resp){
        console.log(resp);
        try{
            console.log('setUsers: in try');

            db.transaction(function(tx) {
                console.log('setUsers: in transaction ');

                angular.forEach(resp.data.data.allusers, function(val) {
                    console.log('setUsers: foreach');
                    tx.executeSql("INSERT into users (id, fname, nr,picture,type,userfunction,userids,users)"+
                     " VALUES (?,?,?,?,?,?,?,?)",
                    [val.id,val.fname ,val.nr,val.picture,val.type,val.userfunction ,val.userids ,val.users
                    ],null,errorHandler);
                });
            });
        }
        finally{
            console.log('FINALLY');
        }               
        console.log('END HTTP.GET');
    });

});

```

})

Thanks in advance for your help!

---

<div class="post-metadata">

**Author:** ![mhartington](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/mhartington/32/137757_2.png) [@mhartington](https://forum.ionicframework.com/u/mhartington)\
**Post date:** [July 19, 2021, 9:59pm UTC](https://forum.ionicframework.com/t/sqlite-loaddata-problem/115179/2 "2021-07-19T21:59:26Z")

</div>


