简体   繁体   中英

Sqlite on Cordova with cordova-plugin-sqlite-2 limits to 32 Bit

I wrote a cordova app, using the plugin cordova-plugin-sqlite-2 from npm.

If I insert numbers bigger than 32 Bit, the integer is cut of. So if I insert this:

txn.executeSql("INSERT INTO Test (ID) VALUES (17179869322)",[]));

And then select those values again:

txn.executeSql("SELECT * FROM Test",[],function(tx,data){console.log(data)});

I get this

+-----+
| ID  |
+-----+
| 138 |
+-----+

To me it seems like the number is truncated, although this should only happen after 8 byte. Am I doing something wrong here?

I run the app natively on an Android 7 phone.

After some more testing I found, that I can still select the ID in the SQL statement, but the result is still wrong. Very confusing:

txn.executeSql("SELECT * FROM Test where ID=17179869322",[],function(tx,data){console.log(data)});

also results in

+-----+
| ID  |
+-----+
| 138 |
+-----+

Note that I did not do any transformations with the results. The number 322 is read directly in the callback function of the sqlite query methon.

All help appreciated.

The problem was solved through suggestions in comments (it was a problem with the plug-in used). In case it helps anyone in the future, here is an expanded version of the thinking behind that process.

In a situation like this, you have to approach it as a problem of elimination -- carrying out tests to determine which part is at fault. The most obvious candidates are:

  • A problem with SQLite handling >32-bit values;

  • The Cordova framework as a whole has a problem with >32-bit numbers;

  • The specific plug-in you are using has a problem with >32-bit numbers;

  • The way you are using Cordova and/or the plug-in is mishandling 32-bit numbers;

Of these, SQLite is the least likely, since it is known to handle >32-bit numbers. Similarly, although I've no personal experience using it, Cordova is likely to be able to handle >32-bit numbers.

To analyse the other options, probably the most useful diagnostic would be to get a copy of the SQLite database after the insertion and use the SQLite command-line to determine whether the correct values have been inserted or not. Unfortunately, this was not possibly/easy in this case.

However, we can see that both the original insert statement ( INSERT INTO Test (ID) VALUES (17179869322) ) and the targeted select statement ( SELECT * FROM Test where ID=17179869322 ) are both "pure" SQL. The chances are that these will be passed more-or-less directly to the underlying SQLite engine and so would be expected to "work". This is pretty much confirmed by the fact that although the select statement gave the wrong values, it did correctly return one row.

The problem is therefore likely to be either inherent in the framework (Cordova+plug-in) or in the way it is being used (ie the way the returned values were being printed).

Given that inspection of the code to print the results showed no problems, the focus centres on the specific plug-in used, and switching to an alternative confirmed that that was the problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM