Quantcast
Channel: Ionic Forum - Latest topics
Viewing all articles
Browse latest Browse all 70429

SQLite Insert with Condition

$
0
0

@bonlemuel wrote:

Hi Everyone,

I'm using this SQLite Plugin. And I just wanna ask. How do you INSERT a record with condition? For instance, you will insert an ID if it does not exist in the database. Here's my actual code.

for(var i = 0; i < $scope.AccountListArray.length; i ++) {
   var select_query = "SELECT * FROM tableChildAccount WHERE id ='"+$scope.AccountListArray[i].id+"'";
   $cordovaSQLite.execute(local_database, select_query, []).then(function(res) {
   if(res.rows.length > 0) {
      console.log($scope.AccountListArray[i].id+" already existed on the database!");
   }
   else {
      console.log("DO NOT EXIST ON DB "+$scope.AccountListArray[i]);
      var insert_query = "INSERT INTO tableChildAccount (id, name, code, token, description, latest_lat, latest_lon, created_at, logged_at) VALUES (?,?,?,?,?,?,?,?,?)";
      $cordovaSQLite.execute(local_database, insert_query,[$scope.AccountListArray[i].id ..........]
        ).then(function(database_entry) {
           console.log("INSERTED ID  -> " + database_entry.insertId);
       }, function (err) {
           console.error("Database Error ->" +JSON.stringify(err));
       });
   }
}, function (err) {
   console.error(err);
});

As you've noticed, I'm getting the data from an Array of data (JSON Data). My problem is, when the code tried to insert a record. I get undefined values on $scope.AccountListArray[i].id.

Please help.

Thank you.

Posts: 3

Participants: 2

Read full topic


Viewing all articles
Browse latest Browse all 70429

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>