Inserting Null into a Table on Android using Bound Parameters inserts the String "NULL" instead.
See original GitHub issueGreetings,
Whenever you attempt to insert a NULL
value as a column
on a table
on an Android
device, a String
of "NULL"
gets inserted instead.
For example, consider the behavior of the following test case.
db.transaction((tx) => {
tx.executeSql('CREATE TABLE IF NOT EXISTS null_test (id INTEGER PRIMARY KEY, value STRING)', []);
tx.executeSql('INSERT INTO null_test (value) VALUES (?)', [null]);
tx.executeSql('SELECT value FROM null_test', [],
(_, result) => {
const { rows } = result;
for (let i = 0; i < rows.length; i += 1) {
const item = rows.item(i);
console.log(i, item); // 0 {value: "NULL"}
}
});
tx.executeSql('SELECT COUNT(*) as count FROM null_test WHERE value IS NULL', [],
(_, result) => {
const { rows } = result;
for (let i = 0; i < rows.length; i += 1) {
const item = rows.item(i);
console.log(i, item); // 0 {count: 0}
}
});
tx.executeSql('DROP TABLE IF EXISTS null_test', []);
});
One would expect the answers to be {value: null}
and {count: 1}
instead. This issue, ultimately, stems from the convertParamsToStringArray
function, and more specifically it’s use in the doUpdateInBackgroundAndPossiblyThrow
method.
This is also an issue in the original cordova plugin. Ultimately the cause of the issue is the API
provided by Android
’s SQLite
library. The compileStatement
method allows for the binding of individual parameters, but it doesn’t allow for a variable length SELECT
statement. Instead, in order to evaluate arbitrary SQL
statements for SELECT
statements rawQuery
must be used. rawQuery
only allows for a String[]
as bound arguments. As such, it’s certainly influenced the decision to use convertParamsToStringArray
in both places.
The fix is relatively straight forward, since the plugin is using compileStatement
and that allows for individual parameters to be bound, we can just do that, and resolve the issue automatically.
It would likely still be an issue for binding NULL
in a SELECT
statement, but it’s not as a likely for that to occur, and I’m not certain how one gets around that while using any query
method.
Thanks for your time, and the plugin,
Expect a Pull Request
Shortly,
Thanks,
~ Ayiga
Issue Analytics
- State:
- Created 6 years ago
- Comments:6 (3 by maintainers)
How can i use Pre-populated SQLite database from my app
We still have this issue.