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 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