How to extract data from a SQLite database with null values

22 views (last 30 days)
I have a db file and I would like to extract certain data.
Code:
dbfile = 'name_database.db';
conn = sqlite(dbfile);
%Import all the data from productTable. The results output argument contains the imported data as a cell array.
sqlquery = 'SELECT * FROM table_name';
record_db = fetch(conn,sqlquery);
However, whe a particular table has 'NULL' values I get the following error:
Error using matlab.depfun.internal.database.SqlDbConnector/fetchRows
Unexpected NULL; (zero-based) column index: 1. Details: [initVecVarVecFromSqldbTypes].
Error in sqlite/fetch
NOTE: The databse has been created by a thirdparty, so I cannot modify how the database is created.
Thanks in advance

Answers (2)

Bhomik Kankaria
Bhomik Kankaria on 27 Feb 2021
Hi Paloma,
As of MATLAB R2020a, the handling of NULL values is a limitation of the MATLAB interface to SQLite (this limitation is described towards the bottom of this documentation page).
The only available workaround is to connect to the SQLite database using a JDBC driver (more information on this can also be found in the documentation page linked above).

Gerhard
Gerhard on 21 Nov 2024 at 13:02
Try this, it worked
Suppose, that 'Col03' may contain NULL values, and you want to extract all rows where Col03 is NOT null, then this query will work...
SELECT Col01, Col02, Col03, IFNULL(Col03, 'NaN') from DB_table WHERE .... AND Col03 !='NaN'
IFNULL (arg1, arg2) will replace arg1 by arg2 if arg1 = NULL
  1 Comment
Gerhard
Gerhard on 21 Nov 2024 at 13:03
Ups, correction. The query must look like this:
SELECT Col01, Col02, IFNULL(Col03, 'NaN') from DB_table WHERE .... AND Col03 !='NaN'
Sorry

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!