question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Inserting Null into a Table on Android using Bound Parameters inserts the String "NULL" instead.

See original GitHub issue

Greetings,

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:closed
  • Created 6 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
Ash-faqcommented, Nov 8, 2018

How can i use Pre-populated SQLite database from my app

3reactions
alitsvincommented, Sep 28, 2018

We still have this issue.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Android, handle inserting null using String.format()
null is getting converted into "null" because of String.format() . So you need to only insert the idCustomer field for new row and...
Read more >
SQLiteDatabase
Instead, you're encouraged to use insert(java.lang. ... of nullable column name to explicitly insert a NULL into in the case where your values...
Read more >
SQL inserting NULL values
Inserting NULL values. The SQL INSERT statement can also be used to insert NULL value for a column. Example: Sample table: agents.
Read more >
How to set/insert null values to in to a column of a row ...
Using ' ' as null. Insert into SampleTable values (NULL);. While inserting data into a table using prepared statement object you can set...
Read more >
PutItem - Amazon DynamoDB
You can return the item's attribute values in the same operation, using the. ... Empty String and Binary attribute values are allowed. Attribute...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found