0

I have key containing 19 digits when I parse it to integer via parseInt() method it changes some of the numbers. When I insert this integer into sqlite table it again changes the number (the col is primary key, not auto incremented and integer type) i-e

  1. '1115430194923422469' string format
  2. 1115430194923422500 after parseInt
  3. 1115430194923422464 value inserted/stored in the database

Now when I retrieve this value from database it return the value mentioned in point 2.

Can anyone explain why this behavior .? The value is same (on retrieval) which was inserted but the the value stored in the sqlite table column is different. How can I add the value correctly so it should remain same in the column.

Here is the code sample.

var keyStr = '1115430194923422469'; var key = parseInt(keyStr); var query = 'INSERT INTO Books (key, type) VALUES (?, ?)'; testdb.execute(query, key, 'Comic');

I am using titanium studio and latest sdk 3.5.1

Hasnain
  • 461
  • 2
  • 11

1 Answers1

1

What is JavaScript's highest integer value that a Number can go to without losing precision? In short: you number is too big to work with it as integer without losing precision in javascript. You can handle it only as text or use some support library like big.js

Community
  • 1
  • 1
farwayer
  • 3,872
  • 3
  • 21
  • 23
  • big.js changes it an object while I need in integer format to insert it in the database. – Hasnain May 03 '15 at 09:36
  • Yes, you can use such numbers in database (sqlite support 64-bit integers). >> big.js changes it an object << `toString()` is overloaded in Big. – farwayer May 03 '15 at 17:23
  • can you update quote en example of how to use it. I have mentioned my code in the question.? – Hasnain May 05 '15 at 08:42
  • Something like this: `var key = new Big('1115430194923422469'); key.add(1); var query = 'INSERT INTO Books (key, type) VALUES (?, ?)'; testdb.execute(query, key.toString(), 'Comic'); ` – farwayer May 05 '15 at 12:13