How to extract data from a SQLite database with null values

22 ビュー (過去 30 日間)
paloma paleo
paloma paleo 2021 年 2 月 21 日
コメント済み: Gerhard 2024 年 11 月 21 日 13:03
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 Center および File ExchangeDatabase Toolbox についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by