How to extract data from a SQLite database with null values

36 visualizaciones (últimos 30 días)
paloma paleo
paloma paleo el 21 de Feb. de 2021
Comentada: Gerhard el 21 de Nov. de 2024
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

Respuestas (2)

Bhomik Kankaria
Bhomik Kankaria el 27 de Feb. de 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 el 21 de Nov. de 2024
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 comentario
Gerhard
Gerhard el 21 de Nov. de 2024
Ups, correction. The query must look like this:
SELECT Col01, Col02, IFNULL(Col03, 'NaN') from DB_table WHERE .... AND Col03 !='NaN'
Sorry

Iniciar sesión para comentar.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by