# SQLite Insert with Condition

**URL:** <https://forum.ionicframework.com/t/sqlite-insert-with-condition/39553>\
**Category:** uncategorized\
**Created:** [December 19, 2015, 3:58am UTC](https://forum.ionicframework.com/t/sqlite-insert-with-condition/39553 "2015-12-19T03:58:12Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![bonlemuel](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/bonlemuel/32/13545_2.png) [@bonlemuel](https://forum.ionicframework.com/u/bonlemuel)\
**Post date:** [December 19, 2015, 3:58am UTC](https://forum.ionicframework.com/t/sqlite-insert-with-condition/39553/1 "2015-12-19T03:58:12Z")

</div>

Hi Everyone,

I’m using this [SQLite Plugin](https://blog.nraboy.com/2014/11/use-sqlite-instead-local-storage-ionic-framework/). 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.

---

<div class="post-metadata">

**Author:** ![nitishrajput01](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/nitishrajput01/32/123978_2.png) [@nitishrajput01](https://forum.ionicframework.com/u/nitishrajput01)\
**Post date:** [December 19, 2015, 8:22am UTC](https://forum.ionicframework.com/t/sqlite-insert-with-condition/39553/2 "2015-12-19T08:22:17Z")

</div>

are you getting id here ?  
console.log($scope.AccountListArray[i].id+" already existed on the database!")

try this  
var select\_query = “SELECT \* FROM tableChildAccount WHERE id =”+$scope.AccountListArray[i].id;

---

<div class="post-metadata">

**Author:** ![bonlemuel](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/bonlemuel/32/13545_2.png) [@bonlemuel](https://forum.ionicframework.com/u/bonlemuel)\
**Post date:** [December 19, 2015, 10:03am UTC](https://forum.ionicframework.com/t/sqlite-insert-with-condition/39553/3 "2015-12-19T10:03:57Z")

</div>

@nitishrajput01:

Thanks for the response. But the ID is a ‘text’. I need to get the data from $scope.AccountListArray[i] to insert it on the database.
