nolanlawson / cordova-plugin-sqlite-2

Native SQLite database API for Cordova/PhoneGap/Ionic, modeled after WebSQL (UNMAINTAINED)
https://nolanlawson.com/2016/04/10/introducing-the-cordova-sqlite-plugin-2/
Apache License 2.0
168 stars 28 forks source link

NULLs get persisted as 'null's (strings) #43

Closed pawelrychlik closed 6 years ago

pawelrychlik commented 8 years ago

The plugin seems to use the bindAllArgsAsStrings() method for binding arguments to the SQL query. https://github.com/nolanlawson/cordova-plugin-sqlite-2/blob/master/src/android/SQLitePlugin.java#L112

Android documentation indicates that the method takes a String array of bind args, none of which must be null. https://developer.android.com/reference/android/database/sqlite/SQLiteProgram.html#bindAllArgsAsStrings(java.lang.String[])

Apparently in consequence, whenever my query arguments contain a javascript null, the value ends up persisted as a "null" string. (I'm running the following code, and then I adb pull the database file and look up the values).

sqlite.transaction((txn) => {
    txn.executeSql("UPDATE MyTable SET name=? WHERE id=?;", [null, 1337]);
});

Can you confirm? Is there any workaround for this? I'm using 1.0.4 on android 5.1.

pawelrychlik commented 8 years ago

and by workaround I don't mean writing a plaintext query.

nolanlawson commented 8 years ago

This is a bug, thank you for reporting. I have a lot of projects on my plate right now, so a test case + a pull request would be awesome. :)

IvanAR commented 6 years ago

Any updates on this MR?

platov commented 5 years ago

@nolanlawson ,

Looks like this issue is reproducing again.

In jsonArrayToStringArray method all the placeholder values are converting to string: https://github.com/nolanlawson/cordova-plugin-sqlite-2/blob/959d53a1825940935fb1df5497355916eca639a9/src/android/SQLitePlugin.java#L311-L313

Then all stringified values are passing into the bindAllArguments function and as result the bindArgs[i] == null expression is always falsy. https://github.com/nolanlawson/cordova-plugin-sqlite-2/blob/959d53a1825940935fb1df5497355916eca639a9/src/android/SQLitePlugin.java#L340-L348

As result null values are bindings using tatement.bindString(i + 1, bindArgs[i]); expression.

nolanlawson commented 5 years ago

@platov Thank you for reporting! Do you have a test case or a suggested PR to fix?

summerswell-ie commented 3 years ago

@platov @nolanlawson - Not sure if this is still an issue for anyone, but I just ran into this as well. I've added a PR that resolves the issue on my side.