我正在 MATLAB 中使用 SQLite 数据库,并尝试将 NULL 值插入表的某些字段。但是,当我尝试插入 NULL 时,MATLAB 会抛出错误。以下是示例:
% Define the SQLite database file name
dbFileName = 'example.db';
% Delete the database file if it exists
if exist(dbFileName, 'file') == 2
delete(dbFileName);
end
% Connect to the SQLite database
conn = sqlite(dbFileName, 'create');
% Create a sample table
createTableQuery = [ ...
'CREATE TABLE example_table (' ...
'id INTEGER PRIMARY KEY, ' ...
'name TEXT, ' ...
'age INTEGER DEFAULT NULL' ...
')'];
exec(conn, createTableQuery);
% Insert data with a NULL value for the 'age' field
insert(conn, 'example_table', {'id', 'name', 'age'}, {1, 'John', []});
% Close the database connection
close(conn);
当我运行此代码时,MATLAB 抛出以下错误:
Error using sqlite/insert
Inconsistent row counts for column 0 (1) and column 2 (0).
Error in test (line 22)
insert(conn, 'example_table', {'id', 'name', 'age'}, {1, 'John', []});
如果我将 [] 替换为 0,插入会成功,但这不是真正的 NULL 值。如何使用 MATLAB 将实际 NULL 值插入 SQLite 数据库表?是否有特定的语法或解决方法?