How to extract data from a SQLite database with null values

22 次查看(过去 30 天)
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

回答(2 个)

Bhomik Kankaria
Bhomik Kankaria 2021-2-27
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 2024-11-21,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 个评论
Gerhard
Gerhard 2024-11-21,13:03
Ups, correction. The query must look like this:
SELECT Col01, Col02, IFNULL(Col03, 'NaN') from DB_table WHERE .... AND Col03 !='NaN'
Sorry

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Database Toolbox 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by