1

I am developing a mobile application with Ionic and SQLITE database. I want a scenario where same record should not be inserted twice for Inventory operation. I am checking the database as follows:

 var query = "SELECT COUNT(*) FROM productScan WHERE uniqueId = (?) AND sync = \'N\'"; $cordovaSQLite.execute(db, query, [uniqueId]).then(function (res) { alert(JSON.stringify(res)); }, function (err) { alert(JSON.stringify(err)); }); 

what happen here is when i install the app for the first time and run it then after scan operation when this code run for duplication check, it gives me following output even though there is no record in database

{"rows":{"length":1},""rowsAffected":0} 

I am new to Structured Query Language(SQL). I am not able to parse the result here. The result is coming wrong. Is the query needs to be reformatted or any different way to achieve the goal?

Thanks for your time.

1
  • 1
    The query returns a single row with a single column. Just read that value like you would do for a 'normal' query. Commented Apr 21, 2016 at 10:06

4 Answers 4

2

BEST way to do this is by giving ALIAS to the COUNT(*) .

something like this

db.executeSql("select count(*) AS TOTAL from mydebit where aid = ?", [ parseInt(this.mydata) ]); 

and then

console.log(data.rows.item(0).TOTAL) 

so we can easily give the name to the return result and use the to get he row value cheersss.....!!!!!!!!!

Sign up to request clarification or add additional context in comments.

Comments

1

I do it in this way

JSON.stringify(data.rows.item(0)['COUNT(*)'])

Comments

0

Somehow i managed to achieve it in a different way

var query = "SELECT * FROM productScan WHERE uniqueId = (?) AND sync = \'N\'"; $cordovaSQLite.execute(db, query, [uniqueId]).then(function (res) { if (res.rows.length == 0) { //processed the operation }else{ alert('You have already scanned this asset!.'); } 

Comments

0
 var query = "SELECT COUNT(*) FROM productScan WHERE uniqueId = (?) AND sync = \'N\'"; $cordovaSQLite.execute(db, query, [uniqueId]).then(function (res) { just parse the result,(json.parse(res)) and find by rowsAffected like if(rowsAffected != 0) { //just do the operation } else { //terminate } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.